Skip to content

Instantly share code, notes, and snippets.

@laderast
Last active December 14, 2021 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laderast/952120ac46d1f27c2d2dba5bd1ab5d10 to your computer and use it in GitHub Desktop.
Save laderast/952120ac46d1f27c2d2dba5bd1ab5d10 to your computer and use it in GitHub Desktop.
Tidyeval/Shiny example 2: passing multiple inputs using syms()
# Shiny App showing tidyeval with multiple variables
library(shiny)
data(iris)
library(dplyr)
numericVariables <- c("Sepal.Length", "Sepal.Width","Petal.Length", "Petal.Width")
# Define UI for application that draws a histogram
ui <- fluidPage(
title= "Passing a List of Variables",
# Show a plot of the generated distribution
fluidRow(
selectInput("numeric_vars", "Select a Numeric Variable",
choices = numericVariables, selected = numericVariables[1:2],
multiple = TRUE),
plotOutput("boxplot")
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
selected_data <- reactive({
## input$numeric_var is a character vector, so we cast it to a list of symbols
var_list <- syms(input$numeric_vars)
## Now we evaluate it with !!!
out_col <- iris %>% select(!!!var_list)
})
output$boxplot <- renderPlot({
boxplot(selected_data())
})
}
# Run the application
shinyApp(ui = ui, server = server)
@BeijingMarathon
Copy link

My friend, you helped me. Hadley suggested method in Master Shiny using any_of() does not work surprisingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment