Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

r - Shiny - custom warning/error messages?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In some cases you may want to use validate, see accepted answer there.

For other cases I made a function based on @zx8754's answer, but using ggplot2. Here it is ready to be packaged.

Where you would have called :

stop("sorry, no data is found.")

call

return(plot_exception("sorry, no data is found."))

the function :

#' plot message for exception
#' 
#' Useful to display messages in code{shiny} reports
#'
#' Typically call code{return(plot_exception(...))} where you would have called code{stop(...)}
#' @param ... text to display, concatenated with sep
#' @param sep separator used for concatenation
#' @param type function to use to print in console
#' @param color text color, by default red for message and warning else black
#' @param console if TRUE print in console, if FALSE just plot
#' @param size text size
#' @examples
#' plot_exception("no data for current filter selection")
#' plot_exception("NO","WAY","!!!",color="blue",size=12,console=FALSE)
#' @export
plot_exception <-function(
  ...,
  sep=" ",
  type=c("message","warning","cat","print"),
  color="auto",
  console=TRUE,
  size = 6){      
  type=match.arg(type)
  txt = paste(...,collapse=sep)
  if(console){
    if(type == "message") message(txt)
    if(type == "warning") warning(txt)
    if(type == "cat") cat(txt)
    if(type == "print") print(txt)
  }
  if(color =="auto") color <- if(type == "cat") "black" else "red"
  if(txt == "warning") txt <- paste("warning:",txt)
  print(ggplot2::ggplot() +
          ggplot2::geom_text(ggplot2::aes(x=0,y=0,label=txt),color=color,size=size) + 
          ggplot2::theme_void())
  invisible(NULL)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...