diff --git a/R/dst_get_all_data.R b/R/dst_get_all_data.R index 78e6faf..50f525c 100644 --- a/R/dst_get_all_data.R +++ b/R/dst_get_all_data.R @@ -12,24 +12,25 @@ #' @inheritParams dst_get_data #' @family Data retrival functions #' @author Aleksander Bang-Larsen -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) } diff --git a/R/dst_get_data.R b/R/dst_get_data.R index dd581f5..6ca86a1 100644 --- a/R/dst_get_data.R +++ b/R/dst_get_data.R @@ -15,21 +15,21 @@ #' @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 @@ -37,36 +37,36 @@ dst_get_data <- function(table, ..., query = NULL, parse_dst_tid = TRUE, lang = 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"){ @@ -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) } diff --git a/man/dst_get_all_data.Rd b/man/dst_get_all_data.Rd index ee273ac..9f89a08 100644 --- a/man/dst_get_all_data.Rd +++ b/man/dst_get_all_data.Rd @@ -5,12 +5,15 @@ \title{This function returns the entire table requested data from the StatBank. (http://www.statistikbanken.dk/statbank5a/ or http://www.dst.dk)} \usage{ -dst_get_all_data(table, lang = "da") +dst_get_all_data(table, lang = "da", parse_dst_tid = TRUE) } \arguments{ \item{table}{Table from StatBank.} \item{lang}{language. "en" for english or "da" for danish.} + +\item{parse_dst_tid}{Logical. Default is TRUE. The date will be the first +day of the period and the time zone is "UTC" and not the Danish "CET".} } \description{ Get all data from a table in the StatBank. This function