rshinyggvis

Creating a scatter plot using ggvis in a shiny app


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

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.


Solution

  • There is just a very small mistake in your server code. You have to add "output$plot" instead of just "plot" when you are assigning the reactive. Here is the corrected code.

    library(ggvis)
    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
     output$plot <- reactive({
       dat()%>%
         ggvis::ggvis(~Total.x,~Total.y)%>%
         layer_points(fill:="red")%>%
         bind_shiny("plot")
       
     })
     
     
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)```