代码之家  ›  专栏  ›  技术社区  ›  RLave

在列表中动态选择元素并可渲染

  •  1
  • RLave  · 技术社区  · 7 年前

    我有一张单子 l 属于 data.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

    1 回复  |  直到 7 年前
        1
  •  1
  •   Wilmar van Ommeren    7 年前

    renderTable 具有 tableOutput ui . 所以改变 dataTableOutput('table') tableOutput('table') .

    你可以用 dataTableOutput renderDataTable 不过。

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