diff --git a/.gitignore b/.gitignore index 4ff5907..0e958ce 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,9 @@ # Session Data files .RData +# Environment with secret values +.Renviron + # Example code in package build process *-Ex.R diff --git a/ipd.Rproj b/ipd.Rproj new file mode 100644 index 0000000..8e3c2eb --- /dev/null +++ b/ipd.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX diff --git a/script-2017.R b/script-2017.R index 4b38b83..f9cd70b 100755 --- a/script-2017.R +++ b/script-2017.R @@ -40,8 +40,8 @@ ipd_states <- c("NJ", "PA") ipd_counties <- c("34005", "34007", "34015", "34021", "42017", "42029", "42045", "42091", "42101") # Census API Key -# census_api_key("YOUR API KEY GOES HERE", install = TRUE) -census_api_key("Insert API Key Here", overwrite = TRUE) +census_api_key(Sys.getenv("CENSUS_API_KEY"), overwrite = TRUE) + # Functions min <- function(i, ..., na.rm = TRUE) { @@ -429,10 +429,17 @@ export_counts$Classification <- factor(export_counts$Classification, "Well Above Average", "NoData")) export_counts <- arrange(export_counts, Variable, Classification) + +# 2022-04-27 # replacing mutate_all with across, which supercedes it in dplyr +# was causing issues because replace_na was trying to apply to "Variable" column, +# a character data type +# across + where allows us to apply the function replace_na to just numeric columns export_counts <- export_counts %>% spread(Classification, Count) %>% - mutate_all(~(replace_na(., 0))) %>% + # mutate_all(~(replace_na(., 0))) %>% + mutate(across(where(is.numeric), ~replace_na(., 0))) %>% mutate(TOTAL = rowSums(.[2:7], na.rm = TRUE)) + # Bin break points breaks <- ipd_summary %>% select(ends_with("PctEst")) export_breaks <- round(mapply(st_dev_breaks, x = breaks, i = 5, na.rm = TRUE), digits = 3) diff --git a/script-2018.R b/script-2018.R index a3eb0a5..75673e1 100755 --- a/script-2018.R +++ b/script-2018.R @@ -7,8 +7,8 @@ library(plyr); library(here); library(sf); library(summarytools); library(tidycensus); library(tidyverse); library(tigris) # Census API Key -# census_api_key("YOUR API KEY GOES HERE", install = TRUE) -census_api_key("Insert API Key Here", overwrite = TRUE) +census_api_key(Sys.getenv("CENSUS_API_KEY"), overwrite = TRUE) + # Fields @@ -520,9 +520,15 @@ export_counts$Classification <- factor(export_counts$Classification, "Well Above Average", "NoData")) export_counts <- arrange(export_counts, Variable, Classification) + +# 2022-04-27 # replacing mutate_all with across, which supercedes it in dplyr +# was causing issues because replace_na was trying to apply to "Variable" column, +# a character data type +# across + where allows us to apply the function replace_na to just numeric columns export_counts <- export_counts %>% spread(Classification, Count) %>% - mutate_all(~(replace_na(., 0))) %>% + # mutate_all(~(replace_na(., 0))) %>% + mutate(across(where(is.numeric), ~replace_na(., 0))) %>% mutate(TOTAL = rowSums(.[2:7], na.rm = TRUE)) # Bin break points diff --git a/script-2019-PhilaOnly.r b/script-2019-PhilaOnly.r index e192f54..acfe64a 100644 --- a/script-2019-PhilaOnly.r +++ b/script-2019-PhilaOnly.r @@ -8,8 +8,7 @@ library(plyr); library(here); library(sf); library(summarytools); library(tidycensus); library(tidyverse); library(tigris); library(dplyr); library(descr) # Census API Key - -census_api_key("", overwrite = TRUE) +census_api_key(Sys.getenv("CENSUS_API_KEY"), overwrite = TRUE) # Fields @@ -529,9 +528,15 @@ export_counts$Classification <- factor(export_counts$Classification, "Well Above Average", "NoData")) export_counts <- arrange(export_counts, Variable, Classification) + +# 2022-04-27 # replacing mutate_all with across, which supercedes it in dplyr +# was causing issues because replace_na was trying to apply to "Variable" column, +# a character data type +# across + where allows us to apply the function replace_na to just numeric columns export_counts <- export_counts %>% spread(Classification, Count) %>% - mutate_all(~(replace_na(., 0))) %>% + # mutate_all(~(replace_na(., 0))) %>% + mutate(across(where(is.numeric), ~replace_na(., 0))) %>% mutate(TOTAL = rowSums(.[2:7], na.rm = TRUE)) # Bin break points diff --git a/script-2019.R b/script-2019.R index 98956e9..325ed63 100644 --- a/script-2019.R +++ b/script-2019.R @@ -8,8 +8,7 @@ library(plyr); library(here); library(sf); library(summarytools); library(tidycensus); library(tidyverse); library(tigris); library(dplyr); library(descr) # Census API Key - -census_api_key("API_Key_Goes_Here", overwrite = TRUE) +census_api_key(Sys.getenv("CENSUS_API_KEY"), overwrite = TRUE) # Fields @@ -524,9 +523,15 @@ export_counts$Classification <- factor(export_counts$Classification, "Well Above Average", "NoData")) export_counts <- arrange(export_counts, Variable, Classification) + +# 2022-04-27 # replacing mutate_all with across, which supercedes it in dplyr +# was causing issues because replace_na was trying to apply to "Variable" column, +# a character data type +# across + where allows us to apply the function replace_na to just numeric columns export_counts <- export_counts %>% spread(Classification, Count) %>% - mutate_all(~(replace_na(., 0))) %>% + # mutate_all(~(replace_na(., 0))) %>% + mutate(across(where(is.numeric), ~replace_na(., 0))) %>% mutate(TOTAL = rowSums(.[2:7], na.rm = TRUE)) # Bin break points diff --git a/script-2020.R b/script-2020.R index 927419c..3356a5d 100644 --- a/script-2020.R +++ b/script-2020.R @@ -8,8 +8,7 @@ library(plyr); library(here); library(sf); library(summarytools); library(tidycensus); library(tidyverse); library(tigris); library(dplyr); library(descr) # Census API Key - -census_api_key("insert_api_key_here", overwrite = TRUE) +census_api_key(Sys.getenv("CENSUS_API_KEY"), overwrite = TRUE) # Fields @@ -532,9 +531,15 @@ export_counts$Classification <- factor(export_counts$Classification, "Well Above Average", "NoData")) export_counts <- arrange(export_counts, Variable, Classification) + +# 2022-04-27 # replacing mutate_all with across, which supercedes it in dplyr +# was causing issues because replace_na was trying to apply to "Variable" column, +# a character data type +# across + where allows us to apply the function replace_na to just numeric columns export_counts <- export_counts %>% spread(Classification, Count) %>% - mutate_all(~(replace_na(., 0))) %>% + # mutate_all(~(replace_na(., 0))) %>% + mutate(across(where(is.numeric), ~replace_na(., 0))) %>% mutate(TOTAL = rowSums(.[2:7], na.rm = TRUE)) # Bin break points