#This is all from my Assignment 3 to clean and prepare the data which I will be using to make my interactive map plot. This was all just copy and pasted from my Assignment 3. 

cal_ed <- fread("acgr25.txt")

cal_ed_filtered <- cal_ed |> 
  filter(CohortStudents != "*")

cal_ed_select <- cal_ed_filtered |> 
  select(AggregateLevel:`Regular HS Diploma Graduates (Rate)`) |> 
  select(-CountyCode, -DistrictCode, -SchoolCode)

cal_ed_select <- cal_ed_select |> 
  mutate(across(CohortStudents:`Regular HS Diploma Graduates (Rate)`, as.numeric))

ca_counties <- map_data("county", "california")

cal_ed_county <- cal_ed_select |> 
  filter(AggregateLevel=="C", CharterSchool=="All",DASS=="All", ReportingCategory == "TA") |> 
  mutate(subregion = tolower(CountyName))
#Making the map. I followed the process in the vignette for Choropleths on the leaflet github page. 
#Found at this link https://rstudio.github.io/leaflet/articles/choropleths.html#custom-info

options(tigris_use_cache = TRUE) 
ca_counties <- counties(state = "CA", cb = TRUE) |>  
  st_transform(crs = 4326)
## Retrieving data for the year 2024
ca_counties <- ca_counties |> 
  left_join(cal_ed_county, by = c("NAME" = "CountyName"))

pal <- colorNumeric(
  palette = "Reds",
  domain = ca_counties$`Regular HS Diploma Graduates (Rate)`,
  reverse = TRUE
)

labels <- paste0(
  "<strong>County: </strong>", ca_counties$NAME, "<br>",
  "<strong>2024 Graduation Rate: </strong>", ca_counties$`Regular HS Diploma Graduates (Rate)`
) %>% lapply(htmltools::HTML)

leaflet(ca_counties) |> 
  addProviderTiles(providers$CartoDB.Positron) |> 
  setView(lng = -119.4179, lat = 36.7783, zoom = 6) |> 
  addPolygons(
    fillColor = ~pal(`Regular HS Diploma Graduates (Rate)`),
    weight = 1,
    opacity = 1,
    color = "white",
    dashArray = "3",
    fillOpacity = 0.7,
    highlightOptions = highlightOptions(
      weight = 3,
      color = "#666",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE
    ),
    label = labels,
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto"
    )
  )