闪亮-自定义警告/错误消息? [英] Shiny - custom warning/error messages?
本文介绍了闪亮-自定义警告/错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当所需数据为空时,如何打印自定义警告/错误消息?
例如,在我的server.R中,我有以下代码
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")
}
找不到数据时,我收到下面这条不友好的红色错误消息。
error: need at least 2 points to select a bandwidth automatically
理想情况下,
sorry, no data is found.
有什么想法吗?
推荐答案
因为我们需要将绘图返回到renderPlot()
,所以需要在plot()
函数中显示错误/警告。
我们正在绘制一个"white"
颜色的空白散点图,然后在图的中间-x=1
,y=1
用text()
函数添加错误信息,参见下面的工作示例:
#dummy dataframe
data <- data.frame(sites.id=rep(letters[1:3],10),particles=runif(30))
#subset - change "SiteX" to "a" to test ifelse
data <- data[data$sites.id=="SiteX", ]
if(nrow(data) == 0) {
# print error/ warning message
plot(1,1,col="white")
text(1,1,"no data")
} else {
# plot the data
dens <- density(data$particles, na.rm = TRUE)
plot(dens, main = paste("Histogram of", sites.id, "particles"),
xlab = "particles")
}
这篇关于闪亮-自定义警告/错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文