Skip to content

Commit

Permalink
handle CSV files with odd header rows, closes #94
Browse files Browse the repository at this point in the history
  • Loading branch information
petrbouchal committed Mar 12, 2024
1 parent 263d1b5 commit 3cd5a9c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# statnipokladna (development version)

* `sp_get_table()` can now handle CSV files with non-standard header rows in the format '"Výkaz"ZC_VYKAZ:ZC_VYKAZ"', fixes #94

# statnipokladna 0.7.2

# statnipokladna 0.7.1
Expand Down
10 changes: 9 additions & 1 deletion R/tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,20 @@ sp_load_table <- function(path, ico = NULL) {

suppressWarnings(suppressMessages(
dt <- readr::read_csv2(path, col_types = readr::cols(.default = readr::col_character()))))

dt_new_names <- stringr::str_remove_all(names(dt), "\"[A-\\u017da-\\u017e\\s\\-\\./]*\"") |>
stringr::str_remove_all("/BIC/")

dt <- dt %>%
purrr::set_names(dt_new_names)

# print(head(dt))
if(max(stringr::str_length(dt$`ZC_ICO:ZC_ICO`), na.rm = TRUE) == 10) {
dt <- dplyr::mutate(dt, `ZC_ICO:ZC_ICO` = stringr::str_sub(.data$`ZC_ICO:ZC_ICO`, 3, 10))
}
if(!is.null(ico)) dt <- dt[dt$`ZC_ICO:ZC_ICO` %in% ico,]
dt <- dt %>%

dt <- dt |>
purrr::set_names(stringr::str_remove(names(dt), "^[A-Z_0-9/]*:")) %>%
dplyr::mutate_at(dplyr::vars(dplyr::starts_with("ZU_")), ~switch_minus(.) %>% as.numeric(.)) %>%
tidyr::extract(.data$`0FISCPER`, c("vykaz_year", "vykaz_month"), "([0-9]{4})0([0-9]{2})") %>%
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ test_that("List of tables make sense", {
test_that("get_table works", {
expect_error(sp_get_table("blah", "2012", "12"))
})

test_that("get_table works with newer files", {
expect_gt(nrow(sp_get_table("budget-local", "2022", "12")), 1000)
})

0 comments on commit 3cd5a9c

Please sign in to comment.