Last active
December 14, 2021 12:05
-
-
Save laderast/952120ac46d1f27c2d2dba5bd1ab5d10 to your computer and use it in GitHub Desktop.
Tidyeval/Shiny example 2: passing multiple inputs using syms()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My friend, you helped me. Hadley suggested method in Master Shiny using any_of() does not work surprisingly.