ggvis icon indicating copy to clipboard operation
ggvis copied to clipboard

ggvis() / should accept NULL or empty data frame

Open ThomasSiegmund opened this issue 10 years ago • 5 comments

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.

ThomasSiegmund avatar Dec 07 '14 12:12 ThomasSiegmund

+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"

alexbbrown avatar Jan 16 '15 19:01 alexbbrown

+1. I got around the problem by wrapping in observe() but this is a less than perfect solution.

codegordi avatar Jan 20 '15 04:01 codegordi

How did you mange to fix it with observe()? I tried different approaches but I always receive this error.

boris4o avatar May 24 '15 17:05 boris4o

yep, how did you fix it with observe()? even i'd like to know.

engti avatar Jun 14 '15 15:06 engti

+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)

ALShum avatar Jul 13 '15 14:07 ALShum