更新renderUI输出

时间:2018-05-07 12:48:55

标签: r shiny

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

编辑:只是为了澄清:我想更改textInput的标签而不更新radioButtons。第二个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)

提前致谢!

1 个答案:

答案 0 :(得分:0)

您可以尝试使用类似的内容向服务器代码添加observeEvent

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

希望这有帮助!