Created
March 5, 2020 17:15
-
-
Save daattali/76d2a7932dc5deaf4fb47d957019af2d to your computer and use it in GitHub Desktop.
using reactivity correctly
This file contains hidden or 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
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) |
This file contains hidden or 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
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