From eb08882db327777bf68549f79dff21ca8c5e3b67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9DAnFeliz=E2=80=9D?= Date: Sat, 6 Apr 2024 12:25:47 -0400 Subject: [PATCH] solving exercises chapther 16 --- .../16.3.4-exercises/01-exercise.R | 29 +++++++++++++++ .../16.3.4-exercises/02-exercise.R | 28 +++++++++++++++ .../16.3.4-exercises/03-exercise.R | 35 +++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 examples/16-escaping_the_graph/16.3.4-exercises/01-exercise.R create mode 100644 examples/16-escaping_the_graph/16.3.4-exercises/02-exercise.R create mode 100644 examples/16-escaping_the_graph/16.3.4-exercises/03-exercise.R diff --git a/examples/16-escaping_the_graph/16.3.4-exercises/01-exercise.R b/examples/16-escaping_the_graph/16.3.4-exercises/01-exercise.R new file mode 100644 index 00000000..39d4a1ed --- /dev/null +++ b/examples/16-escaping_the_graph/16.3.4-exercises/01-exercise.R @@ -0,0 +1,29 @@ + +# Provide a server function that draws a histogram of 100 random numbers +# from a normal distribution when normal is clicked, and 100 random uniforms. + +library(shiny) + +ui <- fluidPage( + actionButton("rnorm", "Normal"), + actionButton("runif", "Uniform"), + plotOutput("plot") +) + +server <- function(input, output, session) { + + values <- reactiveVal(vector(mode = "numeric")) + + observeEvent(input$rnorm, values(rnorm(100))) + + observeEvent(input$runif, values(runif(100))) + + output$plot <- renderPlot({ + req(input$rnorm | input$runif) + hist(values()) + }) +} + +shinyApp(ui, server) + +# source: https://mastering-shiny-solutions.netlify.app/escaping-the-graph \ No newline at end of file diff --git a/examples/16-escaping_the_graph/16.3.4-exercises/02-exercise.R b/examples/16-escaping_the_graph/16.3.4-exercises/02-exercise.R new file mode 100644 index 00000000..ce1df7e4 --- /dev/null +++ b/examples/16-escaping_the_graph/16.3.4-exercises/02-exercise.R @@ -0,0 +1,28 @@ + +# Modify your code from above for to work with this UI: + +library(shiny) + +ui <- fluidPage( + selectInput("type", "type", c("Normal", "Uniform")), + actionButton("go", "go"), + plotOutput("plot") +) + +server <- function(input, output, session) { + + values <- reactiveVal(vector(mode = "numeric")) + + observeEvent(input$go, { + if(input$type == "Normal") values(rnorm(100)) else values(runif(100)) + }) + + output$plot <- renderPlot({ + req(input$go) + hist(values()) + }) +} + +shinyApp(ui, server) + +# source: https://mastering-shiny-solutions.netlify.app/escaping-the-graph \ No newline at end of file diff --git a/examples/16-escaping_the_graph/16.3.4-exercises/03-exercise.R b/examples/16-escaping_the_graph/16.3.4-exercises/03-exercise.R new file mode 100644 index 00000000..b1c84266 --- /dev/null +++ b/examples/16-escaping_the_graph/16.3.4-exercises/03-exercise.R @@ -0,0 +1,35 @@ + +# Rewrite your code from the previous answer to eliminate the use of +# observe()/observeEvent() and only use reactive(). +# Why can you do that for the second UI but not the first? + +# Now we just need to track the event go. + +library(shiny) + +ui <- fluidPage( + selectInput("type", "type", c("Normal", "Uniform")), + actionButton("go", "go"), + plotOutput("plot") +) + +server <- function(input, output, session) { + + values <- reactive({ + if(input$go == 0) return(NULL) + if(input$type == "Normal"){ + rnorm(100) + }else{ + runif(100) + } + }) + + output$plot <- renderPlot({ + if(is.null(values())) return(NULL) + hist(values()) + }) +} + +shinyApp(ui, server) + +# source: https://mastering-shiny-solutions.netlify.app/escaping-the-graph \ No newline at end of file