如何使用用户输入从SHINY中的我的环境中获取data.frame? [英] How to use user input to obtain a data.frame from my environment in Shiny?

查看:7
本文介绍了如何使用用户输入从SHINY中的我的环境中获取data.frame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨,我正在尝试弄清楚如何使用我生成的用户输入来调用R SHINY中的现有表。每个用户输入选项都是一个表的名称,我想使用此输入然后调用选定的表。

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

  input=reactive(input$location)
  output$table<-renderTable(input())

} 
但是,这只创建了一个新表,其中一行包含用户输入。

推荐答案

您可以使用get()获取data.frame。另外,请注意,input不是反应变量的好名称,因为它已经定义,因此我将其重命名为inputx。在这种情况下,您甚至可以不使用反应式,只需使用output$table<-renderTable({get(input$location)})

希望这能有所帮助!

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)

最干净的解决方案可能是将数据帧存储在列表中,并将该列表中的子集存储如下:

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)

这篇关于如何使用用户输入从SHINY中的我的环境中获取data.frame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆