闪亮的应用程序,用于输入多个字段

时间:2015-08-19 06:13:29

标签: r shiny

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

选择:#这是selectInput(所以一次只能选择一个) 选择1 选择2 选择3  一旦做出选择然后说选择1然后scree应该显示进一步的子选项,因为, 在选择1中选择: OP1 OP2 OP3 OP4

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

我无法做到这一点,我几天就尝试了这个,但只是设法完成了ui部分,但在服务器上没什么用.R代码

1 个答案:

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

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'))

      }
  })

})

这个结果出现在以下闪亮的应用程序中:

shiny app dynamic answer showhing and change

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

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

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

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