Created
April 16, 2018 18:34
-
-
Save jcheng5/45813fd5b4ae6b418cc8a081e8d21830 to your computer and use it in GitHub Desktop.
freezeReactiveValue examples
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
library(shiny) | |
library(ggplot2) | |
ui <- fluidPage( | |
sidebarLayout( | |
sidebarPanel( | |
selectInput("dataset", "Dataset", c("cars", "pressure", "mtcars")), | |
uiOutput("input_ui") | |
), | |
mainPanel( | |
plotOutput("plot") | |
) | |
) | |
) | |
server <- function(input, output, session) { | |
dataset <- reactive({ | |
get(input$dataset, "package:datasets") | |
}) | |
output$input_ui <- renderUI({ | |
freezeReactiveValue(input, "x") | |
freezeReactiveValue(input, "y") | |
columns <- names(dataset()) | |
tagList( | |
selectInput("x", "x variable", columns, columns[[1]]), | |
selectInput("y", "y variable", columns, columns[[2]]) | |
) | |
}) | |
output$plot <- renderPlot({ | |
Sys.sleep(1) | |
ggplot(dataset(), aes_string(input$x, input$y)) + geom_point() | |
}) | |
} | |
shinyApp(ui, server) |
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
library(shiny) | |
library(ggplot2) | |
ui <- fluidPage( | |
sidebarLayout( | |
sidebarPanel( | |
selectInput("dataset", "Dataset", c("cars", "pressure", "mtcars")), | |
selectInput("x", "x variable", character(0)), | |
selectInput("y", "y variable", character(0)) | |
), | |
mainPanel( | |
plotOutput("plot") | |
) | |
) | |
) | |
server <- function(input, output, session) { | |
dataset <- reactive({ | |
get(input$dataset, "package:datasets") | |
}) | |
observe({ | |
freezeReactiveValue(input, "x") | |
freezeReactiveValue(input, "y") | |
columns <- names(dataset()) | |
updateSelectInput(session, "x", choices = columns, selected = columns[[1]]) | |
updateSelectInput(session, "y", choices = columns, selected = columns[[2]]) | |
}) | |
output$plot <- renderPlot({ | |
Sys.sleep(1) | |
ggplot(dataset(), aes_string(input$x, input$y)) + geom_point() | |
}) | |
} | |
shinyApp(ui, server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment