Skip to content

Instantly share code, notes, and snippets.

@daattali
Created March 5, 2020 17:15
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 daattali/76d2a7932dc5deaf4fb47d957019af2d to your computer and use it in GitHub Desktop.
Save daattali/76d2a7932dc5deaf4fb47d957019af2d to your computer and use it in GitHub Desktop.
using reactivity correctly
library(shiny)
library(ggplot2)
makePlot <- function(xvar, yvar) {
ggplot(iris, aes_string(xvar, yvar)) + geom_point()
}
ui <- fluidPage(
selectInput("xvar", "X variable", choices = names(iris), selected = names(iris)[1]),
selectInput("yvar", "Y variable", choices = names(iris), selected = names(iris)[2]),
plotOutput("plot")
)
server <- function(input, output, session) {
# This list holds all the previous values
prior <- reactiveValues(
xvar = "",
yvar = ""
)
# This will contain the current plot
plotObject <- reactiveVal()
# Main workhorse - when any inputs change, determine how to update the plot
observe({
replotAxesFlag <- FALSE
if (prior$xvar != input$xvar) {
prior$xvar <- input$xvar
replotAxesFlag <- TRUE
}
if (prior$yvar != input$yvar) {
prior$yvar <- input$yvar
replotAxesFlag <- TRUE
}
if (replotAxesFlag) {
newPlot <- makePlot(prior$xvar, prior$yvar)
plotObject(newPlot)
}
})
output$plot <- renderPlot({
plotObject()
})
}
shinyApp(ui, server)
library(shiny)
library(ggplot2)
makePlot <- function(xvar, yvar) {
ggplot(iris, aes_string(xvar, yvar)) + geom_point()
}
ui <- fluidPage(
selectInput("xvar", "X variable", choices = names(iris), selected = names(iris)[1]),
selectInput("yvar", "Y variable", choices = names(iris), selected = names(iris)[2]),
plotOutput("plot")
)
server <- function(input, output, session) {
plotObject <- reactive({
makePlot(input$xvar, input$yvar)
})
output$plot <- renderPlot({
plotObject()
})
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment