1

Is it possible to ask in condition within conditionalPanel whether the input of a selectedInput belongs to a vector.

It is clear to me that condition is a JavaScript object and here is a similar problem here. However, my problem is a littel bit different. I made a simple example:

#  ----
library(shiny)
library(shinydashboard)

var array1 = ['a','c', 'f'];
# header ----
header <- dashboardHeader(title = "Example")

#sidebar ----
sidebar <- dashboardSidebar(disable = T)

#body ----
body <- dashboardBody(

  fluidRow(
    column(
      width = 12,
      selectInput(
        inputId = "control", 
        label = "choose something:",
        choices = c("a", 
                    "b", 
                    "c", 
                    "d", 
                    "e",
                    "f"),
        multiple = TRUE
      )
    )
  ),

  conditionalPanel(
    condition = "input.control.indexOf(array1) > -1",
    textInput(inputId = "first", label = "first test")
  )

)

# all ui ----
ui <- dashboardPage(
  header = header, 
  sidebar = sidebar, 
  body = body
)

# server ----
server = shinyServer(function(input, output) {


})
# Run the application 
shinyApp(ui = ui, server = server)

I defined an js-array

var array1 = ['a','c', 'f'];

however it dose not work. Any idea?

maniA
  • 1,437
  • 2
  • 21
  • 42

1 Answers1

0

I turned multiple to FASLE because I'm not sure what's the desired condition in your mind if there is overlap.

How it works is that you need to insert your javascript array in your UI first. Also, your javascript syntax in the condition is wrong, the correct one should be:array1.indexOf(input.control) > -1

The following example works.

#  ----
library(shiny)
library(shinydashboard)

# header ----
header <- dashboardHeader(title = "Example")

#sidebar ----
sidebar <- dashboardSidebar(disable = T)

#body ----
body <- dashboardBody(
  # insert javascript code in UI -----------------------
  tags$head(
    tags$script("var array1 = ['a','c', 'f'];")
  ),

  fluidRow(
    column(
      width = 12,
      selectInput(
        inputId = "control", 
        label = "choose something:",
        choices = c("a", 
                    "b", 
                    "c", 
                    "d", 
                    "e",
                    "f"),
        multiple = FALSE
      )
    )
  ),

  conditionalPanel(
    condition = "array1.indexOf(input.control) > -1", # change code here
    textInput(inputId = "first", label = "first test")
  )

)

# all ui ----
ui <- dashboardPage(
  header = header, 
  sidebar = sidebar, 
  body = body
)

# server ----
server = shinyServer(function(input, output) {

})
# Run the application 
shinyApp(ui = ui, server = server)
yusuzech
  • 5,896
  • 1
  • 18
  • 33