This article describes different options for handling outputs with no data or insufficient data (eg, data with small sample size, small counts, etc.), or any other type of restriction you want to set to your data outputs.
We start with a bit of the report that normally looks like this:
When filtering, sometimes the filters yield an output with no data of insufficient data resulting in unattractive and generic errors messages:
You can use R to create custom errors as you like (eg 'No Data to Report'):
Requirements
- A Displayr document containing a data set. Click here to download the QPack of the document used in the examples below. See: How to Import a QPack into a New Document.
- An output filtered using controls. See How to Create a Combo Box Filter for how to create this.
- If basing criteria off of a Count or Sample Size, those statistics must be present on the table or a copy of the table being hidden/handled. The input table I'm using for the examples below looks like:
Method - Within the Code of the Output
This method is used when the Calculation output is created using custom R code to which you wish to add error handling.
The below code corresponds to Coke's Preference percentage Calculation in the red box above (and the top right of the page in the example document).
- Click Calculation and draw a box on the page.
- Paste in the following R code in R CODE:
#select table with relevant data
input=table.Preferred.cola.2
#create if else statements based on error criteria
#the last else returns the normal output
if(length(input) == 0){
"No Data to Report"
} else if(input[1,"Sample Size"] < 10){
"Sample size is\under 10"
} else {
input["Coca-Cola",1]
}
To test this, you can select the first two categories in the Age and Income combo boxes on the page (see QPack).
You can customize the above script for other types of custom calculations. Just make sure to change the input and put your custom calculation inside the else{} (note you can include multiple lines of code within each {} section). A generic framework for you to fill in is as follows with bits to edit in bold:
#select input with relevant data
input=YourTableOrOtherOutputOrVariable
#create if else statements based on error criteria
#the last else returns the normal output
if(TestIfInputIsEmpty){
"Your custom no data message"
} else if(TestIfInputIsSmallOrWhateverYouWish){
"Your custom error message"
} else {
YourFinalResultWhenThereIsEnoughData
}
Method - Overlaying a Calculation
This method is used when you'd like to hide an output using a box with HTML. You can also include an error message, if you wish.
- Click Calculation and draw a box on the page.
- Paste in the following R code in R CODE:
#select table with relevant data
input=table.Preferred.cola.2
#set error message equal to blank normally
errormsg = ""
#create appropriate error message based on the data
if(length(input) == 0){
errormsg = "No Data to Report"
} else if(input[1,"Sample Size"] < 10){
errormsg = "Sample size is\nunder 10"
}
#if there is an error, output the error message in
# a white box to cover the output
if(errormsg != ""){
html = paste0(
'<!DOCTYPE html><html>
<head><style>
body { background-color: white; }
</style></head>
<body><center>',
errormsg,
'</center></body></html>')
rhtmlMetro::Box(html, text.as.html=T)
}
To test this, you can select the first two categories in the Age and Income combo boxes on the page in the example document (see QPack). A generic framework for you to fill in is as follows with bits to edit in bold:
#select table with relevant data
input=YourTableOrOtherOutputOrVariable
#set error message equal to blank normally
errormsg = ""
#create appropriate error message based on the data
if(TestIfInputIsEmpty){
errormsg = "Your Custom No Data Error Message"
} else if(TestIfInputIsSmallOrWhateverYouWish){
errormsg = "Your Custom Error Message"
}
#if there is an error, output the error message in
# a white box to cover the output - or change the background-color below
if(errormsg != ""){
html = paste0(
'<!DOCTYPE html><html>
<head><style>
body { background-color: white; }
</style></head>
<body><center>',
errormsg,
'</center></body></html>')
rhtmlMetro::Box(html, text.as.html=T)
}
Next
How to Work with Conditional R Formulas
How to Hide an Output with a Small Sample Size Using an R Error
How to Hide an Output with a Small Sample Size Using a Calculation
How to Blank or Cap R Table Cells Based on a Different Statistic Using R
How to Hide Rows or Columns Based on Sample Size Using R
Comments
0 comments
Please sign in to leave a comment.