Shiny - RStudio: conditionalPanel does not work when the condition is .Platform$OS.type

Viewed 26

Shiny show everything even the condition does not meet, for example .Platform$OS.type == 'windowsxxx'. Since I use window, so the command .Platform$OS.type will return "windows" and the condition .Platform$OS.type == 'windowsxxx' should be FALSE,but shiny seems does not evaluate this condition and shows everything inside that conditionalPanel. The example code is below, every suggestion is highly appreciated

    library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30),
            
            conditionalPanel(
              condition = ".Platform$OS.type == 'windowsxxx'",
              sliderInput("breakCount", "This is a conditional sliderInput", min=1, max=1000, value=10)
            )
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

}

# Run the application 
shinyApp(ui = ui, server = server)
1 Answers

If you pass .Platform$OS.type as a string to conditionalPanel's condition parameter, which expects a JavaScript expression - it won't get executed by R. Please check the following:

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30),
      
      conditionalPanel(
        condition = sprintf("'%s' == 'windowsxxx'", .Platform$OS.type),
        sliderInput("breakCount", "This is a conditional sliderInput", min=1, max=1000, value=10)
      )
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
}

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