├── .gitignore ├── README.md ├── jobs-reactive.Rmd ├── jobs-tangle-2.Rmd ├── jobs-tangle.Rmd ├── jobs.Rmd ├── reactive-docs.Rproj ├── reactive-docs.key ├── shinyapps └── hadley │ └── jobs.dcf └── sparklines.html /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Reactive documents for teaching 2 | 3 | ```R 4 | devtools::install_github("hadley/tanglekit") 5 | ``` 6 | 7 | Other example of shiny apps for teaching: 8 | 9 | * https://stat.duke.edu/~mc301/shiny/applets.html 10 | * https://cwickham.shinyapps.io/anova/ 11 | * http://rstudio.georgetowncollege.edu:3838/SlowGoodness/ 12 | * https://ijlyttle.shinyapps.io/boxplot/ 13 | * http://spark.rstudio.com/khines/mcmc/ 14 | * https://github.com/gammarama/intRo 15 | * https://rich.shinyapps.io/regression/ 16 | * http://www.albany.edu/psychology/statistics/ 17 | * https://github.com/nielsrhansen/shinyProbability 18 | * https://dswalter.shinyapps.io/tdist/ 19 | * https://dswalter.shinyapps.io/twonorm/ 20 | * https://github.com/mwaskom/ShinyApps 21 | * https://github.com/jalapic/shinyapps 22 | * https://sachsmc.shinyapps.io/illustrateROC/ 23 | -------------------------------------------------------------------------------- /jobs-reactive.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Variation in the Jobs Report" 3 | output: html_document 4 | runtime: shiny 5 | --- 6 | 7 | Inspired by [How Not to Be Misled by the Jobs Report](http://www.nytimes.com/2014/05/02/upshot/how-not-to-be-misled-by-the-jobs-report.html?_r=0&abt=0002&abg=0), the Upshot. 8 | 9 | ```{r, message = FALSE, echo = FALSE} 10 | library(ggvis) 11 | library(shiny) 12 | ``` 13 | 14 | Every month the US Department of Labor reports how many jobs were added to the economy. If this is lower (or higher!) than expected, the report generates many new articles, tweets, and even influences the stockmarket. But what if that's all just based on random variation due to the uncertainty associated with the estimate? 15 | 16 | ```{r, echo = FALSE} 17 | real_jobs <- 150 18 | sliderInput("std_err", "Standard error", 0, 100, 55) 19 | ``` 20 | 21 | Take April 2014, for example. In May, the DoL reported that the economy had grown by `r renderText(input$real_jobs)`,000 jobs, with a standard error of `r renderText(input$std_err)`,000 jobs. 22 | 23 | ```{r, echo = FALSE} 24 | sliderInput("rate", "Rate", -10, 10, 0) 25 | months <- 1:12 26 | 27 | direction <- reactive({ 28 | if (is.null(input$rate)) return("") 29 | 30 | if (input$rate < 0) { 31 | paste0("decreased by ", -input$rate, ",000") 32 | } else if (input$rate == 0) { 33 | "stayed the same" 34 | } else { 35 | paste0("increased by ", input$rate, ",000") 36 | } 37 | }) 38 | ``` 39 | 40 | What does that mean? Let's put it in the context of an imaginary year (which has `r length(months)` months) where the number of jobs `r renderText(direction())` each month. 41 | 42 | ```{r, fig.width = 4, fig.height = 3, echo = FALSE} 43 | simulate_jobs <- function(months, rate = 0, stderr = 55) { 44 | if (is.null(stderr) || is.null(rate)) return(data.frame(months = 1:12, jobs = 1)) 45 | 46 | jobs <- real_jobs + months * rate + rnorm(length(months), sd = stderr) 47 | data.frame(months = months, jobs = jobs) 48 | } 49 | jobs <- reactive({ 50 | invalidateLater(1000, NULL) 51 | simulate_jobs(months, input$rate, input$std_err) 52 | }) 53 | 54 | jobs %>% 55 | ggvis(~months, ~jobs, fill := "#9E4B6C", stroke := NA) %>% 56 | layer_bars(width = 0.8) 57 | ``` 58 | -------------------------------------------------------------------------------- /jobs-tangle-2.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Variation in the Jobs Report" 3 | output: html_document 4 | runtime: shiny 5 | --- 6 | 7 | Inspired by [How Not to Be Misled by the Jobs Report](http://www.nytimes.com/2014/05/02/upshot/how-not-to-be-misled-by-the-jobs-report.html?_r=0&abt=0002&abg=0), the Upshot. 8 | 9 | ```{r, message = FALSE, echo = FALSE} 10 | library(ggvis) 11 | library(shiny) 12 | library(tanglekit) 13 | 14 | real_jobs <- 150 15 | months <- 1:12 16 | ``` 17 | 18 | Every month the US Department of Labor reports how many jobs were added to the economy. If this is lower (or higher!) than expected, the report generates many new articles, tweets, and even influences the stockmarket. But what if that's all just based on random variation due to the uncertainty associated with the estimate? 19 | 20 | Take April 2014, for example. In May, the DoL reported that the economy had grown by `r real_jobs`,000, with a standard error of `r tk_drag(55, ",000 jobs", id = "std_err", min = 0, max = 100)`. What does that mean? Let's put it in the context of an imaginary year (which has `r length(months)` months) where the number of jobs changed by `r tk_drag(0, ",000", id = "rate", min = -10, max = 10)` each month. 21 | 22 | ```{r, fig.width = 2, fig.height = 1.5, echo = FALSE} 23 | simulate_jobs <- function(months, rate = NULL, stderr = NULL) { 24 | if (is.null(rate)) rate <- 0 25 | if (is.null(stderr)) stderr <- 55 26 | 27 | jobs <- real_jobs + months * rate + rnorm(length(months), sd = stderr) 28 | data.frame(months = months, jobs = jobs) 29 | } 30 | make_jobs <- function() { 31 | reactive(simulate_jobs(months, input$rate, input$std_err)) 32 | } 33 | show_jobs <- . %>% 34 | ggvis(~months, ~jobs, fill := "#9E4B6C", stroke := NA) %>% 35 | layer_bars(width = 0.8) %>% 36 | set_options( 37 | duration = 0, 38 | padding = padding(0, 0, 0, 0), 39 | resizable = FALSE, 40 | renderer = "canvas" 41 | ) 42 | 43 | make_jobs() %>% show_jobs() 44 | make_jobs() %>% show_jobs() 45 | make_jobs() %>% show_jobs() 46 | make_jobs() %>% show_jobs() 47 | make_jobs() %>% show_jobs() 48 | make_jobs() %>% show_jobs() 49 | make_jobs() %>% show_jobs() 50 | make_jobs() %>% show_jobs() 51 | make_jobs() %>% show_jobs() 52 | ``` 53 | -------------------------------------------------------------------------------- /jobs-tangle.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Variation in the Jobs Report" 3 | output: html_document 4 | runtime: shiny 5 | --- 6 | 7 | Inspired by [How Not to Be Misled by the Jobs Report](http://www.nytimes.com/2014/05/02/upshot/how-not-to-be-misled-by-the-jobs-report.html?_r=0&abt=0002&abg=0), the Upshot. 8 | 9 | ```{r, message = FALSE, echo = FALSE} 10 | library(ggvis) 11 | library(shiny) 12 | library(tanglekit) 13 | 14 | real_jobs <- 150 15 | months <- 1:12 16 | ``` 17 | 18 | Every month the US Department of Labor reports how many jobs were added to the economy. If this is lower (or higher!) than expected, the report generates many new articles, tweets, and even influences the stockmarket. But what if that's all just based on random variation due to the uncertainty associated with the estimate? 19 | 20 | Take April 2014, for example. In May, the DoL reported that the economy had grown by `r real_jobs`,000, with a standard error of `r tk_drag(55, ",000 jobs", id = "std_err", min = 0, max = 100)`. What does that mean? Let's put it in the context of an imaginary year (which has `r length(months)` months) where the number of jobs changed by `r tk_drag(0, ",000", id = "rate", min = -10, max = 10)` each month. 21 | 22 | ```{r, fig.width = 4, fig.height = 3, echo = FALSE} 23 | simulate_jobs <- function(months, rate = NULL, stderr = NULL) { 24 | if (is.null(rate)) rate <- 0 25 | if (is.null(stderr)) stderr <- 55 26 | 27 | jobs <- real_jobs + months * rate + rnorm(length(months), sd = stderr) 28 | data.frame(months = months, jobs = jobs) 29 | } 30 | jobs <- reactive({ 31 | invalidateLater(1000, NULL) 32 | simulate_jobs(months, input$rate, input$std_err) 33 | }) 34 | jobs %>% 35 | ggvis(~months, ~jobs, fill := "#9E4B6C", stroke := NA) %>% 36 | layer_bars(width = 0.8) 37 | ``` 38 | -------------------------------------------------------------------------------- /jobs.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Variation in the Jobs Report" 3 | output: html_document 4 | --- 5 | 6 | Inspired by [How Not to Be Misled by the Jobs Report](http://www.nytimes.com/2014/05/02/upshot/how-not-to-be-misled-by-the-jobs-report.html?_r=0&abt=0002&abg=0), the Upshot. 7 | 8 | ```{r, message = FALSE} 9 | library(ggvis) 10 | ``` 11 | 12 | Every month the US Department of Labor reports how many jobs were added to the economy. If this is lower (or higher!) than expected, the report generates many new articles, tweets, and even influences the stockmarket. But what if that's all just based on random variation due to the uncertainty associated with the estimate? 13 | 14 | ```{r} 15 | real_jobs <- 150 16 | std_err <- 55 17 | ``` 18 | 19 | Take April 2014, for example. In May, the DoL reported that the economy had grown by `r real_jobs`,000 jobs, with a standard error of `r std_err`,000 jobs. 20 | 21 | ```{r} 22 | rate <- 0 23 | months <- 1:12 24 | ``` 25 | 26 | What does that mean? Let's put it in the context of an imaginary year (which has `r length(months)` months) where the number jobs was the same in every month (e.g. the rate of change was `r rate`). 27 | 28 | We can write a small function to simulate from the distribution: 29 | 30 | ```{r, fig.width = 4, fig.height = 3} 31 | simulate_jobs <- function(months, rate = 0, stderr = 55) { 32 | jobs <- real_jobs + months * rate + rnorm(length(months), sd = stderr) 33 | data.frame(months = months, jobs = jobs) 34 | } 35 | 36 | simulate_jobs(months) %>% 37 | ggvis(~months, ~jobs, fill := "#9E4B6C", stroke := NA) %>% 38 | layer_bars(width = 0.8) 39 | ``` 40 | 41 | -------------------------------------------------------------------------------- /reactive-docs.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | -------------------------------------------------------------------------------- /reactive-docs.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hadley/reactive-docs/a2b115c237fd2761e0a7fac368afcd3f5136ae0f/reactive-docs.key -------------------------------------------------------------------------------- /shinyapps/hadley/jobs.dcf: -------------------------------------------------------------------------------- 1 | name: jobs 2 | account: hadley 3 | bundleId: 113030 4 | url: http://hadley.shinyapps.io/jobs 5 | --------------------------------------------------------------------------------