【问题标题】:Shiny R - table output dependent on choice from dropdown menuShiny R - 表格输出取决于下拉菜单中的选择
【发布时间】:2017-06-01 04:56:28
【问题描述】:

我正在尝试添加一个下拉菜单来操作我的数据框。

例如,

假设我的 df 有列 - a1 a2 a3 b1 b2 c1 c2 c3

我想添加一个下拉菜单,其中包含选择 = list("All", "a", "b", "c") 将过滤表格以包含所有列、以 a 开头或以开头的列以此类推。

不知道该怎么做。可以创建新的 df 以在选择选项时调用,也可以根据选择操作 df。

这是一些示例代码(不是我的真实代码):

library(shiny)
shinyUI(fluidPage(
  titlePanel("x"),
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "x", 
                  choices = list("All", "a", "b", "c"))
    ),
    mainPanel(
      dataTableOutput('x')
    )
  )
))


#dfa <- data.frame(select(df, starts_with("a")))

#dfb <- data.frame(select(df, starts_with("b")))

#dfc <- data.frame(select(df, starts_with("c")))

shinyServer(function(input, output) {
  output$x = renderDataTable({

    ?????

    })
})

【问题讨论】:

    标签: r shiny shiny-server


    【解决方案1】:

    您可以使用反应式:

    shinyServer(function(input, output) {
    
        reactive_df <- reactive({
          if(input$x=="All")
            return df
          else
            return(select(df, starts_with(input$x)))
        }
    
        output$x <- renderDataTable(reactive_df())
    
    }
    

    【讨论】:

    • 感谢您的快速回复!很有帮助。
    猜你喜欢
    • 1970-01-01
    • 2021-08-12
    • 2016-09-24
    • 1970-01-01
    • 2020-08-02
    • 2021-05-27
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    相关资源
    最近更新 更多