Introduction
This article describes how to customize the sample size description using R code:
Standard output when using a sample size description, also known as sample size widget:
Customized sample size description using R code:
Requirements
You will need a sample size description on the page.
Method
The text in the sample size description output has 5 parts: Initial text, Sample description, Sample size description, Sample size, and Final text.
To view the R code behind the sample size description widget, go to Properties > R CODE in the Object Inspector. The important line of code that controls the output text is the last line:
paste0(formInitial, base, formN, n, formFinal)
Each of the fields inside the parentheses in the code corresponds to one of the text fields of the sample size description.
Method 1 - reordering the text
- To reorder the fields, from the object inspector, go to Properties > R CODE and change the order of the text inside the paste0() function, such as:
paste0(formN, n, formInitial, base, formFinal)
Method 2 - removing text
- To remove a field from the description, delete it from inside the parentheses in the R CODE. The following example removes "Base: total sample;" from the output:
paste0(formN, n, formFinal)
Method 3 - adding custom text
- To add custom text to the description, add it to the function in quotation marks with a space on each side, like so:
paste0(formInitial, n, " respondents; ", base)
Method 4 - dynamic updating with combo or list boxes
If you have a filter that is connected to a combo box or list box, the Sample Size Description output updates as the filters change, but the underlying R code needs further editing in order to show the actual selections in the control. To learn how to connect a filter to a combo or list box, please see:
How to Create a Combo Box Filter
How to Create a List Box Filter
How to Connect Filters to Controls using R Code
- In the object inspector of the Sample Size Description, select the same filter variable used in the combo or list box in Inputs > FILTERS & WEIGHT > Filter(s).
-
To make the sample description field react to the combo or list box selection, change the first line of code from:
base <- attr(QFilter, "label")
to:
base <- toString(Combo.box)
where "Combo.box" is the name of your combo or list box used with your filter variable.
3. If all the filters are selected, the output will show all of the included categories and not the text in the Total sample description field. To do that, you will need to make a few more edits to the R code.
Replace
base <- attr(QFilter, "label")
with the code below, changing Combo.box to the name of your combo box control and d3 to the name of the variable the combo box is based on.
available.items <- nlevels(d3)
selected.items <- length(Combo.box)
all.selected <- ifelse(selected.items == available.items, TRUE, FALSE)
base <- toString(Combo.box)
If you are using multiple response data in your filter, use the R code below. It counts the possible selections in the question and excludes the NET.
available.items <- ncol(subset(d3, select=-c(NET)))
selected.items <- length(Combo.box)
all.selected <- ifelse(selected.items == available.items, TRUE, FALSE)
base <- toString(Combo.box)
Method 5 - customize text
When using a Sample Size Description the user would like to modify the output text in a way to:
include the filters selected in View Mode to the description exclude the filter applied to the output directly from the description Example: Looking at this page the Sample Description has the Date From and Date To Filter applied to the output. When selecting additional filters in View Mode the user would like only these to appear, while not showing the Date From and Date To Filter description.
All the text shown for the filter part of the description comes from the same place, which is the concatenation of all of the labels of filters that have been applied to the sample description. The only way to exclude part of it is to know what text you want to remove and then remove it. One way to do that is to insert these lines before the final line of R Code in the Sample Size Description:
Here, we consider three cases. The first two are when an additional filter has been applied, and so the text we want to remove is " and Date From and Date To" or "Date From and Date to and ". We replace either of those two with a blank. The third case is when no other filter has yet been applied, so the text we want to remove is just "Date From and Date To". Here I have replaced it with whatever text has been entered in the Total sample description field, but you could replace it with something else if desired._"
See Also
How to Add Customized Footers and Sample Size Widgets
How to Create a Combo Box Filter
How to Create a List Box Filter
How to Connect Filters to Controls using R Code
Comments
0 comments
Article is closed for comments.