Skip to content

Instantly share code, notes, and snippets.

@ChrisBeeley
Created March 12, 2021 16:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisBeeley/93c1a0117aaf5e770e6175673c98337d to your computer and use it in GitHub Desktop.
Save ChrisBeeley/93c1a0117aaf5e770e6175673c98337d to your computer and use it in GitHub Desktop.
Demo of reactive data and UI
library(palmerpenguins)
library(tidyverse)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Reactive example"),
sidebarLayout(
sidebarPanel(
selectInput("species",
"Select penguin species",
choices = c("Adelie", "Chinstrap", "Gentoo")),
uiOutput("plotSizeUI")
),
mainPanel(
plotOutput("pengPlot")
)
)
)
server <- function(input, output) {
penguinData <- reactive({
penguins %>%
filter(species == input$species)
})
output$plotSizeUI <- renderUI({
min_bill_length <- min(penguinData()$bill_length_mm, na.rm = TRUE)
max_bill_length <- max(penguinData()$bill_length_mm, na.rm = TRUE)
sliderInput("billLength",
"Bill length range selector",
min = min_bill_length,
max = max_bill_length,
value = c(min_bill_length, max_bill_length))
})
output$pengPlot <- renderPlot({
penguinData() %>%
ggplot(aes(x = bill_length_mm, y = bill_depth_mm)) +
scale_x_continuous(
limits = c(input$billLength[1], input$billLength[2])) +
geom_point()
})
}
# 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