This article describes how to customize the sample size description using R code.
You will start with the standard output when using a sample size description (also known as a sample size widget):
And modify it into a customized sample size description using R code:
The following examples are covered:
How the text maps to the R CODE
Method 1 - Reordering the Text
Method 4 - Dynamic Updating with Combo or List Boxes
Method 5 - Customize labels for filters applied
Requirements
Please note these steps require a Displayr license.
- You will need a sample size description on a Page.
- Knowledge of the paste() function is useful: How to Use Paste Functions to Create Dynamic Text Using R.
How the text maps to the R CODE
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 Data > Show Advanced Options > 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 Data > Show Advanced Options > 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 Data > 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:
#Replace Combo.box with the name of your combo or list box
base <- toString(Combo.box)
If you wish to instead include both selected categories from a combo box and the filter labels when a filter is applied, you can combine them as follows:
base_f <- attr(QFilter, "label")
base_c <- toString(Combo.box)
# Show combo box selections and include filter label only when a filter is applied
base <- ifelse(base_f != "Total sample", paste0(base_f, "; ", base_c), base_c)
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 show the text from the inputs tab paste in the appropriate bit of code below and change Combo.box to the name of your combo box control and d3 to the name of the variable the combo box is based on:
-
- If the combo box is using a Nominal (categorical) variable:
#how many categories are in your variable - replace d3 with the variable
available.items <- nlevels(d3)
#count how many items are selected in the combo box - replace Combo.box with the name of yours
selected.items <- length(Combo.box)
#create a T/F variable for if the categories selected = the categories in your variable
all.selected <- ifelse(selected.items == available.items, TRUE, FALSE)
#change the base description to the Total sample description if all is selected
base = ifelse(all.selected, formTotalSample, base)
-
- If the combo box is using multiple response data in the filter:
#how many categories are in your variable - replace d3 with the variable
available.items <- ncol(subset(d3, select=-c(NET)))
#count how many items are selected in the combo box - replace Combo.box with the name of yours
selected.items <- length(Combo.box)
#create a T/F variable for if the categories selected = the categories in your variable
all.selected <- ifelse(selected.items == available.items, TRUE, FALSE)
#change the base description to the Total sample description if all is selected
base = ifelse(all.selected, formTotalSample, base)
-
- If dealing with multiple control boxes:
1. Repeat the previous code for each control on your page, but rename the last line uniquely as base1, base2, and so on:
...
base1 = ifelse(all.selected, formTotalSample, base)
...
base2 = ifelse(all.selected, formTotalSample, base)
2. Add a line above the final line of code in the output to paste these base strings together, separated by a semi-colon:
base = paste0(base1, "; ", base2)
Method 5 - Customize labels for filters applied
You can exclude or modify the labels from specific filters if wanted. For example, you only want to show the names of filters selected in View Mode and not the Date From and Date To applied to the widget in the Filter(s) dropdown (in the screenshot below).
You can exclude or modify the names of the filters using the gsub() function in R. To remove the date filter labels you'd use something like:
#specify filter label to remove from description
text.to.remove = "Date From and Date To"
#remove if found at the end of the filter list
base = gsub(paste("and", text.to.remove), "", base)
#remove if found at the beginning of the filter list
base = gsub(paste(text.to.remove, "and"), "", base)
#remove if only filter applied
base = gsub(text.to.remove, "", base)
If you'd like to replace the text with something else instead of removing it, you can add the replacement inside the empty "" in the code above.
Next
How to Show / Hide / Edit Captions for Tables and Charts
How to Add Customized Footers and Sample Size Widgets
How to Create a Combo Box Filter