Creating a scatter plot using ggvis in a shiny app

最后都变了- 提交于 2021-02-10 20:11:45

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/64256371/creating-a-scatter-plot-using-ggvis-in-a-shiny-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!