【问题标题】:How to populate input options for shiny using information from server如何使用来自服务器的信息填充闪亮的输入选项
【发布时间】:2019-07-29 06:03:14
【问题描述】:

我在闪亮的应用程序上使用过滤器来过滤数据框。目前,我必须手动将可能的值输入到 ui 中。有没有办法从服务器数据框中读取唯一值并使用该列表填充输入选项?我知道可以通过使表全局化来做到这一点,但我试图避免这样做。

示例代码:

ui <- fluidPage(
  sidebarPanel(
    selectInput(inputId = 'col1Input',
                label ='col1',
                choices = c(1,2,3))),
  mainPanel(
    DT::dataTableOutput("table")))
server <- function(input,output){
  df <- data.frame('col1' = c(1,2,3), 'col2' = c(1,2,3))
  output$table <- DT::renderDataTable(dplyr::filter(df, col1 == input$col1Input))
}

shinyApp(ui = ui, server = server)

谢谢!

【问题讨论】:

    标签: r shiny shinydashboard shiny-server shiny-reactivity


    【解决方案1】:

    您可以使用 renderUI 从服务器构建 UI。那么您可以简单地使用服务器中数据框中的一列来构建您的选择输入吗?

    See here 了解如何使用 renderUI。

    ui <- fluidPage(
      uiOutput("sidebarOutput"),
      uiOutput("mainPanel")
    )
    
    server <- function(input,output){
      df <- data.frame('col1' = c(1,2,3), 'col2' = c(1,2,3))
      output$sidebarOutput <- renderUI({
        sidebarPanel(
          selectInput(inputId = 'col1Input',
                      label =colnames(df[1]),
                      choices = df[[1]]))
      })
    
      output$mainPanel <- renderUI({
          output$table <- DT::renderDataTable({
              validate(
                need(input$col1Input != "", "No column selected")
              )
              dplyr::filter(df, col1 == input$col1Input)
            })
    
          mainPanel(
            DT::dataTableOutput("table")
          )
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    编辑: 您可能还想过滤您选择的特定列。你可以这样做:

    ui <- fluidPage(
      uiOutput("sidebarOutput"),
      uiOutput("mainPanel")
    )
    
    server <- function(input,output){
      df <- data.frame('col1' = c(1,2,3), 'col2' = c(4,5,6))
      output$sidebarOutput <- renderUI({
        sidebarPanel(
          selectInput(inputId = 'filtercolumn',
                      label = "Select a column to filter on",
                      choices = colnames(df)),
    
          renderUI({selectInput(inputId = 'valuetofilter',
                      label = paste0("filtering on column: ", colnames(df[input$filtercolumn])),
                      choices = df[[input$filtercolumn]])})
          )
      })
    
      output$mainPanel <- renderUI({
          output$table <- DT::renderDataTable({
              validate({
                need(input$filtercolumn != "", "No column selected")
                need(input$valuetofilter != "", "No value selected")
              })
    
              dplyr::filter(df,(!!as.name(input$filtercolumn)) == !!input$valuetofilter) #note the unquoting
            })
    
          mainPanel(
            DT::dataTableOutput("table")
          )
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    感谢this的回答

    【讨论】:

    • hm 我可以看到它是如何工作的。我会使用它将值发送到 UI 选择输入,然后返回到服务器以过滤数据吗?
    • 我根据您的代码添加了一个示例。所有这一切实际上是将数据帧从全局的东西移动到 renderUI 块中。让我知道您是否是这个意思?
    猜你喜欢
    • 2015-10-22
    • 2020-03-19
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    相关资源
    最近更新 更多