【问题标题】:Shiny: create a selectInput after choosing a value of another selectInputShiny:在选择另一个 selectInput 的值后创建一个 selectInput
【发布时间】:2017-10-19 21:21:06
【问题描述】:

我有一个闪亮的应用程序,我想在数据框上构建一个条件查询系统。

首先,我想要一个显示所有可用表的 selectInput。用户选择表后,我希望出现另一个框,他可以在其中选择要过滤的列名。这是我到目前为止所拥有的:

ui.r:

library(shiny)
library(shinydashboard)

source("global_variables.r")

ui=dashboardPage(

  dashboardHeader(title="Test"),
  dashboardSidebar(

    sidebarMenu(

      menuItem("Conditionals",tabName = "conditionals")

    )

  ),
  dashboardBody(

    tabItems(

      tabItem(tabName="conditionals",
              fluidRow(

                box(

                  title = "Conditionals",
                  width = 4,
                  selectInput("Con_tableName",choices=c("NONE",tableNames),label = "Table Name"),
                  tags$div(id = 'placeholder') 


                )

              )

      )

    )

  )

)

server.r:

library(shiny)

source("global_variables.r", local = FALSE)

Table1=data.frame();

shinyServer(
  function(input, output,session) {



    observe({
      reactive(
        if(input$Con_tableName!="NONE"){

          insertUI( selector="#placeholder",
                    ui={
                      selectInput("Con_colName",choices=c("NONE",colnames(dynGet(input$Con_tableName))),label = "Column Name")
                    }
          )
        }
      )
    })
  }
)

global_variables.r:

tableNames=c("Table1","Table2","Table3")

问题是,如果我在 selectInput 中选择一个值,observe 不会被触发。

编辑: 根据 BigDataScientists 的评论,将insertUI 更改为renderUI。更新文件:

ui.r:

library(shiny)
library(shinydashboard)

source("global_variables.r")

ui=dashboardPage(

  dashboardHeader(title="Test"),
  dashboardSidebar(

    sidebarMenu(

      menuItem("Conditionals",tabName = "conditionals")

    )

  ),
  dashboardBody(

    tabItems(

      tabItem(tabName="conditionals",
              fluidRow(

                box(

                  title = "Conditionals",
                  width = 4,
                  selectInput("Con_tableName",choices=c("NONE",tableNames),label = "Table Name"),
                  uiOutput("conditionalUI")


                )

              )
      )

    )

  )
)

server.r:

library(shiny)

source("global_variables.r", local = FALSE)

Table1=data.frame();

shinyServer(
  function(input, output,session) {



    observeEvent(input$Con_tableName,{
      reactive(
        if(input$Con_tableName!="NONE"){

          output$conditionalUI=renderUI({

           selectInput("Con_colName",choices=c("NONE",colnames(input$Con_tableName)),label = "Column Name")

          })

        }
      )
    })
  }
)

【问题讨论】:

  • 我会使用renderUI() 而不是insertUI(),因为您将在第一个 selectInput 更改时创建新元素......
  • 感谢您的快速回答!我改变了这个,但我仍然有同样的问题。该事件仍未触发
  • 你能分享编辑吗,...
  • 给你。再次感谢您的快速帮助!
  • 给我错误,对不起,...也许它适用于其他 smdy

标签: r shiny reactive


【解决方案1】:

您可以使用conditionalPanel()。下面有一个小例子可能适用于您的情况。

library(shiny)

shinyApp(
  ui <- fluidPage(
    mainPanel(
      selectInput("input1", "Select something", choices = c('','1','2','3')),
      conditionalPanel("input.input1!=''",
                       selectInput('input2', "Select something else", choices = c('4','5')))
    )
  ),
  server <- function(input, output){}
)

【讨论】:

  • 我已经有了同样的想法,但是我想支持多个条件。不只是 2-3。
  • 我不太明白你的评论。基本上,我编写的代码演示了一种在用户选择某些内容后生成 selectInput 的方法。显然,您需要根据自己的需要进行调整。如果您发布两个表格,我们将更容易创建一个完整的工作示例。
猜你喜欢
  • 2021-05-17
  • 2016-01-10
  • 2021-01-22
  • 2016-04-28
  • 1970-01-01
  • 2013-10-01
  • 1970-01-01
  • 2019-01-19
  • 2021-06-13
相关资源
最近更新 更多