From 3c4e8dd722a335a17e2a6487a55166f0379a9948 Mon Sep 17 00:00:00 2001 From: Ben Young Date: Fri, 13 Dec 2024 14:55:09 -0500 Subject: [PATCH] move try catch block to avoid catching other errors; adjust skip lines for 2022 --- data-raw/CensusData.R | 50 ++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/data-raw/CensusData.R b/data-raw/CensusData.R index c046784..e99237b 100644 --- a/data-raw/CensusData.R +++ b/data-raw/CensusData.R @@ -250,32 +250,34 @@ getStateLocalGovExpenditure <- function(year) { suppressWarnings(utils::download.file(url, FullFileName, mode = "wb")) } date_accessed <- as.character(as.Date(file.mtime(FullFileName))) - - # Specify rows to skip - skip_rows <- 10 - - # Load table - df<- as.data.frame(readxl::read_excel(FullFileName, sheet = 1, - col_names = TRUE, skip = skip_rows)) - # Save date_last_modified - date_last_modified_ls <- sub("Revision date: ", - "", - df[grep("Revision date: ", - df$Description), "Description"]) - # Keep Expenditures only - df<- df[which(df$Description == "Expenditure1"):nrow(df), ] - if (year > 2011) { - Line <- as.integer(df[complete.cases(df), 1]) - } - df <- df[, colnames(df) %in% c("Description", "United States Total", - state.name, "District of Columbia")] - df <- cbind(Line, df[complete.cases(df), ]) }, - error = function(e) { - stop(paste(year, "state and local government expenditure data", - "is not avaliable from Census. Nothing is returned.")) - } + error = function(e) { + stop(paste(year, "state and local government expenditure data", + "is not avaliable from Census. Nothing is returned.")) + } ) + # Specify rows to skip + if(year == 2022) { + skip_rows <- 8 + } else { + skip_rows <- 10 + } + # Load table + df<- as.data.frame(readxl::read_excel(FullFileName, sheet = 1, + col_names = TRUE, skip = skip_rows)) + # Save date_last_modified + date_last_modified_ls <- sub("Revision date: ", + "", + df[grep("Revision date: ", + df$Description), "Description"]) + # Keep Expenditures only + df<- df[which(df$Description == "Expenditure1"):nrow(df), ] + if (year > 2011) { + Line <- as.integer(df[complete.cases(df), 1]) + } + df <- df[, colnames(df) %in% c("Description", "United States Total", + state.name, "District of Columbia")] + df <- cbind(Line, df[complete.cases(df), ]) } # Convert values to numeric and $