Skip to content

Instantly share code, notes, and snippets.

@asbates
Created June 1, 2021 19:35
Show Gist options
  • Save asbates/1b819bf0a9db30d723eb4aadfc90fa6c to your computer and use it in GitHub Desktop.
Save asbates/1b819bf0a9db30d723eb4aadfc90fa6c to your computer and use it in GitHub Desktop.
App that implements exercise 2 of Mastering Shiny section 14.4.5
# this app implements exercise 2 of section 14.4.5 in Mastering Shiny
# as you update the inputs, you can check how long the computation takes
# by seeing which of y1, y2, y3 changes
# example:
# change x1 and the time for y1 will be different from when app is initialized
# so it takes 1 second for graph to recompute when x1 changes
# exercise solution:
# when x1 changes: 1 second
# when x2 changes: 2 seconds
# when x3 changes: 1 second
library(shiny)
ui <- fluidPage(
numericInput("x1", "x1", 1),
numericInput("x2", "x2", 2),
numericInput("x3", "x3", 3)
)
server <- function(input, output) {
x1 <- reactive({input$x1})
x2 <- reactive({input$x2})
x3 <- reactive({input$x3})
y1 <- reactive({
start <- Sys.time()
Sys.sleep(1)
x1()
Sys.time() - start
})
y2 <- reactive({
start <- Sys.time()
Sys.sleep(1)
x2()
Sys.time() - start
})
y3 <- reactive({
start <- Sys.time()
Sys.sleep(1)
x2() + x3() + y2() + y2()
Sys.time() - start
})
observe({
print(y1())
print(y2())
print(y3())
cat("---------- \n")
})
}
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment