【发布时间】:2015-05-05 07:46:02
【问题描述】:
当所需数据为空时,如何打印自定义警告/错误消息?
例如,在我的 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.
有什么想法吗?
【问题讨论】:
-
试试
nrow(data)==0而不是is.na(data)?并用plot()显示错误信息,因为我们在renderPlot()里面。 -
我怎样才能
display the error message with plot()? -
类似
plot(1,1); text(1,1,"no data") -
谢谢。你的意思是像我上面的编辑吗?
-
我已尝试使用
plot(1,1); text(1,1,"no data"),但我仍然收到此错误need at least 2 points to select a bandwidth automatically