--- title: "Team Season Dashboard" format: ____[BLANK 1]____ server: ____[BLANK 2]____ --- ```{r} #| context: setup library(shiny) library(tidyverse) library(____[BLANK 3]____) library(leaflet) library(gganimate) library(gifski) teams <- read_csv("https://www.dropbox.com/scl/fi/k02mwgl05fkqnuub0nmi0/nhl_team_data.csv?rlkey=k1r4n50bejrvm38dxna9y1lbw&st=xi3591j8&dl=1", show_col_types = FALSE) metric_options <- c( "Points" = "P", "Wins" = "W", "Goals For" = "GF", "Goals Against" = "GA", "Goal Differential" = "goal_diff", "Win Percentage" = "win_pct" ) total_seasons <- n_distinct(teams$year) total_teams <- n_distinct(teams$Team) most_championships <- teams |> filter(playoff_result == "Won Stanley Cup") |> count(Team, sort = TRUE) |> slice_max(n, n = 1, with_ties = FALSE) |> mutate(Team = word(Team)) |> pull(Team) ``` # Team Trends ## Sidebar {.sidebar} ```{r} selectInput( "____[BLANK 4]____", "____[BLANK 5]____", choices = sort(unique(teams$Team)), selected = "____[BLANK 6]____" ) ``` ```{r} sliderInput( "____[BLANK 7]____", "Years", min = min(teams$year, na.rm = TRUE), max = max(teams$year, na.rm = TRUE), value = range(teams$year, na.rm = TRUE), sep = "" ) ``` ```{r} selectInput( "metric", "Metric", choices = ____[BLANK 8]____ ) ``` ## ### {height="30%"} ```{r} #| content: valuebox #| title: "Total Seasons in Data" list(icon = "calendar-range", color = "____[BLANK 10]____", value = total_seasons) ``` ```{r} #| content: valuebox #| title: "Total Teams in Data" list(icon = "people-fill", color = "#4A4A4A", value = ____[BLANK 11]____) ``` ```{r} #| content: valuebox #| title: "Team with Most Championships" list(icon = "____[BLANK 12]____", color = "#D4AF37", value = most_championships ) ``` ### {.tabset} ```{r} #| title: "Team Trends (Static)" ____[BLANK 13]____("trend_plot") ``` ```{r} #| title: "Team Trends (Animated)" imageOutput("____[BLANK 14]____", height = "50%", width = "50%") ``` ```{r} #| context: server filtered <- ____[BLANK 15]____({ teams |> filter( Team == input$team, year >= input$year_range[1], year <= input$year_range[2] ) }) ``` ```{r} #| context: server ____[BLANK 16]____$trend_plot <- renderPlotly({ dat <- filtered() team_color <- dat$Color[1] metric_name <- names(metric_options)[metric_options == input$metric] p <- dat |> ggplot(aes(x = year, y = .data[[input$metric]], group = 1, text = playoff_result)) + geom_line(color = team_color) + geom_point(color = team_color) + labs( x = "", y = "", title = paste(____[BLANK 17]____, metric_name , "by Season"), ) + theme_classic() ggplotly(p, ____[BLANK 18]____ = "text") }) ``` ```{r} #| context: server output$trend_plot_anim <- renderImage({ dat <- ____[BLANK 19]____ team_color <- dat$Color[1] metric_name <- names(metric_options)[metric_options == input$metric] p <- dat |> ggplot(aes(x = year, y = .data[[input$metric]], group = 1)) + geom_line(color = team_color) + geom_point(color = team_color) + labs( x = "", y = "", title = paste(input$team, metric_name , "by Season"), ) + theme_classic() + ____[BLANK 20]____(year) # Save as GIF tmpFile <- tempfile(fileext = ".gif") anim_save(tmpFile, ____[BLANK 21]____(p, renderer = gifski_renderer(), nframes = 20, fps = 2, res = 300, height = 2, width = 4, units = "in")) # Return list with src list(src = tmpFile, contentType = "image/gif") }, deleteFile = TRUE) ``` # Team Locations ## Sidebar {.sidebar} ```{r} selectInput( "season", "Season", choices = unique(teams$year) ) ``` ## ### {height="25%"} :::{.valuebox title="The NHL: Over a Century of Expansion"} Founded in 1917 with just four Canadian teams, the NHL shrank to the famous Original Six by 1942---Boston, Chicago, Detroit, Montreal, New York, and Toronto. These teams comprised a stable core that lasted until 1967. That year's expansion doubled the league overnight, and successive waves through the 1970s and 1990s grew it to today's 32 teams spanning the US and Canada. Relocation has also reshaped the map you see below. In 1995 Quebec became Colorado, then in 1997 Hartford became Carolina, and finally in 2011 Atlanta became Winnipeg. Each of these changes reflect shifting markets and the sport's evolving geography. ::: ### ```{r} leafletOutput("team_map") ``` ```{r} #| context: server output$team_map <- renderLeaflet({ map_data <- ____[BLANK 22]____ |> filter( year == input$season ) |> distinct(Team, Long, Lat, Logo) |> drop_na(Lat, Long) leaflet(map_data) |> addProviderTiles(providers$CartoDB.Positron) |> ____[BLANK 23]____( lng = ~Long, lat = ~Lat, icon = ~icons( iconUrl = Logo, iconWidth = 60, iconHeight = 60 ), label = ____[BLANK 24]____ ) }) ```