Displayr has a built-in Sample Size Description widget (under Insert > More > Data > Sample Size Description) that you can use to describe the data being displayed, as outlined in this post. But what if the default text isn't quite what you want? This post explains how to easily customize the text to your liking. You will see how to change, reorder, and remove elements of the description, as well as modify it to reference filters selected in a combo box.

Breaking down the fields

The text in the sample size description output has 5 parts: Initial text, Sample description, Sample size description, Sample size, and Final text.

You can use the Object Inspector (as seen below) to customize some of the aspects of the Sample Size Description output: Initial text, Sample size description, and Final text. If your data is not filtered, the text in Total sample description field will be shown for Sample description.

Other bits of the output, Sample description and Sample size, are determined in the underlying R code of the output. The Sample size field displays the number of cases specified by the Complete data variable, including any filters applied. The Sample description field displays the name or names of any filters applied or the text in the Total sample description field.

Changing the basic fields in the Object Inspector allows for some customization. For deeper customization, you need to edit the R code.

Deeper Customization with R

Basic edits to the sample size description text output using R aren't as difficult as they sound. Changes such as reordering or removing the fields involves editing a single line of code.

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.

  1. Reordering the text: To swap the order of the fields, change the order of the text inside the paste0() function, such as: paste0(formN, n, formInitial, base, formFinal)
  2. Removing text: To remove a field, delete it from inside the parentheses. The following example removes "Base: total sample;" from the output: paste0(formN, n, formFinal)
  3. Adding custom text: To add custom text to the description, add it to the function in quotation marks, like so: paste0(formInitial, n, " respondents ", base).

Advanced Customization: Dynamic updating with Combo or List Boxes

If I have an R variable 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 this blog post. Like other charts or visualizations, the Sample Size Description must first be connected to the filer used in the combo box. In the Object Inspector of the Sample Size Description, select same filter variable used in the Combo Box in Inputs > FILTERS & WEIGHT > Filter(s).

 

 

After selecting the filter variable, the text updates to reflect the new Sample size. But, it only displays the name of the combo box filter - Gender - and not if Male or Female is selected. To enable that, we need to edit the R code.

To make the sample description field react to the combo 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 box or control used with your filter variable. That small change means the sample description field will update as the selections in the combo box change. The text will update to include all selections in the combo box, so if the combo box allows multiple selections, the text may become rather large.

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, we 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 set 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)

This code compares the number of possible selections from the underlying question to the number of selections in the combo box. If they're the same, it stores TRUE in all.selected. Then, replace base in the final paste0() line with ifelse(all.selected, formTotalSample, base).

Rather than showing just the selections in the combo box, if the number of selections matches the number of variables in the underlying question, the widget will display the text in the Total sample description field just as it does when not using a filter connected to a combo box.