Skip to content

Instantly share code, notes, and snippets.

@jcheng5
Created April 16, 2018 18:34
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 jcheng5/45813fd5b4ae6b418cc8a081e8d21830 to your computer and use it in GitHub Desktop.
Save jcheng5/45813fd5b4ae6b418cc8a081e8d21830 to your computer and use it in GitHub Desktop.
freezeReactiveValue examples
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)
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