diff --git a/chapter-18-exercises/exercise-1/README.md b/chapter-18-exercises/exercise-1/README.md index eda5711b..f7ccaa84 100644 --- a/chapter-18-exercises/exercise-1/README.md +++ b/chapter-18-exercises/exercise-1/README.md @@ -8,7 +8,7 @@ You will need to create and work with multiple files for this exercise, followin ## 1. Wrangle Data You will need to implement a script to download the relevant movie review from the NY Times. Follow the instructions in the provided `exercise.R` file. -_This wrangling is the same as that used in Chapter 11 exercise-2; you can and should reuse that content._ +_This wrangling is the same as that used in Chapter 14 exercise-2; you can and should reuse that content._ Note that you will need to register for an API key at ****. Save this in a file `apikey.R`, and be sure to modify your `.gitignore` file to list your `apikey.R` file as one to not be committed. diff --git a/chapter-19-exercises/exercise-5/README.md b/chapter-19-exercises/exercise-5/README.md new file mode 100644 index 00000000..9ef3f956 --- /dev/null +++ b/chapter-19-exercises/exercise-5/README.md @@ -0,0 +1,7 @@ +# Exercise 5 +In this exercise, you'll build a simple Shiny application that can communicate between a server and a user-interface. The final product will be a scatterplot in which you can select which columns of the `mpg` dataset to display, and how you want the graph to be rendered (using shiny widgets): + +![final product scatterplot](imgs/final-plot.png) + +To complete the exercise, you'll need to work in both the `ui.R` and `server.R` files in RStudio, and follow the instructions there. + diff --git a/chapter-19-exercises/exercise-5/imgs/final-plot.png b/chapter-19-exercises/exercise-5/imgs/final-plot.png new file mode 100644 index 00000000..08635a19 Binary files /dev/null and b/chapter-19-exercises/exercise-5/imgs/final-plot.png differ diff --git a/chapter-19-exercises/exercise-5/server.R b/chapter-19-exercises/exercise-5/server.R new file mode 100644 index 00000000..2b1d268a --- /dev/null +++ b/chapter-19-exercises/exercise-5/server.R @@ -0,0 +1,17 @@ +### Exercise 5 ### + +library(shiny) +library(ggplot2) +# Create a shiny server that creates a scatterplot. + +# It should use an `input` with features: `x_var`, `y_var`, `color`, and `size` +# Save the result of `renderPlot` to output$scatter + + # Store `x` and `y` values to plot + + + # Store the title of the graph in a variable + + + # Create ggplot scatter + diff --git a/chapter-19-exercises/exercise-5/ui.R b/chapter-19-exercises/exercise-5/ui.R new file mode 100644 index 00000000..2f10008d --- /dev/null +++ b/chapter-19-exercises/exercise-5/ui.R @@ -0,0 +1,24 @@ +# UI for scatterplot +library(shiny) + +# Get a vector of column names (from `mpg`) to use as select inputs +select_values <- colnames(mpg) + +# Create a shinyUI with a `fluidPage` layout + + # Add a page header + + + # Add a `selectInput` for the `x` variable + + + # Add a `selectInput` for the `y` variable + + + # Add a `sliderInput` to set the `size` of each point + + + # Add a `selectInput` that allows you to select a color from a list of choices + + + # Plot the output with the name "scatter" diff --git a/chapter-19-exercises/exercise-6/README.md b/chapter-19-exercises/exercise-6/README.md new file mode 100644 index 00000000..797dcb8c --- /dev/null +++ b/chapter-19-exercises/exercise-6/README.md @@ -0,0 +1,16 @@ +# Exercise 6 +In this exercise, you'll practice building an interactive Plotly map in a Shiny application. + +As in previous exercises, you should fork and clone this repository, then follow the instructions below. + +## server.R +Your `server.R` file already loads the data you need, as well as scripts for building a map. Inside your `shinyServer`, you should do the following: + +- Replace the static input `'population'` with a dynamic value that comes from your UI + +## ui.R +Your `ui.R` file already has a `tabPanel` built displaying your map. In this section, you should add another `tabPanel` for your scatter-plot by doing the following: + +- Add a `selectInput` (with a proper id) that allows you to select a variable to map (`population`, `votes`, or `ratio`) + +Also, make sure you look at the `build_map.R` file and **understand how it is using variables to make a dynamic application**. diff --git a/chapter-19-exercises/exercise-6/data/electoral_college.csv b/chapter-19-exercises/exercise-6/data/electoral_college.csv new file mode 100644 index 00000000..40419b8d --- /dev/null +++ b/chapter-19-exercises/exercise-6/data/electoral_college.csv @@ -0,0 +1 @@ +state,votes,population Alabama,9,4858979 Alaska,3,738432 Arizona,11,6828065 Arkansas,6,2978204 California,55,39144818 Colorado,9,5456574 Connecticut,7,3590886 Delaware,3,945934 Florida,29,20271272 Georgia,16,10214860 Hawaii,4,1431603 Idaho,4,1654930 Illinois,20,12859995 Indiana,11,6619680 Iowa,6,3123899 Kansas,6,2911641 Kentucky,8,4425092 Louisiana,8,4670724 Maine,4,1329328 Maryland,10,6006401 Massachusetts,11,6794422 Michigan,16,9922576 Minnesota,10,5489594 Mississippi,6,2992333 Missouri,10,6083672 Montana,3,1032949 Nebraska,5,1896190 Nevada,6,2890845 New Hampshire,4,1330608 New Jersey,14,8958013 New Mexico,5,2085109 New York,29,19795791 North Carolina,15,10042802 North Dakota,3,756927 Ohio,18,11613423 Oklahoma,7,3911338 Oregon,7,4028977 Pennsylvania,20,12802503 Rhode Island,4,1056298 South Carolina,9,4896146 South Dakota,3,858469 Tennessee,11,6600299 Texas,38,27469114 Utah,6,2995919 Vermont,3,626042 Virginia,13,8382993 Washington,12,7170351 District of Columbia,3,672228 West Virginia,5,1844128 Wisconsin,10,5771337 Wyoming,3,586107 \ No newline at end of file diff --git a/chapter-19-exercises/exercise-6/data/state_codes.csv b/chapter-19-exercises/exercise-6/data/state_codes.csv new file mode 100644 index 00000000..4c517564 --- /dev/null +++ b/chapter-19-exercises/exercise-6/data/state_codes.csv @@ -0,0 +1 @@ +code,state AL,Alabama AK,Alaska AZ,Arizona AR,Arkansas CA,California CO,Colorado CT,Connecticut DE,Delaware FL,Florida GA,Georgia HI,Hawaii ID,Idaho IL,Illinois IN,Indiana IA,Iowa KS,Kansas KY,Kentucky LA,Louisiana ME,Maine MD,Maryland MA,Massachusetts MI,Michigan MN,Minnesota MS,Mississippi MO,Missouri MT,Montana NE,Nebraska NV,Nevada NH,New Hampshire NJ,New Jersey NM,New Mexico NY,New York NC,North Carolina ND,North Dakota OH,Ohio OK,Oklahoma OR,Oregon PA,Pennsylvania RI,Rhode Island SC,South Carolina SD,South Dakota TN,Tennessee TX,Texas UT,Utah VT,Vermont VA,Virginia WA,Washington WV,West Virginia WI,Wisconsin WY,Wyoming \ No newline at end of file diff --git a/chapter-19-exercises/exercise-6/scripts/build_map.R b/chapter-19-exercises/exercise-6/scripts/build_map.R new file mode 100644 index 00000000..39c135aa --- /dev/null +++ b/chapter-19-exercises/exercise-6/scripts/build_map.R @@ -0,0 +1,32 @@ +# BuildMap file: function that returns a plotly map +library(plotly) +library(stringr) + +# BuildMap function: fill this in with a function that returns a map: +# Derived from: https://plot.ly/r/choropleth-maps/ + +build_map <- function(data, map_var) { + # give state boundaries a white border + l <- list(color = toRGB("white"), width = 2) + + # specify some map projection/options + g <- list( + scope = 'usa', + projection = list(type = 'albers usa'), + showlakes = TRUE, + lakecolor = toRGB('white') + ) + + # Plot + p <- plot_geo(data, locationmode = 'USA-states') %>% + add_trace( + z = data[, map_var], text = ~state, locations = ~code, + color = data[, map_var], colors = 'Purples' + ) %>% + colorbar(title = "Color Title") %>% + layout( + title = str_to_title(map_var), + geo = g + ) + return(p) +} diff --git a/chapter-19-exercises/exercise-6/server.R b/chapter-19-exercises/exercise-6/server.R new file mode 100644 index 00000000..131076ea --- /dev/null +++ b/chapter-19-exercises/exercise-6/server.R @@ -0,0 +1,24 @@ +# server.R +library(dplyr) +library(shiny) +library(plotly) + +# Read in data +source('./scripts/build_map.R') +df <- read.csv('./data/electoral_college.csv', stringsAsFactors = FALSE) +state_codes <- read.csv('./data/state_codes.csv', stringsAsFactors = FALSE) + +# Join together state.codes and df +joined_data <- left_join(df, state_codes, by="state") + +# Compute the electoral votes per 100K people in each state +joined_data <- joined_data %>% mutate(ratio = votes/population * 100000) + +# Start shinyServer +shinyServer(function(input, output) { + + # Render a plotly object that returns your map + output$map <- renderPlotly({ + return(build_map(joined_data, "population")) + }) +}) \ No newline at end of file diff --git a/chapter-19-exercises/exercise-6/ui.R b/chapter-19-exercises/exercise-6/ui.R new file mode 100644 index 00000000..d2bc0609 --- /dev/null +++ b/chapter-19-exercises/exercise-6/ui.R @@ -0,0 +1,13 @@ +# Libraries +library(plotly) +library(shiny) + +# ui.R +shinyUI(fluidPage( + mainPanel( + # Add a selectInput (with a proper id) that allows you to select a variable to map + + # Plot the map + plotlyOutput("map") + ) +)) \ No newline at end of file diff --git a/chapter-19-exercises/exercise-7/README.md b/chapter-19-exercises/exercise-7/README.md new file mode 100644 index 00000000..7049c8bd --- /dev/null +++ b/chapter-19-exercises/exercise-7/README.md @@ -0,0 +1,24 @@ +# Exercise 7 +In this exercise, you'll practice building an a multi-tab Shiny application. The final product should look like this: + +![scatter plot in shiny app](imgs/scatter.png) + +The Map panel is already built for you to model. Your scatter panel should enable users to search for a state in the scatterplot. + +As in previous exercises, you should fork and clone this repository, then follow the instructions below. + +## server.R +Your `server.R` file already loads the data you need, as well as scripts for building a map and a scatter plot. Inside your `shinyServer`, you should do the following: + +- Create a `scatter` property on your `output` object. That property should be a `renderPlotly` object that returns a scatterplot (`build_scatter`) +- Make sure to pass your data and search string (i.e., `input$search`) to your `build_scatter` function. + +## ui.R +Your `ui.R` file already has a `tabPanel` built displaying your map. In this section, you should add another `tabPanel` for your scatter-plot by doing the following: + +- Create a `tabPanel` to show your scatter plot +- Add a `titlePanel` to your tab +- Create a `sidebarLayout` for this tab (page) +- Create a `sidebarPanel` for your controls +- In your `sidebarPanel`, make a `textInput` widget for searching for a state in your scatter plot +- Create a `mainPanel`, in which you should display your plotly Scatter plot diff --git a/chapter-19-exercises/exercise-7/data/electoral_college.csv b/chapter-19-exercises/exercise-7/data/electoral_college.csv new file mode 100644 index 00000000..40419b8d --- /dev/null +++ b/chapter-19-exercises/exercise-7/data/electoral_college.csv @@ -0,0 +1 @@ +state,votes,population Alabama,9,4858979 Alaska,3,738432 Arizona,11,6828065 Arkansas,6,2978204 California,55,39144818 Colorado,9,5456574 Connecticut,7,3590886 Delaware,3,945934 Florida,29,20271272 Georgia,16,10214860 Hawaii,4,1431603 Idaho,4,1654930 Illinois,20,12859995 Indiana,11,6619680 Iowa,6,3123899 Kansas,6,2911641 Kentucky,8,4425092 Louisiana,8,4670724 Maine,4,1329328 Maryland,10,6006401 Massachusetts,11,6794422 Michigan,16,9922576 Minnesota,10,5489594 Mississippi,6,2992333 Missouri,10,6083672 Montana,3,1032949 Nebraska,5,1896190 Nevada,6,2890845 New Hampshire,4,1330608 New Jersey,14,8958013 New Mexico,5,2085109 New York,29,19795791 North Carolina,15,10042802 North Dakota,3,756927 Ohio,18,11613423 Oklahoma,7,3911338 Oregon,7,4028977 Pennsylvania,20,12802503 Rhode Island,4,1056298 South Carolina,9,4896146 South Dakota,3,858469 Tennessee,11,6600299 Texas,38,27469114 Utah,6,2995919 Vermont,3,626042 Virginia,13,8382993 Washington,12,7170351 District of Columbia,3,672228 West Virginia,5,1844128 Wisconsin,10,5771337 Wyoming,3,586107 \ No newline at end of file diff --git a/chapter-19-exercises/exercise-7/data/state_codes.csv b/chapter-19-exercises/exercise-7/data/state_codes.csv new file mode 100644 index 00000000..4c517564 --- /dev/null +++ b/chapter-19-exercises/exercise-7/data/state_codes.csv @@ -0,0 +1 @@ +code,state AL,Alabama AK,Alaska AZ,Arizona AR,Arkansas CA,California CO,Colorado CT,Connecticut DE,Delaware FL,Florida GA,Georgia HI,Hawaii ID,Idaho IL,Illinois IN,Indiana IA,Iowa KS,Kansas KY,Kentucky LA,Louisiana ME,Maine MD,Maryland MA,Massachusetts MI,Michigan MN,Minnesota MS,Mississippi MO,Missouri MT,Montana NE,Nebraska NV,Nevada NH,New Hampshire NJ,New Jersey NM,New Mexico NY,New York NC,North Carolina ND,North Dakota OH,Ohio OK,Oklahoma OR,Oregon PA,Pennsylvania RI,Rhode Island SC,South Carolina SD,South Dakota TN,Tennessee TX,Texas UT,Utah VT,Vermont VA,Virginia WA,Washington WV,West Virginia WI,Wisconsin WY,Wyoming \ No newline at end of file diff --git a/chapter-19-exercises/exercise-7/imgs/scatter.png b/chapter-19-exercises/exercise-7/imgs/scatter.png new file mode 100644 index 00000000..fafdf56f Binary files /dev/null and b/chapter-19-exercises/exercise-7/imgs/scatter.png differ diff --git a/chapter-19-exercises/exercise-7/scripts/build_map.R b/chapter-19-exercises/exercise-7/scripts/build_map.R new file mode 100644 index 00000000..0c5fbaaf --- /dev/null +++ b/chapter-19-exercises/exercise-7/scripts/build_map.R @@ -0,0 +1,35 @@ +# BuildMap file: function that returns a plotly map +library(plotly) +library(stringr) + +# BuildMap function: fill this in with a function that returns a map: +# Derived from: https://plot.ly/r/choropleth-maps/ + +build_map <- function(data, map.var) { + # give state boundaries a white border + l <- list(color = toRGB("white"), width = 2) + + # specify some map projection/options + g <- list( + scope = 'usa', + projection = list(type = 'albers usa'), + showlakes = TRUE, + lakecolor = toRGB('white') + ) + + # Make equation for map color / text + var.equation <- paste0('~', map.var) + + # Plot + p <- plot_geo(data, locationmode = 'USA-states') %>% + add_trace( + z = data[,map.var], text = ~state, locations = ~code, + color = data[,map.var], colors = 'Purples' + ) %>% + colorbar(title = "Color Title") %>% + layout( + title = str_to_title(map.var), + geo = g + ) + return(p) +} diff --git a/chapter-19-exercises/exercise-7/scripts/build_scatter.R b/chapter-19-exercises/exercise-7/scripts/build_scatter.R new file mode 100644 index 00000000..9b857cc3 --- /dev/null +++ b/chapter-19-exercises/exercise-7/scripts/build_scatter.R @@ -0,0 +1,27 @@ +# Build Scatter file: function that returns a plotly Scatter plot +library(plotly) +library(stringr) + +### Build Scatter ### +build_scatter <- function(data, search = "", xvar = "population", yvar = "votes") { + # Get x and y max + xmax <- max(data[,xvar]) * 1.5 + ymax <- max(data[,yvar]) * 1.5 + + # Filter data based on search + data <- data %>% + filter(grepl(search, state)) + + # Plot data + plot_ly(x = data[, xvar], + y = data[, yvar], + mode="markers", + marker = list( + opacity = .4, + size = 10 + )) %>% + layout(xaxis = list(range = c(0, xmax), title = xvar), + yaxis = list(range = c(0, ymax), title = yvar) + ) %>% + return() +} \ No newline at end of file diff --git a/chapter-19-exercises/exercise-7/server.R b/chapter-19-exercises/exercise-7/server.R new file mode 100644 index 00000000..f45bec77 --- /dev/null +++ b/chapter-19-exercises/exercise-7/server.R @@ -0,0 +1,25 @@ +# server.R +library(dplyr) + +# Read in data +source('./scripts/build_map.R') +source('./scripts/build_scatter.R') +df <- read.csv('./data/electoral_college.csv', stringsAsFactors = FALSE) +state_codes <- read.csv('./data/state_codes.csv', stringsAsFactors = FALSE) + +# Join together state.codes and df +joined_data <- left_join(df, state_codes, by="state") + +# Compute the electoral votes per 100K people in each state +joined_data <- joined_data %>% mutate(ratio = votes/population * 100000) + +# Start shinyServer +shinyServer(function(input, output) { + + # Render a plotly object that returns your map + output$map <- renderPlotly({ + return(build_map(joined_data, input$mapvar)) + }) + + +}) \ No newline at end of file diff --git a/chapter-19-exercises/exercise-7/ui.R b/chapter-19-exercises/exercise-7/ui.R new file mode 100644 index 00000000..dc8b48e7 --- /dev/null +++ b/chapter-19-exercises/exercise-7/ui.R @@ -0,0 +1,36 @@ +# ui.R +library(shiny) +library(plotly) +shinyUI(navbarPage( + "Electoral College", + # Create a tab panel for your map + tabPanel( + "Map", + titlePanel("Electoral College Votes"), + # Create sidebar layout + sidebarLayout( + + # Side panel for controls + sidebarPanel( + + # Input to select variable to map + selectInput( + "mapvar", + label = "Variable to Map", + choices = list( + "Population" = "population", + "Electoral Votes" = "votes", + "Votes / Population" = "ratio" + ) + ) + ), + + # Main panel: display plotly map + mainPanel( + plotlyOutput("map") + ) + ) + ) + + # Create a tabPanel to show your scatter plot +)) \ No newline at end of file