hartmut hartmut - 1 month ago 9
R Question

have fields of an input depend other inputs

I have 2 inputs, say A and B.

I would like the "value" field of B to depend on the value inputted by the user for A.

I.e., my suggestion for B depends on what I learnt in A.

The following code does not work. How should I fix it please?

shinyApp(
ui = fluidPage(
textInput("A", "Enter a string"),
textInput("B", "Enter another string", value = "Second"),
textOutput("curval")
),
server = function(input, output) {
if (input$A == "foo"){input$B$value <- "bar"}
}
)


This issue is also raised here. But it has not been answered (although the comments are definitely helpful).

thank you

Answer

You should use uiOutput and renderUI to generate widgets which depends of the input :

shinyApp(
  ui = fluidPage(
    textInput("A", "Enter a string"),
    uiOutput("B_ui"),
    textOutput("curval")
  ),
  server = function(input, output) {
    output$B_ui <- renderUI({
      if (input$A=="foo") textInput("B","Enter another string",value="bar")
      else textInput("B","Enter another string",value="Second")
    })
  }
)

Maybe there is little mistakes I can't install shiny on my working computer.