Reputation: 1922
My inquiry feels close to this inqury, but the support provided is limited to the logic being applied to one data element.
I would like to learn the JavaScript to apply to a string/ vector/ array (not sure which language to use for my example) such that if any mammal were selected, the message would appear, but if any bird were select, the message would no appear.
library(shiny)
birds <- c("finch","robin","crow","duck")
mammals <- c("elephant", "human", "dog", 'cat')
ui <- fluidPage(
titlePanel("Select Conditional"),
mainPanel(
column(4,
selectizeInput(inputId = "animals",
label = "Select An Animal",
choices= list('Examples of Birds' = birds,
'Example of mammals' = mammals)
)),
column(8,
conditionalPanel(condition = "input.animals.indexOf('birds')",
textOutput("text")))
))
server <- function(input, output) {
output$text <- renderText({"This is a mammal."})
}
shinyApp(ui = ui, server = server)
I have attempted
conditionalPanel(condition = "input.animals.Array.indexOf('birds')",
conditionalPanel(condition = "input.animals.str.indexOf('birds')"
conditionalPanel(condition = "input.animals.Vector.indexOf('birds')"
conditionalPanel(condition = "input.animals.String.indexOf('birds')"
Thanks for any ideas. Please excused my lack of knowledge about vectors, arrays, strings.
Upvotes: 1
Views: 696
Reputation: 186
Non-input variables in R doesn't get passed to JavaScript, as a workaround, I built the character vector into a JS array and passed that inside the condition.
So, the actual condition passed in this case is ['elephant', 'human', 'dog', 'cat'].includes(input.animals)
which does what you wanted.
library(shiny)
birds <- c("finch","robin","crow","duck")
mammals <- c("elephant", "human", "dog", 'cat')
ui <- fluidPage(
titlePanel("Select Conditional"),
mainPanel(
column(4,
selectizeInput(inputId = "animals",
label = "Select An Animal",
choices= list('Examples of Birds' = birds,
'Example of mammals' = mammals)
)),
column(8,
conditionalPanel(condition = paste0(paste0("[",toString(paste0("'",mammals,"'")),"]"),".includes(input.animals)"),
textOutput("text")))
))
server <- function(input, output) {
output$text <- renderText({"This is a mammal."})
}
shinyApp(ui = ui, server = server)
Upvotes: 1