This article describes how to add a hidden warning message that appears when the sample size on a page drops below a specific number.
When the sample size is above the specified threshold:
When a filter is applied and the sample size drops below the specified sample size threshold, a warning appears:
Requirements
You will need the following:
- At least one output on a page to filter,
- a combo box filter that has been applied to the output(s) on the page, and
- a sample size widget.
Please note these steps require a Displayr license.
Method
- From the toolbar, select Anything
> Calculation > Custom Code.
- Click on the page to place the calculation.
- Enter the code below:
# Set the threshold for small sample
min.sample = 30
# Obtain the current filtered sample
current.sample = length(which(QFilter & !is.na(Q3_01)))
#Change the text of the message
contents = ifelse(current.sample < min.sample, "Warning! Low base size, exercise caution.", "")
#Change the background color of the message
bgcolor = ifelse(current.sample < min.sample, "#42afe3", "#FFFFFF")
#Change the opacity of the message
opacity = ifelse(current.sample < min.sample, "1", "0")
#Set the font color
textcolor = "#FFFFFF"
#Build the HTML for the text
your.html = paste('<DOCTYPE html><html><head><style>div.mystyle {font-size: 16; font-family: Open Sans; padding-left: 10; padding-top: 10; padding-bottom: 10; background-color:', bgcolor, '; color: ', textcolor,'; opacity:', opacity, ';}</style></head><body><div class = "mystyle">', contents, '</div></body></html>', sep="")
# Render the HTML
rhtmlMetro::Box(text = your.html, text.as.html = TRUE)
3. Modify the second line (min.sample) to set the desired threshold for small sample.
4. Modify the fourth line to incorporate variable(s) that define the sample. In this example, I've used Q3_01.
5. OPTIONAL: You can also modify the background and font colors using color hex codes.
6. Click Calculate if Automatic is not already ticked.
7. Apply the combo box filter and if the sample size threshold is not met, the sample size warning will appear on the page.