Skip to content

Instantly share code, notes, and snippets.

@laderast
Created August 7, 2018 21:59
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/a5205554324306e642b2df9f80ed6409 to your computer and use it in GitHub Desktop.
Save laderast/a5205554324306e642b2df9f80ed6409 to your computer and use it in GitHub Desktop.
Using Tidyeval with Shiny - selectInput
# Code example to show how to use tidyeval within Shiny
library(shiny)
data(iris)
library(dplyr)
numericVariables <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
ui <- fluidPage(
title= "Passing an input value into the select statement",
# Show a plot of the generated distribution
fluidRow(
selectInput("numeric_var", "Select a Numeric Variable",
choices = numericVariables, selected = numericVariables[1]),
plotOutput("histogram")
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
selected_data <- reactive({
## input$numeric_var is a character, so we cast it to symbol
var_name <- sym(input$numeric_var)
## Now we evaluate it with !!
out_col <- iris %>% pull(!!var_name)
})
output$histogram <- renderPlot({
hist(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