## ----include = FALSE----------------------------------------------------------
# chunks that download hosted data are skipped on CRAN machines; set
# NOT_CRAN=true (as the pkgdown workflow does) to evaluate everything
NOT_CRAN <- identical(Sys.getenv("NOT_CRAN"), "true")
knitr::opts_chunk$set(
  message = FALSE,
  warning = FALSE,
  fig.width = 8,
  eval = NOT_CRAN
)

## ----setup, include = FALSE, eval = TRUE--------------------------------------
library(edfinr)
library(dplyr)
library(tidyr)
library(stringr)
library(ggplot2)

## ----eval = FALSE-------------------------------------------------------------
# library(edfinr)
# library(dplyr)
# library(tidyr)
# library(stringr)
# library(ggplot2)

## ----example-nominal----------------------------------------------------------
# # Get nominal (unadjusted) data - this is the default
# nominal_data <- get_finance_data(yr = "2016:2023", geo = "KY")
# 
# # View the nominal revenue for a specific district
# nominal_data |>
#   filter(dist_name == "Jefferson County") |>
#   select(year, dist_name, rev_total, rev_total_pp)

## ----example-adjusted---------------------------------------------------------
# # Get data adjusted to 2023 dollars
# real_2023_data <- get_finance_data(yr = "2016:2023", geo = "KY", cpi_adj = 2023)
# 
# # View the same district with inflation-adjusted values
# real_2023_data |>
#   filter(dist_name == "Jefferson County") |>
#   select(year, dist_name, rev_total, rev_total_pp)

## ----adj-index----------------------------------------------------------------
# real_2023_data |>
#   distinct(year, cpi_adj_index) |>
#   arrange(year)

## ----flows-vs-stocks----------------------------------------------------------
# raw <- get_finance_data(yr = "2019", geo = "KY", dataset_type = "full")
# adj <- get_finance_data(yr = "2019", geo = "KY", dataset_type = "full", cpi_adj = "2023")
# 
# # capital outlay (a flow) is scaled up to 2023 dollars
# head(adj$exp_cap_total / raw$exp_cap_total, 3)
# 
# # long-term debt outstanding (a stock) is identical in both
# identical(adj$debt_lt_end, raw$debt_lt_end)

## ----cpi-index----------------------------------------------------------------
# # Examine the CPI index values
# cpi_values <- get_finance_data(yr = "all", geo = "KY") |>
#   select(year, cpi_sy12) |>
#   distinct() |>
#   arrange(year)
# 
# print(cpi_values)
# 
# # Calculate cumulative inflation since 2012
# cpi_values |>
#   mutate(
#     inflation_since_2012 = (cpi_sy12 - 1) * 100,
#     inflation_label = paste0(round(inflation_since_2012, 1), "%")
#   )

## ----real-spending-analysis, fig.alt = "Two-panel line chart comparing nominal and real 2023-dollar per-pupil revenue in Kentucky by source (local, state, federal) from 2012 to 2023; the nominal series rise steadily while the real series are much flatter."----
# # get multiyear data in nominal dollars
# ky_nominal <- get_finance_data(yr = "all", geo = "KY", cpi_adj = "none") |>
#   mutate(type = "Nominal dollars")
# 
# # get multi-year data adjusted to 2023 dollars
# ky_real <- get_finance_data(yr = "all", geo = "KY", cpi_adj = "2023") |>
#   mutate(type = "Real 2023 dollars")
# 
# # join data
# ky_data <- bind_rows(ky_nominal, ky_real)
# 
# # calculate statewide per-pupil revenue trends for real dollars
# rev_trends <- ky_data |>
#   group_by(type, year) |>
#   summarize(
#     rev_local = sum(rev_local, na.rm = TRUE),
#     rev_state = sum(rev_state, na.rm = TRUE),
#     rev_fed = sum(rev_fed, na.rm = TRUE),
#     enroll = sum(enroll, na.rm = TRUE)
#   ) |>
#   mutate(
#     rev_local_pp = rev_local / enroll,
#     rev_state_pp = rev_state / enroll,
#     rev_fed_pp = rev_fed / enroll
#   ) |>
#   select(type, year, rev_local_pp:rev_fed_pp) |>
#   pivot_longer(
#     cols = rev_local_pp:rev_fed_pp,
#     names_to = "var", values_to = "val") |>
#   mutate(
#     var = str_remove_all(var, "rev_"),
#     var = str_remove_all(var, "_pp"),
#     var = str_to_title(var),
#     var = str_replace_all(var, "Fed", "Federal")
#   )
# 
# # plot trends
# ggplot(rev_trends) +
#   geom_line(
#     aes(x = year, y = val, color = var)
#     ) +
#   facet_wrap(~type) +
#   scale_x_continuous(breaks = seq(2013, 2023, 2)) +
#   scale_y_continuous(labels = scales::label_dollar()) +
#   labs(
#     title = "Comparing Nominal and Real Per-Pupil Revenue in Kentucky",
#     subtitle = "Statewide average per-pupil revenue by source, 2012-2023",
#     x = "Year",
#     y = "Per-Pupil Revenue",
#     color = "Revenue Source"
#   ) +
#   theme_minimal()

## ----base-year-comparison, fig.alt = "Line chart of Jefferson County, Kentucky per-pupil total revenue from 2012 to 2023 shown in nominal dollars, 2012 dollars, and 2023 dollars; the three lines share the same shape at different levels."----
# # select ky district to assess
# district_sample <- "Jefferson County"
# 
# # get data with nominal dollars and cpi-adjusted for different base years
# nominal <- get_finance_data(yr = "2012:2023", geo = "KY") |>
#   filter(dist_name == district_sample) |>
#   select(year, rev_total_pp) |>
#   mutate(type = "Nominal")
# 
# adjusted_2012 <- get_finance_data(yr = "2012:2023", geo = "KY", cpi_adj = 2012) |>
#   filter(dist_name == district_sample) |>
#   select(year, rev_total_pp) |>
#   mutate(type = "2012 Dollars")
# 
# adjusted_2023 <- get_finance_data(yr = "2012:2023", geo = "KY", cpi_adj = 2023) |>
#   filter(dist_name == district_sample) |>
#   select(year, rev_total_pp) |>
#   mutate(type = "2023 Dollars")
# 
# # join and plot data
# bind_rows(nominal, adjusted_2012, adjusted_2023) |>
#   ggplot(aes(x = year, y = rev_total_pp, color = type)) +
#   geom_line(linewidth = 1.2) +
#   scale_x_continuous(breaks = seq(2013, 2023, 2)) +
#   scale_y_continuous(labels = scales::label_dollar()) +
#   labs(
#     title = paste("Per-Pupil Revenue:", district_sample),
#     x = "Year",
#     y = "Revenue per Pupil",
#     color = "CPI Adjustment"
#   ) +
#   theme_minimal()

