library(tidyverse)
library(nycflights23)
q <- 0Makeup Lab Quiz Info
There will be a make-up lab quiz offered during Week 10. You can take this in-class makeup and replace your lowest in-class lab quiz (provided your score on the makeup is higher). There will be no take-home portion.
Must schedule a 30 minute block outside of class during the following times:
- Monday 12:30-2:30pm
- Tuesday 10:30a-12p
- Tuesday 3-4pm
- Wednesday 9-11am
Guidelines
This is a closed note, closed internet resources, closed other people lab quiz. I want to see what’s in your brain! You may use the cheat sheets provided by me, but otherwise you may not use any resources.
The lab quizzes are not written to be tricky or very difficult. If you’ve been completing the in-class activities and the homework, and putting the time and effort in to understand them, you should do well on the lab quizzes.
Format
The reason I give lab quizzes is because your life will be easier if you know how to do basic tasks in R “on the fly”. I also want to see what you know and not just what you can do with access to your resources.
This makeup will only include a required in-class portion:
- Pencil-and-paper questions
- Cheat sheets provided by me but no other resources
- Designed to assess what you know “on the fly” without access to resources
Skills
All skills from the first 3 lab quizzes are “fair game” for this makeup, along with the following new topics. Since this quiz can replace a previous one, I’m looking for growth and proficiency with all topics from the course.
APIs and Web Scraping
- Know and explain the difference between using an API and web scraping
- Explain simple HTML scraping code
Shiny
- Distinguish which code belongs in the
ui()function and which belongs in theserver()function - Correctly track how an
input$idflows from the UI into a reactive/rendering function in the server, and how it connects back to anoutput$idin the UI - Know the matching pairs for displaying content (eg
plotOutputmatchesrenderPlot)
SQL
- Given a SQL query, explain what output will be produced
- Translate between SQL queries and {dplyr} code
Grading
For the in-class portion, each question is worth 1 point, graded as “successful” (almost entirely correct), “not successful” (missing a key component), or “half credit” (mostly correct).
Practice Questions
Data
The nycflights23 package contains information about all flights that departed from NYC (e.g. EWR, JFK and LGA) in 2023. The main data is in the flights data frame, but there are additional data sets which may help understand what causes delays, specifically:
-
weather: hourly meteorological data for each airport -
planes: construction information about each plane -
airports: airport names and locations -
airlines: translation between two letter carrier codes and names
Questions
Know and explain the difference between using an API and web scraping
You are tasked with collecting daily temperature data for NYC airports.
- Provide one reason why accessing an official weather API would be preferable to scraping a weather website’s HTML
- If the weather website does not have an API
Explain simple HTML scraping code
Read the following {rvest} code and explain
- What
html_elements("table.wikitable")does - What
html_table()does
library(rvest)
url <- "https://en.wikipedia.org/wiki/List_of_busiest_airports_by_passenger_traffic"
busiest_airports <- read_html(url) |>
html_elements("table.wikitable") |>
html_table() |>
pluck(1)Shiny
Below is a very simple Shiny app. I’ve noted 3 errors related to reactivity and app structure. For each error, identify what is wrong and how to fix it.
Error 1:
Error 2:
Error 3:
ui <- fluidPage(
selectInput("airport_choice", "Choose an Airport:", choices = c("JFK", "LGA", "EWR")),
renderPlot("delay_plot") # Error 1
)
server <- function(input, output, session) {
output$delay_plot <- plotOutput({ # Error 2
plot_data <- flights |>
filter(origin == airport_choice) # Error 3
ggplot(plot_data, aes(x = dep_delay)) +
geom_histogram()
})
}
shinyApp(ui, server)Given a SQL query, explain what output will be produced
Below is a SQL query using the flights and airlines tables in the SQL databse. (you can assume all tables/datasets and variable names are the same as {nycflights23}). Describe the output in 1-2 sentences. Your answer should include what each row and each column represent.
SELECT airlines.name, COUNT(flights.flight) AS total_flights
FROM flights
INNER JOIN airlines
ON flights.carrier = airlines.carrier
WHERE flights.origin = 'JFK'
GROUP BY airlines.name
ORDER BY total_flights DESC
LIMIT 5;
Translate between SQL queries and {dplyr} code
Below is a SQL query:
SELECT origin, AVG(dep_delay) AS avg_delay
FROM flights
WHERE month = 1
GROUP BY origin
ORDER BY avg_delay DESC;
Write down a {dplyr} pipeline to accomplish the same task (you can assume all tables/datasets and variable names are the same)
