闪亮的R - 表输出取决于下拉菜单中的选择

时间:2017-05-31 20:48:18

标签: r shiny shiny-server

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

例如,

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

我想添加一个下拉菜单,其中包含choices = list(" All"," a"," b"," c&#34 ;)将过滤表以包括所有列,以a开头或以b开头的列等等。

不知道如何解决这个问题。打开以创建新的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({

    ?????

    })
})

1 个答案:

答案 0 :(得分: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())

}
相关问题