class: center, middle ### More `shiny` <img src="img/hero_wall_pink.png" width="800px"/> ### Kelly McConville .large[Math 241 | Week 11 | Spring 2021] --- ## Announcements/Reminders * Don't forget [to sign-up for your final project group](https://docs.google.com/spreadsheets/d/1lA4KYLbkxBBA57OoiuZgOL2cnYdTvb1JR7lzxy5xKUA/edit#gid=0). * Lab 8 due in two weeks. + Don't submit on Gradescope. + Upload to your Math 241 repo and put on either the Reed Shiny Server or [https://www.shinyapps.io/](https://www.shinyapps.io/). + Can work with another Math 241 student. --- ## Goals * Recap `shiny` * Uploading to the Reed Shiny Server * Updating `leaflet` maps * Displaying the code in your app * Modifying the layout and adding HTML components * More practice --- ### But First: [Another Take on Spatial Data](https://www.nytimes.com/interactive/2021/04/06/us/variants-cases-spread.html) <img src="img/nytimes_covid_map.png" width="90%" /> --- ### [Isostat Lucy McGowan for the win!](https://livefreeordichotomize.com/2021/04/07/nytimes-map-how-to/) ```r library(tidycensus) library(tidyverse) library(geofacet) library(zoo) ``` ```r api_key <- "insert your key" ``` ```r pop <- get_acs(geography = "state", variables = "B01003_001", year = 2019, key = api_key, cache_table = TRUE) cases <- read_csv("https://github.com/nytimes/covid-19-data/raw/master/us-states.csv") ``` --- ```r d <- cases %>% group_by(state) %>% mutate(case = c(cases[1], diff(cases))) %>% ungroup() %>% filter(!(date == as.Date("2021-03-08") & state == "Missouri")) %>% left_join(pop, by = c("fips" = "GEOID")) %>% group_by(state) %>% arrange(date) %>% mutate( case_7 = rollmean(case, k = 7, fill = NA), case_per_100 = (case_7 / estimate) * 100000) %>% ungroup() %>% filter(date > as.Date("2021-01-31"), date < as.Date("2021-04-05")) states <- tibble(state = state.name, state_ = state.abb) %>% add_row(state = "District of Columbia", state_ = "DC") d <- left_join(d, states, by = "state") %>% filter(!is.na(state_)) d <- d %>% group_by(state) %>% slice_min(case_per_100) %>% slice(1) %>% mutate(min_date = date) %>% select(min_date, state) %>% left_join(d, by = "state") %>% mutate(col = ifelse(date >= min_date, "yes", "no")) ``` --- ```r ggplot(d, aes(x = date, y = case_per_100)) + geom_line(color = "#BE2D22") + geom_area(aes(fill = col), alpha = 0.75) + scale_fill_manual(values = c("white", "#BE2D22")) + facet_geo(~state_) + theme_minimal() + labs(x = "", y = "", title = "Cases per 100,000", subtitle = "Feb 1 - Apr 4, Red area indicates rise since lowest point of 2021", caption = "Note: Shows seven-day average") + theme(axis.text = element_blank(), axis.ticks = element_blank(), panel.grid.minor = element_blank(), panel.grid.major.x = element_blank(), legend.position = "none") ``` <img src="slidesWk11Th_files/figure-html/unnamed-chunk-7-1.png" width="432" /> --- ### The Key: [`facetgeo`](https://cran.r-project.org/web/packages/geofacet/vignettes/geofacet.html) ```r ggplot(d, aes(x = date, y = case_per_100)) + geom_line(color = "#BE2D22") + geom_area(aes(fill = col), alpha = 0.75) + scale_fill_manual(values = c("white", "#BE2D22")) + facet_geo(~state_) + theme_minimal() + labs(x = "", y = "", title = "Cases per 100,000", subtitle = "Feb 1 - Apr 4, Red area indicates rise since lowest point of 2021", caption = "Note: Shows seven-day average") + theme(axis.text = element_blank(), axis.ticks = element_blank(), panel.grid.minor = element_blank(), panel.grid.major.x = element_blank(), legend.position = "none") ``` --- ## `shiny` Recap -- Main Components * **Inputs**: What user manipulates + Text + Sliders + Dropdown menus + Action buttons * **Output**: What changes based on user's selections + Graphs + Maps + Tables + Text * Use **reactive programming** + Update outputs when inputs change --- ## `shiny` Recap -- Main Components * **UI**: User interface + Defines how your app **looks** * **Server function** + Defines how your app **works** ```r library(shiny) # User interface ui <- fluidPage() # Server function server <- function(input, output){} # Creates app shinyApp(ui = ui, server = server) ``` --- ## Let's Practice. * Grab the "appPractice.Rmd" file in the `ShinyApps` folder in our shared folder on the RStudio Server. * I am going to put you in Breakout Rooms. + If you have any questions, try to help each other but also use the "Ask For Help" feature and Simon or I will hop in. * If you finish before we come back together, start dressing up your app, adding more interactivity, or helping the others in your Breakout Room. * Then we will come back together and go through the answers. --- ## More Reactivity: `observe()` * Let's look at another app in the shared folder. + Within the ShinyApps folder, it is in "app_biketown". * Features `leaflet` functions and `observe()`. * Need to use `leafletProxy()` so that the entire map isn't redrawn with each update to the inputs. --- ## Uploading to Shiny Server * [Let's go through the process together](https://www.reed.edu/data-at-reed/resources/R/archive/shiny-server.html). --- ## Including your code on your app ```r runApp("/home/mcconville/ShinyApps/math241_names/app.r", display.mode = "showcase") ``` --- ## So Much More: * Consider using a [different theme](https://rstudio.github.io/shinythemes/) than the default theme: ```r library(shinythemes) ``` ```r ui <- fluidPage(theme = shinytheme("superhero"), ... ) ``` * [Add HTML Elements](https://shiny.rstudio.com/articles/tag-glossary.html) + Let's look at the app in the "app_HTML" folder. * [Layout Options](https://shiny.rstudio.com/articles/layout-guide.html) + Let's look at the app in the "app_tabs" folder. --- ## Dashboards * Dashboards are a way to make your `shiny` web application even shinier -- * Examples: + [GREGORY](https://shiny.reed.edu/s/users/wojciko/stats_greg/) + [Fires](https://shiny.reed.edu/s/users/aflowers/fires/) -- * Two `R` Packages: + [`flexdashboard`](https://rmarkdown.rstudio.com/flexdashboard/) + [`shinydashboard`](https://rstudio.github.io/shinydashboard/)