问题
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
回答1:
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")
})
}
)
来源:https://stackoverflow.com/questions/43389314/have-fields-of-an-input-depend-other-inputs