How to use user input to obtain a data.frame from my environment in Shiny?

后端 未结 1 1594
执念已碎
执念已碎 2020-12-11 10:36

Hi i am trying to figure out how to use the user input i generate to call an existing table in R shiny. Each user input options is the name of a table, i want to use this in

相关标签:
1条回答
  • 2020-12-11 10:59

    You could use get() to obtain the data.frame. Also, note that input is not a good name for a reactive variable, since it is already defined, therefore I renamed in to inputx. In this case, you could even do without the reactive and simply use output$table<-renderTable({get(input$location)})

    Hope this helps!

    Dublin=Head(mtcars,5)
    Cork=head(mtcars,10)
    Galway=head(mtcars,15)
    Belfast=head(mtcars,2)
    
    ui=fluidPage( 
      selectInput(inputId="location",label="Please Choose location", 
                  choices=c("Dublin"="Dublin","Cork"="Cork","Galway"="Galway","Belfast"="Belfast")), 
      tableOutput("table") ) 
    
    server=function(input, output){
    
      inputx=reactive({get(input$location)}) 
      output$table<-renderTable(inputx())
    
    }
    shinyApp(ui,server)
    

    The cleanest solution would probably be to store your dataframes in a list, and subset from that list as follows:

    Dublin=Head(mtcars,5)
    Cork=head(mtcars,10)
    Galway=head(mtcars,15)
    Belfast=head(mtcars,2)
    
    mylist = list(Dublin=Dublin,Cork=Cork,Galway=Galway,Belfast=Belfast)
    
    ui=fluidPage( 
      selectInput(inputId="location",label="Please Choose location", 
                  choices=c("Dublin"="Dublin","Cork"="Cork","Galway"="Galway","Belfast"="Belfast")), 
      tableOutput("table") ) 
    
    server=function(input, output){
      output$table<-renderTable(mylist[input$location])
    }
    shinyApp(ui,server)
    
    0 讨论(0)
提交回复
热议问题