Home > OS >  Show selectInput in rshiny based on condition (conditionalPanel)
Show selectInput in rshiny based on condition (conditionalPanel)

Time:02-21

I want to create an app that allows you to select one variable based on a condition. So I have create a switchInput with conditions Yes, and No, and as you can see, a stratify SelectInput should appear in case Yes is marked.

However, no new SelectInput is displayed:

# Shiny
library(shiny)
library(shinyWidgets)
library(shinyjqui)

# Data
library(readxl)
library(dplyr)

# Plots
library(ggplot2)



not_sel <- "Not Selected"


# User interface

ui <- navbarPage(
  main_page <- tabPanel(
  title = "",
  titlePanel(""),
  sidebarLayout(
    sidebarPanel(
      title = "Inputs",
      fileInput("xlsx_input", "Select XLSX file to import", accept = c(".xlsx")),
      selectInput("num_var_1", "Variable X axis", choices = c(not_sel)),
      selectInput("num_var_2", "Variable Y axis", choices = c(not_sel)),
      switchInput(
        inputId = "Id013",
        onLabel = "Yes",
        offLabel = "No"
      ),
      conditionalPanel(
        condition = "Id013 == 'Yes'", selectInput("Stratify", "Stratify", choices = c(not_sel)), #uiOutput("factor"),
      ),
      actionButton("run_button", "Run Analysis", icon = icon("play"))
    ),
    mainPanel(
      tabsetPanel(
        tabPanel(
          title = "Plot",
          br(),
          plotOutput("sel_graph")
          )
        )
      )
    )
  )
)




# Server

server <- function(input, output){
  
  # Dynamic selection of the data. We allow the user to input the data that they want 
  data_input <- reactive({
    #req(input$xlsx_input)
    #inFile <- input$xlsx_input
    #read_excel(inFile$datapath, 1)
    iris
  })
  
}

# Connection for the shinyApp
shinyApp(ui = ui, server = server)

I understand, based on the conditionalPanel function:

Creates a panel that is visible or not, depending on the value of a JavaScript expression. The JS expression is evaluated once at startup and whenever Shiny detects a relevant change in input/output.

That the change on the switchInput value should be enough to generate this changes in the UI interface.

CodePudding user response:

As said in the docs of conditionalPanel():

For example, if you have an input with an id of foo, then you can use input.foo to read its value.

So you need to use input.Id013 instead of Id013 in the condition. Also, even if the labels of the switch are "Yes" or "No", it returns a value TRUE/FALSE (which are written "true" or "false" in Javascript). So the condition you need to use is:

condition = "input.Id013 == true"
  • Related