Skip to content

Commit

Permalink
Add ability to specify date parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksanderbl29 committed Nov 27, 2024
1 parent c24e8f3 commit e170888
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
17 changes: 9 additions & 8 deletions R/dst_get_all_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,25 @@
#' @inheritParams dst_get_data
#' @family Data retrival functions
#' @author Aleksander Bang-Larsen <contact@@aleksanderbl.dk>
dst_get_all_data <- function(table, lang = "da") {
# Get metadata for table
dst_get_all_data <- function(table, lang = "da", parse_dst_tid = TRUE) {

# Get metadata for table
metadata <- dst_meta(table)

# Extract variable names from metadata
variable_names <- get_vars(metadata)

# Get all options for each variable as a query-list
query <- get_var_options(metadata, variable_names)

# Request table with query params
data <- dst_get_data(
table = table,
query = query,
lang = lang
lang = lang,
parse_dst_tid = parse_dst_tid
)

return(data)
}

Expand Down
28 changes: 14 additions & 14 deletions R/dst_get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,58 +15,58 @@
#' @param value_presentation For now, "value" or "default"
#' @export
#' @family Data retrival functions
dst_get_data <- function(table, ..., query = NULL, parse_dst_tid = TRUE, lang = "da",
dst_get_data <- function(table, ..., query = NULL, parse_dst_tid = TRUE, lang = "da",
meta_data = NULL, format = "CSV", value_presentation = "Value"){

# Test that the language is either da or english
if(!stringr::str_detect(lang, "da|en")){
stop("The lang parameter can only be 'da' or 'en'")
}

# Test that the format is either CSV and BULK
if(!stringr::str_detect(format, "CSV|BULK")){
stop("The format parameter can only be CSV or BULK")
}

dst_url <- paste0("http://api.statbank.dk/v1/data/", table, "/", format, "?")

dst_url <- httr::parse_url(url = dst_url)

## If query is NULL, then use ... as query
if(is.null(query)){
query <- list(...)
if(length(query) == 0) stop("You need to build a query in ... or supply one to 'query'")
}

# Force the names to be uppercase to match requirements from API
names(query) <- toupper(names(query))
dst_names <- names(query)

# Match the text values with ids that needs to be supplied in the api request.
query <- dst_query_match(table = table, lang = lang, meta_data = meta_data, query = query, format = format)

query$valuePresentation <- value_presentation
query$lang <- lang

## Insert request into url
dst_url$query <- query

dst_url$query <- lapply(X = dst_url$query, FUN = paste, collapse = ',')

dst_url <- httr::build_url(dst_url)
dst_url <- dst_correct_url(dst_url)

## GET the data
dst_data <- httr::GET(dst_url)

# Make sure the returned status is OK
if(httr::status_code(dst_data) != 200){
stop(httr::content(dst_data, as = "text", encoding = "UTF-8")$message)
}

# Get the content
dst_data <- httr::content(x = dst_data, as = "text", encoding = "UTF-8")

if(lang == "da"){
dst_data <- read.csv2(text = dst_data, stringsAsFactors = FALSE, na.strings = c(".."))
} else if(lang == "en"){
Expand All @@ -75,11 +75,11 @@ dst_get_data <- function(table, ..., query = NULL, parse_dst_tid = TRUE, lang =
stop("You haven't selected an appropiate language ('da' or 'en'")
}
names(dst_data) <- c(dst_names, "value")

# Parse the dates if param is TRUE
if(parse_dst_tid){
dst_data$TID <- dst_date_parse(dst_date = dst_data$TID)
}

return(dst_data)
}
5 changes: 4 additions & 1 deletion man/dst_get_all_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e170888

Please sign in to comment.