Doug Fir
Doug Fir

Reputation: 21292

ggplot with tryCatch: want blank plot if there's an error during expression

Some data:

x %>% dput
structure(list(date = structure(c(18782, 18783, 18784, 18785, 
18786, 18787, 18789, 18791, 18792, 18793, 18795, 18797, 18798, 
18799, 18801, 18803, 18805, 18806), class = "Date"), `Expired Trials` = c(3L, 
1L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L), `Trial Sign Ups` = c(3L, 1L, 1L, 2L, 3L, 4L, 1L, 1L, 1L, 
1L, 2L, 1L, 3L, 2L, 2L, 1L, 1L, 1L), `Total Site Conversions` = c(3, 
1, 1, 2, 3, 4, 1, 1, 1, 1, 2, 1, 3, 2, 2, 1, 1, 1), `Site Conversion Rate` = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_), `Trial to Paid Conversion Rate` = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_)), row.names = c(NA, -18L), class = c("tbl_df", 
"tbl", "data.frame"))

Context is within a shiny app where sometimes field 'Sessions' will exist and others it won't, depending on the users selections. Rather than display the red warning message, I just want nothing or a blank plot shown instead of an error message:

x %>% 
     ggplot(aes(date, Sessions)) +
     geom_col(na.rm = T) +
     geom_line(aes(y = `Site Conversion Rate`), na.rm = T)
Error in FUN(X[[i]], ...) : object 'Sessions' not found

Tried:

tryCatch(expr = {x %>% 
    ggplot(aes(date, Sessions)) +
    geom_col(na.rm = T) +
    geom_line(aes(y = `Site Conversion Rate`), na.rm = T)
}, 
error = function(e) {message(''); print(e)},
finally = {ggplot() + theme_void()})

But, this still spits out the error, wanted/expected a blank plot instead.

How can I do this?

Upvotes: 2

Views: 313

Answers (1)

akrun
akrun

Reputation: 887721

Consider using an if/else expression with all i.e. we plot only if all the column names specified in plot are present or else return a blank plot

nm1 <- c("date", "Sessions", "Site Conversion Rate")
if(!all(nm1 %in% names(x))) {
     message("Not all columns are found")
     ggplot()
    } else {x %>% 
     ggplot(aes(date, Sessions)) +
     geom_col(na.rm = TRUE) +
     geom_line(aes(y = `Site Conversion Rate`), na.rm = TRUE)}

Or another option is possibly with specifying otherwise

library(purrr)
f1 <- function(x) {
  p1 <- x %>% 
      ggplot(aes(date, Sessions)) +
      geom_col(na.rm = TRUE) +
      geom_line(aes(y = `Site Conversion Rate`), na.rm = TRUE)
  print(p1)
 
 }
f1p <- possibly(f1, otherwise = ggplot())

-testing

f1p(x)

-output

enter image description here


Or a modification of the OP's tryCatch

tryCatch(expr = {print(x %>% 
    ggplot(aes(date, Sessions)) +
    geom_col(na.rm = T) +
    geom_line(aes(y = `Site Conversion Rate`), na.rm = TRUE))
}, 
error = function(e) {message(''); print(e)},
finally = {
       ggplot() + 
          theme_void()
})
<simpleError in FUN(X[[i]], ...): object 'Sessions' not found>

Upvotes: 3

Related Questions