【问题标题】:shiny app for a taking input of multiple fields用于输入多个字段的闪亮应用程序
【发布时间】:2015-11-12 07:07:00
【问题描述】:

我正在尝试构建这样的应用程序:-

Choose: # 这是 selectInput(所以一次只能选择一个) 选择 1 选择 2 选择 3 一旦做出选择然后说选择1,那么scree应该显示更多的子选项, 在选择 1 中选择: op1 op2 op3 op4

这些是多个复选框,用户可以勾选任意数量的复选框。 在此之后,将显示相关的输出。 并且我希望应用程序在输入更改时反映更改。

我不知道这样做,我从几天开始就在尝试这个,但只成功完成了 ui 部分,但在 server.R 代码上没有太多工作

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    希望这会对你有所帮助。

    UI.R 文件

    library(shinyBS)
    library(shiny)
    
    shinyUI(fluidPage(
    
    # input control for first choice
    
    selectInput("first_choice", 
                  label = h1("First Answer a General Question"),
                  choices = list("select","A","B","C"),
                selected = "select"
                  ),
    
    #collapsable panel for second choice
    
    h1("then get into details"),
    
    bsCollapse(
    bsCollapsePanel( title = "details",
                     uiOutput("second_choice")
                     ),
    id = "collapser", multiple = FALSE, open = NULL
    ),
    h2("first answer"),
    h3(textOutput("first_answer")),
    h2("second answer"),
    h3(textOutput("second_answer"))
    
    ))
    

    server.R 文件

    library(shiny)
    
    shinyServer(function(input, output,session) {
    
      #retrieve selected values and render text from selection
    
      output$first_answer  <- renderText({input$first_choice})
      output$second_answer <- renderText({input$dynamic})
      output$second_choice <- renderUI({
    
        switch(input$first_choice,
               "A" = checkboxGroupInput("dynamic", "Dynamic",
                                       choices = c("Aragon","Frodo"),
                                       selected = "option2"),
               "B" = checkboxGroupInput("dynamic", "Dynamic",
                                        choices = c("Bilbo","Gandalf","Sauron"),
                                        selected = "option2"),
               "C" = checkboxGroupInput("dynamic", "Dynamic",
                                        choices = c("Boromir","Legolas"),
                                        selected = "option2")
    
               )
    
      })
    
      #observe function in order to open the collapsable panel when the first answer is given
    
    
      observe({
        if (input$first_choice != "select") {
          updateCollapse(session,"collapser",open = c('details'))
    
          }
      })
    
    })
    

    这会产生以下闪亮的应用程序:

    第二个菜单仅在第一个得到答案后打开,第二个问题的选项根据第一个答案动态更改。 选定的答案分配给 first_answersecond_answer

    请注意折叠面板使用 shinyBS 包。

    您可以在以下 Rstudio 资源中找到有关动态 UI 更改的更多信息: http://shiny.rstudio.com/articles/dynamic-ui.html

    http://shiny.rstudio.com/gallery/dynamic-ui.html

    【讨论】:

    • 谢谢!,实际上问题在于每个第一选择,第二选择的数量各不相同。在这里,您为每个第一选择分配了 3 个选择,但我想要 4 个百分比为白色,2 个为黑色百分比等等.....有可能吗??!!
    • 这应该是你要找的。请注意,动态更改是通过 renderUI() 函数和 switch() 函数实现的。在引用的资料和 R 文档中了解有关这些功能的更多信息。
    猜你喜欢
    • 2016-02-21
    • 2016-09-09
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    相关资源
    最近更新 更多