根据条件在 rshiny 中显示 selectInput (conditionalPanel)

Show selectInput in rshiny based on condition (conditionalPanel)

我想创建一个允许您 select 基于条件的变量的应用程序。 所以我创建了一个带有条件 Yes 和 No 的 switchInput,如您所见,如果标记 Yes,应该出现一个分层 SelectInput。

但是,没有显示新的 SelectInput:

# 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)

我明白了,基于conditionalPanel函数:

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.

switchInput 值的变化应该足以在 UI 界面中产生这种变化。

conditionalPanel() 的文档所述:

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

所以你需要在条件中使用input.Id013而不是Id013。此外,即使开关的标签是“是”或“否”,它 returns 也是一个值 TRUE/FALSE(在 Javascript 中写为“真”或“假”)。所以你需要使用的条件是:

condition = "input.Id013 == true"