【问题标题】:Updating some but not all elements inside renderUI output更新 renderUI 输出中的一些但不是所有元素
【发布时间】:2018-05-08 22:54:59
【问题描述】:

我正在尝试通过使用 renderUI 在输出中渲染它来构建交互式 UI。问题是:我在这个渲染函数中创建了输入,其行为应该根据提供的答案而改变。但是当我这样做时,反应性会更新整个输出并删除提供的答案,将输入重置为原始状态。有没有办法确定我要更新哪些输入?或者有没有更好的方法来构建这个结构?

编辑:澄清一下:我想更改 textInput 的标签而不更新单选按钮。第二个 radioButton 答案应该只影响 textInput 的行为。

ui <- miniPage(
  miniTabstripPanel(id = 'tabs',
    miniTabPanel("Data",
                 miniContentPanel(
                   selectInput(inputId = 'indicator', label = "Select indicator:",
                               choices = c('Select an indicator' = 'none',
                                           "Water" = 'iwater',
                                           'Antenatal care 4+ visits' = 'anc4',
                                           'Institutional delivery' = 'ideliv')),
                 )
    ),
    miniTabPanel("Second tab",
       miniContentPanel(
          uiOutput(outputId = "indicarea")  
       )
    )
  )
)

server <- function(input, output, session) {

  iwater_vartype = reactiveVal(value= "Example label 1")
  observeEvent(input$iwater_variabletype,{
    if (input$iwater_variabletype == 'codes') {
      iwater_vartype("Example label 1")
    }
    else {
      iwater_vartype("Example label 2")
    }
  })

  observeEvent(input$indicator,{
    output$indicarea = renderUI({
      buildUI(input$indicator)
    })
  })

  buildUI = function(indic) {
    switch(indic, 
           'none' = {
             h3("Please select an indicator to proceed.")
           },
           'iwater' = {
               tagList(
                  h3("Improved source of drinking water"),
                  br(), hr(), br(),
                  radioButtons(inputId = 'iwater_subsample', label = "Asked it in all?",
                              choices = c('Yes' = 'yes', 'No' = 'no')),
                  radioButtons(inputId = 'iwater_variabletype', label = "How was the info collected?",
                               choices = c('One variable' = 'codes', 'Several variables' = 'variables')),
                  textInput(inputId = 'iwater_sourcevariable', label= iwater_vartype())
               )
           },
           'anc4' = {
               tagList(
                  textInput(inputId = 'test', label= 'testing')
             )
           }
      )
  }
}

runGadget(ui, server)

提前致谢!

【问题讨论】:

  • 如果您想要不同的行为,请将renderUI 功能分开:一个用于按钮,一个用于文本输入
  • 我不确定我是否可以,因为它们是动态的,并且连接到输入的单选按钮的数量可能会有所不同。我想像流程图一样复制这种行为

标签: r shiny


【解决方案1】:

您可以尝试使用类似这样的方式将observeEvent 添加到您的服务器代码中。

observeEvent(input$iwater_variabletype,{
    updateTextInput(
        session = session,
        inputId = "iwater_sourcevariable",
        label = "new Label for text Input"
      )
    })

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 2023-04-01
    • 2015-12-19
    • 2021-12-16
    相关资源
    最近更新 更多