How can I print a custom warning/ error message when the required data is empty?
for instance, in my server.R, I have this code below,
output$plot = renderPlot({
# Sites.
site1 = input$site1
# Prepare SQL query.
query <- "SELECT * FROM datatable
WHERE sites.id = 'SITE1'
"
# Match the pattern and replace it.
query <- sub("SITE1", as.character(site1), query)
# Store the result in data.
data = dbGetQuery(DB, query)
if (is.na(data) || data == '') {
# print error/ warning message
"sorry, no data is found."
} else {
# plot the data
dens <- density(data$particles, na.rm = TRUE)
plot(dens, main = paste("Histogram of ", "particles"),
xlab = "particles")
}
I get this unfriendly red error message below when no data is found.
error: need at least 2 points to select a bandwidth automatically
ideally,
sorry, no data is found.
Any ideas?