I have simplified my shiny app as follow:
library(shiny)
library(shinythemes)
library(shinyBS)
ui <- fluidPage(
theme = shinytheme("flatly"),
navbarPage("Demo",
tabPanel("Home",
column(2,
selectInput(inputId = "s1",label = "select project",choices = c("1", "2", "3")),
uiOutput("sensor")
))))
server <- function(input, output,session){
output$sensor <- renderUI({
s = c(as.numeric((input$s1))^2,as.numeric((input$s1))^3)
selectInput("t1",label ="Select",choices = ifelse(input$s1 == "1",c("x1","x2"),s ),multiple = FALSE)
})
}
I expect to get the x1
and x2
in second dropdown if my first selection is 1, otherwise either 4
and 8
or 9
and 27
should be the expected values in second dropdown.
But surprisingly, what I'm getting is just one value to select in second dropdown : if 1
I'm getting just x1
, if 2
I have just 4
and if 3
I have just 9
!!
why the selectInput
is not updating properly ?
From the online help for
ifelse
:ifelse returns a value with the same shape as test which is filled with elements selected from either yes or no depending on whether the element of test is TRUE or FALSE.
and
ifelse(test, yes, no)
In your case,
test
isinput$s1 == "1"
. In other words, a (character) vector of length 1. That's why you get only one value in your choices forinput$t1
.To get what you want, try something like this (untested code):
As an aside, you can get the same effect with
updateSelectInput
and no need forrenderUI
anduiOutput
.Edit
Here's the full code for the
uiOutput
versionand for the
updateSelectinput
versionPersonally, I prefer the latter as it seems cleaner.