ggvis
ggvis copied to clipboard
ggvis() / should accept NULL or empty data frame
It's difficult to integrate ggvis plots in shiny apps if at some point you don't have data to work on.
I would be nice if you could do:
reactive_data <- reactive({
if (is.null(input$x)) return(NULL) #because shiny has not yet set up the dynamic input x
...
if(nrow(df) == 0) return(NULL) # user has not yet selected any data to work on
df
})
reactive_data %>% ggvis(...) %>% ... %>%
bind_shiny()
For a NULL input ggvis could simply cancel the computation early and return(invisible()). Zero row data frames work for basic ggvis plots but fail e.g. for layer_density.
+1. In a reactive context:
Browse[1]> reactive({ h<-data.frame(x=c()); if (nrow(h) == 0) { return(NULL) }; h %>% ggvis }) %>% bind_shiny("foo")
gives the error
Error in UseMethod("as.vega", x) :
no applicable method for 'as.vega' applied to an object of class "NULL"
+1. I got around the problem by wrapping in observe()
but this is a less than perfect solution.
How did you mange to fix it with observe()? I tried different approaches but I always receive this error.
yep, how did you fix it with observe()? even i'd like to know.
+1. As a temporary hack solution, in cases of NULL data I just return a ggvis with something dumb like: data.frame(x = 0, y = 0) %>% ggvis(~x, ~y)