'R Shiny - conditionnal panel based on list
Yes, there are many questions phrased very similary, for instance
r shiny - conditionalPanel with condition: check item in list or Multiple conditions in condition panel R shiny
None, however, seems to sort the problem - that in addition seems to be one of js syntax more than R proper.
Consider the following MRE:
library(shiny)
collective <- c("happiness","love","peace","time","foo","bar")
numerable <- c("apple","orange","banana","quux","baz","bob")
ui <- fluidPage(
selectInput("choice","choose one",choices=c(collective,numerable)),
conditionalPanel(
condition = TRUE, ## This is the condition I need to write !
numericInput("number",
"How many?",
value=3)
),
textOutput("result")
)
server <- function(input, output, session) {
output$result <- renderText({
if(is.null(input$number)){
paste("You want some", input$choice)
}else{
paste("you want",input$number,input$choice)
}
})
}
shinyApp(ui = ui, server = server)
Here I want to show the number box only if the user choices something that can be counted.
In plain R, I would write my condition as
if(input$choice %in% numerable) ... etc.
However the condition must be written in js at that point.
The question is therefore, how do I translate this R statement into a js condition that will work in this context ?
Similar questions got the following answers, or perhaps workarounds:
1) Use a renderUI instead, which can be coded in R like so:
output$numberUI <- renderUI({
if( input$choice %in% numerable) {
# pass
}else{
numericInput("number",
"How many?",
value=3)
}
})
and replace the conditional panel by uiOutput("numberUI"). This works indeed, in actual fact this is what I'm doing now in my "real" app, but I find the resulting code to be less readable with the UI definition scattered between UI and server.
2) Explicitly list all the possible cases with
condition = "input.choice == 'apple' || input.choice =='orange' || input.choice == 'banana' " ... etc., which is obviously clumsy and error prone (and in addition will not work if the list of numerable is data-driven, in my case it is in the real app based on names(the_data) ).
I'm pretty sure that js has an operator equivalent to R's %in% so the question boils down to how do I translate this R statement: input$choice %in% numerable to js ? The obvious "input.choice in numerable" does not work, presumably because I first need to convert numerable to a js object ?
EDIT: based on Stéphane Laurent's below. I realized that of course condition is a string, so nothing stops me from creating the condition string dynamically. And so:
condition = paste('[',paste(paste('"',numerable,'"',sep=''),collapse=','),'].includes(input.choice)',sep='')
... if somewhat unreadable (it can be prettified with pipes, glue etc) does the job.
Solution 1:[1]
Try
condition = '["apple","orange","banana","quux","baz","bob"].includes(input.choice)'
or
condition = '["apple","orange","banana","quux","baz","bob"].indexOf(input.choice) > -1'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Stéphane Laurent |
