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. There are a couple of ways this is typically handled:
Method - Within the code of the R-based output
Method - Hiding an output using a Calculation
Method - Suppressing an error with tryCatch()
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
Please note this requires the Data Stories module or a Displayr license.
- 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 R-based output
This method can be used when the output is created using R Code, such as a custom Calculation or R-based visualization. You will essentially check something about the item being referenced/created, and return an error message if it fails the criteria.
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
}
If you're adding this type of handling to an r-based visualization or other output, you should find the name of the final object being created by the output and use that as the "input" above. This is typically the Properties > General > Name, but if that doesn't work, then you should review the code for something like viz in the snapshot below:
Method - Hiding an output using a Calculation
This method is used when you'd like to hide an output using a filled-in box over top of it. You must use this method when trying to hide/customize error messages of items on a page that are not R-based - like our built-in tables. This code will hide the output underneath with a white box (you can change the color if needed), and show a custom 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)
}
Method - Suppressing an error with tryCatch()
Sometimes when an output returns no data, it can cause that output or other downstream outputs to return an error. When this happens, the output and other outputs affected will be highlighted in red. The original output with error will show the error message (the output on top below) and downstream affected outputs (the visualization using the output on top) will show their previous results until the original error is fixed:
When items error, their processing immediately stops, and you cannot use the two handling methods described above. Also, items depending on items returning an error (like the visualization in the screenshot above) cannot be recalculated unless the upstream item is removed as a dependency or the error is corrected. This chain of events mainly occurs when doing a lot of interactive filtering and manipulation of tables/data with R.
To suppress the error and allow for a better error message and handling, you can use the tryCatch() function. You must nest the code inside the tryCatch() and provide a new error message.
For example:
1. In the example above, select the top output and go to Properties > R CODE.
2. If prompted, click Yes to acknowledge you are editing the code and that it may mess up the original output.
3. Paste the following on the first line of the code:
##EDITING FOR ERROR HANDLING
#suppress the original error state
tryCatch({
4. Paste the following on a new line at the bottom of the code. You will need to find the name of the output created to use in the code. This is usually the Properties > GENERAL > Name:
#call the name of the output created above i.e. viz, viz.2, turf
#this is usually Properties > GENERAL > Name
table.output
# add the error handling
} ,error=function(e){""}) #add custom error message inside ""
5. The code above will render the output as simply blank and return the error message.
If you want to see the actual error messages again, you can still show them, but not make the output an error by modifying the code at the bottom as follows which prints whatever error (e) is returned:
} ,error=function(e){print(e$message)})
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