-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a31ff4
commit 90f2a3e
Showing
18 changed files
with
264 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
linters: linters_with_defaults() # see vignette("lintr") | ||
encoding: "UTF-8" | ||
exclusions: list( | ||
"tests") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,25 @@ | ||
#' Corrects url encoding for Danish letters. | ||
#' | ||
#' @param url A build url. | ||
dst_correct_url <- function(url){ | ||
url <- stringr::str_replace_all(string = url, pattern = "%C6", replacement = "%C3%86") # ? | ||
url <- stringr::str_replace_all(string = url, pattern = "%D8", replacement = "%C3%98") # ? | ||
url <- stringr::str_replace_all(string = url, pattern = "%C5", replacement = "%C3%85") # ? | ||
url <- stringr::str_replace_all(string = url, pattern = "%E6", replacement = "%C3%A6") # ? | ||
url <- stringr::str_replace_all(string = url, pattern = "%F8", replacement = "%C3%B8") # ? | ||
url <- stringr::str_replace_all(string = url, pattern = "%E5", replacement = "%C3%A5") # ? | ||
|
||
dst_correct_url <- function(url) { | ||
url <- stringr::str_replace_all(string = url, | ||
pattern = "%C6", | ||
replacement = "%C3%86") # ? | ||
url <- stringr::str_replace_all(string = url, | ||
pattern = "%D8", | ||
replacement = "%C3%98") # ? | ||
url <- stringr::str_replace_all(string = url, | ||
pattern = "%C5", | ||
replacement = "%C3%85") # ? | ||
url <- stringr::str_replace_all(string = url, | ||
pattern = "%E6", | ||
replacement = "%C3%A6") # ? | ||
url <- stringr::str_replace_all(string = url, | ||
pattern = "%F8", | ||
replacement = "%C3%B8") # ? | ||
url <- stringr::str_replace_all(string = url, | ||
pattern = "%E5", | ||
replacement = "%C3%A5") # ? | ||
|
||
return(url) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,74 @@ | ||
|
||
|
||
|
||
#' Helper function to parse the dates from the statbank. | ||
#' | ||
#' @param dst_date A vector of length one or more with date formats like | ||
#' 1982M12D09, 1982M12, 1982Q4 or 1982 | ||
#' @returns Returns the input date formatted to be Europe/Copenhagen | ||
dst_date_parse <- function(dst_date){ | ||
|
||
dst_date_parse <- function(dst_date) { | ||
tz <- "Europe/Copenhagen" | ||
|
||
if(all(stringr::str_detect(dst_date, "[0-9]{4}+[M]{1}+[0-1]{1}+[0-9]{1}+[D]{1}+(([0-2]{1}+[0-9]{1})|([3]{1}+[0-1]{1}))")) & | ||
all(stringr::str_length(string = dst_date) == 10)){ | ||
|
||
if (all(stringr::str_detect(dst_date, | ||
"[0-9]{4}+[M]{1}+[0-1]{1}+[0-9]{1}+[D]{1}+(([0-2]{1}+[0-9]{1})|([3]{1}+[0-1]{1}))" # nolint | ||
)) && all(stringr::str_length(string = dst_date) == 10)) { | ||
# Daily | ||
dst_date <- lubridate::ymd(paste0(stringr::str_sub(dst_date, start = 1L, end = 4L), "-", | ||
stringr::str_sub(dst_date, start = -5L, end = -4L), "-", | ||
stringr::str_sub(dst_date, start = -2L)), | ||
tz = tz) | ||
} else if(all(stringr::str_detect(dst_date, "^[0-9]{4}+[M]{1}+(([0]{1}+[0-9]{1})|([1]{1}+[0-2]{1}))")) & | ||
all(stringr::str_length(string = dst_date) == 7)){ | ||
|
||
dst_date <- lubridate::ymd(paste0( | ||
stringr::str_sub(dst_date, start = 1L, end = 4L), | ||
"-", | ||
stringr::str_sub(dst_date, start = -5L, end = -4L), | ||
"-", | ||
stringr::str_sub(dst_date, start = -2L) | ||
), | ||
tz = tz) | ||
} else if ( | ||
# nolint start | ||
all(stringr::str_detect(dst_date, | ||
"^[0-9]{4}+[M]{1}+(([0]{1}+[0-9]{1})|([1]{1}+[0-2]{1}))")) && | ||
all(stringr::str_length(string = dst_date) == 7)) { | ||
# nolint end | ||
# Monthly | ||
dst_date <- lubridate::ymd(paste0(stringr::str_sub(dst_date, start = 1L, end = 4L), "-", | ||
stringr::str_sub(dst_date, start = -2L), "-", | ||
"-01"), | ||
tz = tz) | ||
} else if(all(stringr::str_detect(dst_date, "^[0-9]{4}+([Q]{1}|[K]{1})+[1-4]{1}")) & | ||
all(stringr::str_length(string = dst_date) == 6)){ | ||
|
||
dst_date <- lubridate::ymd(paste0( | ||
stringr::str_sub(dst_date, start = 1L, end = 4L), | ||
"-", | ||
stringr::str_sub(dst_date, start = -2L), | ||
"-", | ||
"-01" | ||
), | ||
tz = tz) | ||
} else if ( | ||
# nolint start | ||
all(stringr::str_detect(dst_date, "^[0-9]{4}+([Q]{1}|[K]{1})+[1-4]{1}")) && | ||
all(stringr::str_length(string = dst_date) == 6)) { | ||
# nolint end | ||
# Quarterly | ||
dst_date <- lubridate::ymd(paste0(stringr::str_sub(dst_date, start = 1L, end = 4L), "-", | ||
as.numeric(stringr::str_sub(dst_date, start = -1L)) * 3 - 2, "-", | ||
"-01"), | ||
tz = tz) | ||
} else if(all(stringr::str_detect(dst_date, "^[0-9]{4}")) & | ||
all(stringr::str_length(dst_date) == 4)){ | ||
|
||
dst_date <- lubridate::ymd(paste0( | ||
stringr::str_sub(dst_date, start = 1L, end = 4L), | ||
"-", | ||
as.numeric(stringr::str_sub(dst_date, start = -1L)) * 3 - 2, | ||
"-", | ||
"-01" | ||
), | ||
tz = tz) | ||
} else if (all(stringr::str_detect(dst_date, "^[0-9]{4}")) && | ||
all(stringr::str_length(dst_date) == 4)) { | ||
# Yearly | ||
dst_date <- lubridate::ymd(paste0(stringr::str_sub(dst_date, start = 1L, end = 4L), "-01-01"), | ||
tz = tz) | ||
} else if(all(stringr::str_detect(dst_date, "^[0-9]{4}+[H]{1}+[1-2]{1}")) & | ||
all(stringr::str_length(dst_date) == 6)){ | ||
|
||
dst_date <- lubridate::ymd(paste0(stringr::str_sub( | ||
dst_date, start = 1L, end = 4L | ||
), "-01-01"), tz = tz) | ||
} else if ( | ||
# nolint start | ||
all(stringr::str_detect(dst_date, "^[0-9]{4}+[H]{1}+[1-2]{1}")) & | ||
all(stringr::str_length(dst_date) == 6)) { | ||
# nolint end | ||
# Half yearly | ||
dst_date[stringr::str_sub(dst_date, start = -1L) == 1] <- paste0(stringr::str_sub(dst_date[stringr::str_sub(dst_date, start = -1L) == 1], start = 1L, end = 4L), | ||
"-01-01") | ||
dst_date[stringr::str_sub(dst_date, start = -1L) == 2] <- paste0(stringr::str_sub(dst_date[stringr::str_sub(dst_date, start = -1L) == 2], start = 1L, end = 4L), | ||
"-07-01") | ||
dst_date <- lubridate::ymd(dst_date, | ||
tz = tz) | ||
dst_date[stringr::str_sub(dst_date, start = -1L) == 1] <- paste0(stringr::str_sub(dst_date[stringr::str_sub(dst_date, start = -1L) == 1], start = 1L, end = 4L), "-01-01") # nolint | ||
dst_date[stringr::str_sub(dst_date, start = -1L) == 2] <- paste0(stringr::str_sub(dst_date[stringr::str_sub(dst_date, start = -1L) == 2], start = 1L, end = 4L), "-07-01") # nolint | ||
dst_date <- lubridate::ymd(dst_date, tz = tz) | ||
|
||
} else { | ||
stop("None of the regular expressions were matched. Please inspect the dates.") | ||
stop("None of the regular expressions were matched. Please inspect the dates.") # nolint | ||
} | ||
|
||
return(dst_date) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,43 @@ | ||
|
||
|
||
#' This is a helper function to extract the ID's in the values list | ||
#' that the dst_meta function returns. | ||
#' This is a helper function to extract the ID's in the values list that the | ||
#' dst_meta function returns. | ||
#' | ||
#' @param meta_data The meta data object. | ||
#' @param variable The variable to search in. | ||
#' @param values_text Character vector. The values you want to extract the IDs for. If NULL, the returned value will be "*". | ||
dst_find_val_id <- function(meta_data, variable, values_text = NULL){ | ||
|
||
#' @param values_text Character vector. The values you want to extract the IDs | ||
#' for. If NULL, the returned value will be "*". | ||
dst_find_val_id <- function(meta_data, variable, values_text = NULL) { | ||
names(meta_data$values) <- toupper(names(meta_data$values)) | ||
|
||
if(!is.null(values_text)){ | ||
ids <- list(meta_data$values[[variable]]$id[meta_data$values[[variable]]$text %in% values_text]) | ||
if (!is.null(values_text)) { | ||
ids <- list(meta_data$values[[variable]]$id[meta_data$values[[variable]]$text %in% values_text]) #nolint | ||
names(ids) <- variable | ||
|
||
# Test that all the values_text can be matched. If not, stop. | ||
|
||
# nolint start | ||
id_test <- as.character(values_text) %in% meta_data$values[[variable]]$text | ||
if(sum(id_test) != length(values_text)){ | ||
stop(paste0("All the values_text could not be matched in the text field of the values column of ", | ||
variable, | ||
". It might be a typo or maybe the value isn't present in the variable.")) | ||
if (sum(id_test) != length(values_text)) { | ||
stop( | ||
paste0( | ||
"All the values_text could not be matched in the text field of the values column of ", | ||
variable, | ||
". It might be a typo or maybe the value isn't present in the variable." | ||
) | ||
) | ||
} | ||
# nolint end | ||
|
||
} else { | ||
ids <- list("*") | ||
names(ids) <- variable | ||
} | ||
|
||
if(length(ids) > 1){ | ||
warning("Results couldn't be transformed to a vector as results are in a list bigger than length 1.") | ||
if (length(ids) > 1) { | ||
warning( | ||
"Results couldn't be transformed to a vector as results are in a list bigger than length 1." #nolint | ||
) | ||
} else { | ||
ids <- ids[[variable]] | ||
} | ||
return(ids) | ||
} | ||
|
||
# Lav unit test. | ||
# dst_find_val_id(meta_data = folk1, variable = "OMRÅDE", values_text = c("København", "Frederiksberg", "Odense")) # OK | ||
# dst_find_val_id(meta_data = folk1, variable = "OMRÅDE", values_text = c("København", "frederiksberg", "Odense")) # fejl eller advarsel?? | ||
# dst_find_val_id(meta_data = folk1, variable = "OMRÅDE") # Return "*". | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,30 @@ | ||
|
||
|
||
#' This functions downloads all the available tables. | ||
#' | ||
#' | ||
#' @param lang "da" (danish) or "en" (english) | ||
#' @export | ||
dst_get_tables <- function(lang = "da"){ | ||
|
||
dst_get_tables <- function(lang = "da") { | ||
# Test that the language is either da or english | ||
if(!stringr::str_detect(lang, "da|en")){ | ||
if (!stringr::str_detect(lang, "da|en")) { | ||
stop("The lang parameter can only be 'da' or 'en'") | ||
} | ||
|
||
# prepare the url | ||
dst_url <- paste0("http://api.statbank.dk/v1/tables?lang=", lang, "&format=JSON") | ||
|
||
dst_url <- paste0("http://api.statbank.dk/v1/tables?lang=", | ||
lang, | ||
"&format=JSON") | ||
|
||
# get data | ||
dst_tables <- httr::GET(url = dst_url) | ||
dst_tables <- httr::content(dst_tables, encoding = "UTF-8") | ||
|
||
# parse JSON | ||
dst_tables <- jsonlite::fromJSON(dst_tables) | ||
dst_tables$variables <- unlist(lapply(dst_tables$variables, paste, sep = ",", collapse = ",")) | ||
|
||
dst_tables$variables <- unlist(lapply( | ||
dst_tables$variables, | ||
paste, | ||
sep = ",", | ||
collapse = "," | ||
)) | ||
|
||
return(dst_tables) | ||
} | ||
|
||
|
Oops, something went wrong.