# Shiny Conditional Panels and pickerInput

Shiny applications offer a fantastic way to produce interactive web applications. Unsurprisingly, there are abundant R packages built specifically for Shiny to facilitate ‘telling your data story’. For example, I often use the pickerInput() function from the shinyWidgets package, which is more aesthetic and has built-in ‘Select All/None’ buttons compared to the base checkboxGroupInput().

pickerInput(inputId = 'picker',
choices = c("Plot", "Text", "Analysis"),
multiple = T,
options = list(actions-box = TRUE))

This basic interface allows users to select any combination of selections. These inputs may be combined with the conditionalPanel() function. This allows an app’s users to change the interface structure based on their selected options from the pickerInput. In this example, you could have a specific panel appear depending on the selections. Given that Shiny Apps call upon different inputs and outputs for their panels and reactives, respectively (e.g., textInput -> textOutput, plotInput -> plotOutput), having certain panels appear depending on conditions is sometimes needed. We can have a plot appear when ‘Plot’ is selected and a written result when ‘Text’ is selected. For newby Shiny users, like myself, conditionalPanel() can be confusing because it uses JavaScript. Well, thanks to my friend Tan, here is a quick way to call a pickerInput selection into a conditionalPanel(). This would be placed the UI section of your app. Assume the pickerInput()‘s ID is ’picker1’ and:

conditionalPanel(condition = "input.picker1.includes('Plot')",
plotOutput(outputId = 'plot1'))

Using this, when someone clicks ‘Plot’, a panel for plot will appear.

Here is a full example using three panels:

knitr::include_app("https://tylerpritchard.shinyapps.io/conditional_panels/", height = "100%")
library(shiny)
library(shinyWidgets)
library(kableExtra)
choicespicker <- c("Plot", "Text", "Analysis")

ui <- fluidPage(

titlePanel("Conditional Panels!"),

sidebarLayout(
sidebarPanel(pickerInput(inputId = 'picker',
choices = choicespicker,
multiple = T,
options = list(actions-box = TRUE)),
),

mainPanel(
conditionalPanel(condition = "input.picker.includes('Plot')",
plotOutput("plot1")),
br(),
conditionalPanel(condition = "input.picker.includes('Text')",
textOutput("text1")),
br(),
conditionalPanel(condition = "input.picker.includes('Analysis')",
textOutput("analysis1"))
)
)
)

server <- function(input, output) {

x <- rnorm(100, 10)
y <- rnorm(100, 10)

output$plot1 <- renderPlot({ p1 <- plot(x, y) p1 }) output$text1 <- renderText({
paste("Wow, what a nice set of random variables! With X having a mean of ", mean(x), " and Y having a mean of ", mean(y), "! Their correlation is ", sep="")
})
output$analysis1 <- renderText({ r <- round(cor.test(x, y)$estimate, 2)
p <- round(cor.test(x, y)\$p.value, 3)
paste("The results suggest that X and Y are not unlikely given a true null hypothesis that they are unrelated, r = ", r, " p = ", p, sep="")

})
}

shinyApp(ui = ui, server = server)

I like conditionalPanel() because it doesn’t reserve the space for other panels. That is, when selecting ‘Analysis’ only in the above example, it doesn’t leave a blank area for the Plot and Text. Rather, it puts the panels at the highest position possible.

I hope you enjoyed this quick demonstration of conditionalPanel() using Shiny.

##### Tyler Pritchard
###### PhD Student, Sessional Professor, Researcher, and Clinician

My research interests include suicide and theory, research methods and statistics, and online activity.