Creating scatter plot using ggvis in a Shiny App

I am trying to create a simple app that does the following:

  • Import a csv file as a reactive (this has to be a reactive)
  • Print the contents of csv as data table in one tabPanel
  • Create an interactive scatter plot using ggvis in another tabPanel

However I am unable to create the plot- the tabPanel where plot is supposed to appear is blank. There is no error or warning messages in the console whatsoever. Not sure what is wrong with the code.

Here is the code:

# Define UI for application 
ui <- fluidPage(

    # Application title
    titlePanel("App"),

    sidebarLayout(
        sidebarPanel(
            fileInput(inputId = "inputCrossTab"
                      , label = "Select the input sheet"
                      , multiple = FALSE
                      , accept = c('.csv')
                      )
        ),

        # Show a table and plot 
        mainPanel(
           tabsetPanel(id = "tabset"
                       , type = 'pills'
                       , tabPanel(title = "Table"
                                  , DT::dataTableOutput('table')
                                  )
                       , tabPanel(title = "Charts"
                                  ,ggvisOutput("plot")
                                  )
               
           )
        )
    )
)

# Define server logic required 
server <- function(input, output) {
    
    # Import csv data as a reactive
    dat <- reactive({
        
        req(input$inputCrossTab$datapath)
        dat_tab <- data.table::fread(  file   = input$inputCrossTab$datapath
                                     , header = TRUE)
        
    })
    
    # Render imported data as table
    output$table <- DT::renderDataTable({
        dat()
    })
    
    # Plot the table as a scatter plot
    plot <- reactive({
        dat()%>%
            ggvis::ggvis(~Total.x,~Total.y)%>%
            layer_points(fill:="red")%>%
            bind_shiny("plot")

    })
    
    
}

# Run the application 
shinyApp(ui = ui, server = server)

Here is the csv file I am importing (It's just a file with 5 rows and 3 columns...some random made up data)..

X1,Total.x,Total.y
ncksncnxzc,0.8338719625,0.0163952762
xsmkslaxmkaslx,0.5867098735,0.2033673932
njasdnsa,0.3586965341,0.8281010715
sadlasdl;,0.060212096,0.1735624054
nsakksad,0.7281606887,0.3851430044

One more thing, here i am importing a csv for the sake of reproducible example. In my actual code I am importing a csv file and doing some transformation and creating a reactive data table like the given csv data.

Please help me solve this.

Thanks,
Anoop

First note, the standard method to render plots in shiny is writing the function renderFoo() to output$bar (see this documentation for a walkthrough). In the code you provided you're writing the ggvis object to a reactive.

Second, It appears that the ggvis rendering in Shiny is non-standard (based on this documentation) and I believe using observe() instead of a standard renderFoo() is the correct way to implement the ggvis into your Shiny app.

  # Plot the table as a scatter plot
  observe({
    dat()%>%
      ggvis::ggvis(~Total.x,~Total.y)%>%
      layer_points(fill:="red")%>%
      bind_shiny("plot")
  })