【问题标题】:Shiny dynamically select element in list and renderTable闪亮的动态选择列表和渲染表中的元素
【发布时间】:2018-12-05 10:07:39
【问题描述】:

我有一个ldata.frames 列表,我想用selectInput 动态选择元素。

一旦用户选择了一个元素,我希望能够在后台使用它,例如在renderTable 中使用它并输出该data.frame 的表格。

此外,我希望能够在列表中选择该元素,并应用 lm 例如。

我面临的问题在这里:l[[ElemInput()]] 如何将这个经典的R 代码与反应元素转换?

这里是完整的shinydashboard

library(shiny)
library(shinydashboard)


ui <- dashboardPage(
  dashboardHeader(
    title = "My Dashboard"
  ),
  dashboardSidebar(
    sidebarMenu(
      menuItem("aaa", tabName = "aaa", icon = icon("dashboard"))
    )
  ),
  dashboardBody(
    tabItems(
      # First tab content
      tabItem(tabName = "aaa",
              h2("Dashboard aaa"),
              fluidRow(
                box(
                    selectInput("input_name", "Select input:", choices = names(l))
                ),
                box(
                  dataTableOutput('table')
                )
              )
      )
    )
  )
)

server <- function(input, output) { 
  # create a fake list 
  set.seed(123)
  l <- list(element1 = data.frame(aaa=1:5, bbb=sample(5)),
            element2 = data.frame(ccc=6:10, ddd=sample(5)))

  # reactive read element
  ElemInput <- reactive({
    input$input_name
  })

  # render table
    output$table <- renderTable({
      l[[ElemInput()]] #  this part doesn't work
    })

}

shinyApp(ui, server)

可能相关的问题,但我无法为我的案例找到解决方案:

dynamically list 'choices' for selectInput from a user selected column

Store selectInput in a string/character object

【问题讨论】:

    标签: r list shiny shinydashboard


    【解决方案1】:

    您应该在您的ui 中使用renderTabletableOutput。所以将dataTableOutput('table') 更改为tableOutput('table')

    如果你使用renderDataTable,你可以使用dataTableOutput

    另外我更喜欢使用观察者而不是反应函数。它更容易阅读。

    server <- function(input, output) { 
      observeEvent(input$input_name,{
        # observe element change and render table
        output$table <- renderTable({
          data.frame(l[[input$input_name]])
        })
      })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      • 2014-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-11
      • 2018-03-02
      相关资源
      最近更新 更多