Search code examples
rshinyggvis

R ggvis error "No data supplied to mark"


I am struggling to incorporate a ggvis code into my shiny app. I get an error the description of which I cannot find on the web. The error is:

Error : No data supplied to mark.

Could please someone indicate what I am doing wrong? Thanks!

ui.R:

shinyUI(pageWithSidebar(
  headerPanel("test"),
  sidebarPanel(
    fileInput("INPUT","Upload your .xls:")
  ),
  mainPanel(
    ggvisOutput("PLOT")
  )
))

server.R:

library(ggvis)

shinyServer(function(input, output, session) {

  PLOTDF<-reactive({
    if (is.null(input$INPUT)==F) {

      library(gdata)
      tax<-read.xls(input$INPUT$datapath,check.names=F)

      plotdf<-data.frame(c(tax[1,1],tax[1,2]),c(tax[2,1],tax[2,2]))
      colnames(plotdf)<-c("a","b")

      plotdf

    }
  })

  reactive({
    plotdf_read<-PLOTDF()
    plotdf_read$data %>% ggvis(x=~a,y=~b) %>% layer_points()
    }) %>% bind_shiny("PLOT")

})

Solution

  • I'm not familiar with gdata but I have a few suspicions on the error. It looks like your PLOTDF reactive function will return a dataframe plotdf. I'm not sure why you have plotdf_read$data instead of plotdf_read in your plot statement. plotdf_read$data is trying to get the "data" column from the dataframe -- it looks like the dataframe only has columns "a" and "b". I think you mean to use plotdf_read %>% ggvis....

    Also here's a bit of a trick when it comes to passing in reactives into ggvis. Right now this you are doing something similar to this:

    reactive({
      PLOTDF() %>% ggvis(x=~a,y=~b) %>% layer_points()
      }) %>% bind_shiny("PLOT")
    

    Basically, you are getting a data.frame in a reactive context and passing it into ggvis. This will certainly work but I think there is a better way.

    ggvis was designed to accept "bare reactives" -- we can pass into ggvis the reactive itself. In the below code segment notice I pass PLOTDF into ggvis and NOT PLOTDF():

    PLOTDF %>% ggvis %>% ggvis(x=~a,y=~b) %>% layer_points()
    %>% bind_shiny("PLOT")
    

    What is the difference? Well in the first example, every time your data.frame changes you will replot the entire ggvis. In the second example, every time your data.frame changes only the data points in the ggvis will replot.

    EDIT: improved grammar and explanations.