From 90f2a3ed004c10047faaba008dabef4fe9e67a0a Mon Sep 17 00:00:00 2001 From: aleksanderbl29 <73799306+aleksanderbl29@users.noreply.github.com> Date: Thu, 28 Nov 2024 00:03:28 +0100 Subject: [PATCH] Fix lint issues --- .lintr | 2 + R/dst_correct_url.R | 30 +++++++++---- R/dst_date_parse.R | 94 ++++++++++++++++++++++++----------------- R/dst_find_val_id.R | 43 ++++++++++--------- R/dst_get_data.R | 60 ++++++++++++++++++-------- R/dst_get_tables.R | 30 +++++++------ R/dst_meta.R | 29 ++++++------- R/dst_meta_parse.R | 45 +++++++++++--------- R/dst_query_match.R | 38 ++++++++++------- R/dst_search.R | 15 ++++--- R/dst_value_limit.R | 16 +++---- man/dst_find_val_id.Rd | 11 ++--- man/dst_get_all_data.Rd | 4 +- man/dst_get_data.Rd | 7 +-- man/dst_meta.Rd | 9 ++-- man/dst_meta_parse.Rd | 11 ++--- man/dst_search.Rd | 11 ++--- tests/testthat.R | 1 - 18 files changed, 264 insertions(+), 192 deletions(-) diff --git a/.lintr b/.lintr index 3d4eb22..c813fb2 100644 --- a/.lintr +++ b/.lintr @@ -1,2 +1,4 @@ linters: linters_with_defaults() # see vignette("lintr") encoding: "UTF-8" +exclusions: list( + "tests") diff --git a/R/dst_correct_url.R b/R/dst_correct_url.R index f580f54..2461fa0 100644 --- a/R/dst_correct_url.R +++ b/R/dst_correct_url.R @@ -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) -} \ No newline at end of file +} diff --git a/R/dst_date_parse.R b/R/dst_date_parse.R index 1bf6ba0..0f3fab9 100644 --- a/R/dst_date_parse.R +++ b/R/dst_date_parse.R @@ -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) } - diff --git a/R/dst_find_val_id.R b/R/dst_find_val_id.R index 0ad3e71..09021ae 100644 --- a/R/dst_find_val_id.R +++ b/R/dst_find_val_id.R @@ -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 "*". - diff --git a/R/dst_get_data.R b/R/dst_get_data.R index 2d5aa6b..5b3a2ce 100644 --- a/R/dst_get_data.R +++ b/R/dst_get_data.R @@ -6,26 +6,32 @@ #' @param table Table from StatBank. #' @param ... Build your own query. #' @param query A list object with your query. -#' @param 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". +#' @param 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". #' @param lang language. "en" for english or "da" for danish. -#' @param meta_data Meta data for the table. If NULL the meta data will be requested. +#' @param meta_data Meta data for the table. If NULL the meta data will be +#' requested. #' @param format character value. "CSV" or "BULK". If you choose BULK then you #' need to select a value for each of the parameters. #' @param value_presentation For now, "value" or "default" #' @export #' @family Data retrival functions #' @importFrom utils read.csv read.csv2 -dst_get_data <- function(table, ..., query = NULL, parse_dst_tid = TRUE, lang = "da", - meta_data = NULL, format = "CSV", value_presentation = "Value"){ - +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")){ + 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")){ + if (!stringr::str_detect(format, "CSV|BULK")) { stop("The format parameter can only be CSV or BULK") } @@ -34,9 +40,10 @@ dst_get_data <- function(table, ..., query = NULL, parse_dst_tid = TRUE, lang = dst_url <- httr::parse_url(url = dst_url) ## If query is NULL, then use ... as query - if(is.null(query)){ + if (is.null(query)) { query <- list(...) - if(length(query) == 0) stop("You need to build a query in ... or supply one to 'query'") + 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 @@ -44,7 +51,13 @@ dst_get_data <- function(table, ..., query = NULL, parse_dst_tid = TRUE, lang = 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 <- dst_query_match( + table = table, + lang = lang, + meta_data = meta_data, + query = query, + format = format + ) query$valuePresentation <- value_presentation query$lang <- lang @@ -52,7 +65,9 @@ dst_get_data <- function(table, ..., query = NULL, parse_dst_tid = TRUE, lang = ## Insert request into url dst_url$query <- query - dst_url$query <- lapply(X = dst_url$query, FUN = paste, collapse = ',') + 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) @@ -61,24 +76,33 @@ dst_get_data <- function(table, ..., query = NULL, parse_dst_tid = TRUE, lang = dst_data <- httr::GET(dst_url) # Make sure the returned status is OK - if(httr::status_code(dst_data) != 200){ + 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"){ - dst_data <- read.csv(text = dst_data, stringsAsFactors = FALSE, sep = ";", na.strings = "..") + if (lang == "da") { + dst_data <- read.csv2( + text = dst_data, + stringsAsFactors = FALSE, + na.strings = c("..") + ) + } else if (lang == "en") { + dst_data <- read.csv( + text = dst_data, + stringsAsFactors = FALSE, + sep = ";", + na.strings = ".." + ) } else { 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){ + if (parse_dst_tid) { dst_data$TID <- dst_date_parse(dst_date = dst_data$TID) } diff --git a/R/dst_get_tables.R b/R/dst_get_tables.R index 69171c5..9e51b85 100644 --- a/R/dst_get_tables.R +++ b/R/dst_get_tables.R @@ -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) } - - diff --git a/R/dst_meta.R b/R/dst_meta.R index f2f3522..d3b7061 100644 --- a/R/dst_meta.R +++ b/R/dst_meta.R @@ -1,32 +1,31 @@ - -#' This function returns meta data for a table from StatBank Denmark / Statistics Denmark. -#' (http://www.statistikbanken.dk/statbank5a/ or http://www.dst.dk) +#' This function returns meta data for a table from StatBank Denmark / +#' Statistics Denmark. (http://www.statistikbanken.dk/statbank5a/ or +#' http://www.dst.dk) #' -#' This function POSTs a request for meta data on a table from Statistics Denmark -#' and returns a JSON object with the information. +#' This function POSTs a request for meta data on a table from Statistics +#' Denmark and returns a JSON object with the information. #' #' @param table The name of the table you want meta data for. #' @param ... Ignored. #' @param lang You can choose "en" for english or "da" for danish. #' @export -dst_meta <- function(table, ..., lang = "da"){ +dst_meta <- function(table, ..., lang = "da") { ## Create and parse url dkstat_url <- paste0("http://api.statbank.dk/v1/tableinfo/", table, "?") dkstat_url <- httr::parse_url(url = dkstat_url) - + ## Insert query - dkstat_url$query <- list("lang" = lang, - "format" = "JSON") - + dkstat_url$query <- list("lang" = lang, "format" = "JSON") + ## Get data meta <- httr::GET(url = dkstat_url) - + ## Parse from JSON - meta <- jsonlite::fromJSON(txt=httr::content(meta, as="text"), - simplifyDataFrame=TRUE) - + meta <- jsonlite::fromJSON(txt = httr::content(meta, as = "text"), + simplifyDataFrame = TRUE) + ## Structure results meta <- dst_meta_parse(meta, lang) - + return(meta) } diff --git a/R/dst_meta_parse.R b/R/dst_meta_parse.R index 75088c9..e41201f 100644 --- a/R/dst_meta_parse.R +++ b/R/dst_meta_parse.R @@ -1,41 +1,46 @@ #' This function parses the JSON from the StatBank. #' (http://www.statistikbanken.dk/statbank5a/ or http://www.dst.dk) #' -#' This function structures the JSON data a bit an creates a basic_query. -#' Some tables are VERY large, so the basic request is rather simple. The -#' function returns a list with basic data, info on available variables -#' and a list with a basic request. +#' This function structures the JSON data a bit an creates a basic_query. Some +#' tables are VERY large, so the basic request is rather simple. The function +#' returns a list with basic data, info on available variables and a list with a +#' basic request. #' #' @param meta The returned meta data from the function meta_dst. -#' @param lang The language from the orignal dst_meta function (due to an error in the API) -dst_meta_parse <- function(meta, lang){ - +#' @param lang The language from the orignal dst_meta function (due to an error +#' in the API) +dst_meta_parse <- function(meta, lang) { # Get basic info on the table. # The basics contains a description on what # data the table contains, when it was updates and the units of the values. - basics_names <- c("id", "text", "description", - "unit", "updated", "footnote") + basics_names <- c("id", "text", "description", "unit", "updated", "footnote") basics <- meta[names(meta) %in% basics_names] - + # Get the description of the variables. # Keep the "elimination" columns as this describes if the variable # is mandatory or not. - variables <- meta[["variables"]][,c("id", "text", "elimination")] - + variables <- meta[["variables"]][, c("id", "text", "elimination")] + # Structure the values the user can choose in their query - values <- meta[["variables"]][,"values"] + values <- meta[["variables"]][, "values"] names(values) <- variables$id - + # Replace Q with K in the time as this is requiered by the API. # I'm not sure if this is a bug or an issue anymore but I keep the # fix just in case. - if(lang == "en"){ - test <- grepl(pattern="Tid", names(values)) - if(sum(test) > 0){ - values$Tid$id <- sub(pattern="Q", replacement="K", x=values$Tid$id) + if (lang == "en") { + test <- grepl(pattern = "Tid", names(values)) + if (sum(test) > 0) { + values$Tid$id <- sub(pattern = "Q", + replacement = "K", + x = values$Tid$id) } } - + # Return the data as a list. - return(list("basics" = basics, "variables" = variables, "values" = values)) + return(list( + "basics" = basics, + "variables" = variables, + "values" = values + )) } diff --git a/R/dst_query_match.R b/R/dst_query_match.R index bc5512b..7428631 100644 --- a/R/dst_query_match.R +++ b/R/dst_query_match.R @@ -1,4 +1,3 @@ - #' Helper function to return ids based on text values #' #' This is a helper function to return the ids based on the text values. @@ -8,10 +7,9 @@ #' @param query query to match against #' @param format Format to specify match for csv import (not used) #' -dst_query_match <- function(table, lang, meta_data, query, format){ - +dst_query_match <- function(table, lang, meta_data, query, format) { # if no meta data is supplied we download this to match the request. - if(is.null(meta_data)){ + if (is.null(meta_data)) { meta_data <- dst_meta(table = table, lang = lang) } @@ -19,31 +17,39 @@ dst_query_match <- function(table, lang, meta_data, query, format){ matched_query <- vector(mode = "list", length = length(query)) names(matched_query) <- names(query) - # Stop the function if the query variable names dosn't match the meta data names. - # Check if all names in the query by matching it with the possible names in the meta - # data - if(!(all(names(query) %in% toupper(names(meta_data$values))))){ - stop(paste0("All the variable names in your query couldn't be matched to the meta data.\n")) + # Stop the function if the query variable names dosn't match the meta data + # names. Check if all names in the query by matching it with the possible + # names in the meta data + if (!(all(names(query) %in% toupper(names(meta_data$values))))) { + stop(paste0( + "All the variable names in your query couldn't be matched to the meta data.\n" #nolint + )) } # Loop over query and match the text with the ids and then return the IDs. - # The id's are used to query the API so we need to replace the text with the ID. + # The id's are used to query the API so we need to replace the text + # with the ID. + # the * is a "get all variables" replacement. - for(i in seq_along(query)){ - if(query[[i]][1] != "*"){ - matched_query[[i]] <- dst_find_val_id(meta_data = meta_data, variable = names(query)[i], values_text = query[[i]]) + for (i in seq_along(query)) { + if (query[[i]][1] != "*") { + matched_query[[i]] <- dst_find_val_id( + meta_data = meta_data, + variable = names(query)[i], + values_text = query[[i]] + ) } else { matched_query[[i]] <- "*" } } + # nolint start # Make sure that the user hasn't eliminated some of the mandatory variable IDs # I mathc the names of the IDs in the meta data where the elimination == FALSE - if(!all(toupper(meta_data$variables$id[meta_data$variables$elimination == FALSE]) %in% toupper(names(query)))){ + if (!all(toupper(meta_data$variables$id[meta_data$variables$elimination == FALSE]) %in% toupper(names(query)))) { stop("Not all the mandatory variables are present in your query.") } + # nolint end return(matched_query) } - - diff --git a/R/dst_search.R b/R/dst_search.R index 9d99a0f..9bb87ad 100644 --- a/R/dst_search.R +++ b/R/dst_search.R @@ -1,20 +1,23 @@ -#' This function provides a search function for the description field of available -#' data series +#' This function provides a search function for the description field of +#' available data series #' #' @param string Character string. Speficies the search term. #' @param field Character string. #' @param lang Character. "da" for danish or "en" for english. -#' @param use_cache A logical scalar. Should the function call Statbank or use a (possibly outdated) saved version of their tables? +#' @param use_cache A logical scalar. Should the function call Statbank or use a +#' (possibly outdated) saved version of their tables? #' @export -dst_search <- function(string = "gpd", field = "text", lang = "da", use_cache = NULL){ +dst_search <- function(string = "gpd", + field = "text", + lang = "da", + use_cache = NULL) { if (is.null(use_cache)) { url <- httr::parse_url("http://api.statbank.dk/v1/tables") url$query <- list(lang = lang, format = "JSON") series <- httr::content(httr::GET(url = url)) series <- jsonlite::fromJSON(txt = series) - } - else if (!is.null(use_cache) & lang == "da") { + } else if (!is.null(use_cache) && lang == "da") { series <- dkstat::tables_da } else { series <- dkstat::tables_en diff --git a/R/dst_value_limit.R b/R/dst_value_limit.R index 0b54eb0..a5d5d13 100644 --- a/R/dst_value_limit.R +++ b/R/dst_value_limit.R @@ -1,23 +1,22 @@ - #' This is a helper function that returns the number #' of values the call will return. #' The API has a limit of 100.000 values. #' #' @param query Query object to analyse #' @param dst_meta Meta data to filer query with -dst_value_limit <- function(query, dst_meta){ - +dst_value_limit <- function(query, dst_meta) { # is the element in the list a "*"? - is_star <- lapply(query, function(x) {all(stringr::str_detect(string = x, pattern = "[*]"))}) + is_star <- lapply(query, function(x) { + all(stringr::str_detect(string = x, pattern = "[*]")) + }) names(dst_meta$values) <- toupper(names(dst_meta$values)) # If there are any "*", then replace the query with # values from dst_meta. - if(sum(do.call(rbind, is_star)) > 0){ - - for(i in seq_along(query)){ - if(is_star[[i]]){ + if (sum(do.call(rbind, is_star)) > 0) { + for (i in seq_along(query)) { + if (is_star[[i]]) { query[[i]] <- dst_meta$values[[names(query)[i]]]$text } } @@ -30,4 +29,3 @@ dst_value_limit <- function(query, dst_meta){ return(query_length) } -# dkstat:::dst_value_limit(query = list(OMRÅDE = "København", TID = "*"), dst_meta = dst_meta("folk1", lang = "da")) diff --git a/man/dst_find_val_id.Rd b/man/dst_find_val_id.Rd index ba2919a..13b9e7f 100644 --- a/man/dst_find_val_id.Rd +++ b/man/dst_find_val_id.Rd @@ -2,8 +2,8 @@ % Please edit documentation in R/dst_find_val_id.R \name{dst_find_val_id} \alias{dst_find_val_id} -\title{This is a helper function to extract the ID's in the values list -that the dst_meta function returns.} +\title{This is a helper function to extract the ID's in the values list that the +dst_meta function returns.} \usage{ dst_find_val_id(meta_data, variable, values_text = NULL) } @@ -12,9 +12,10 @@ dst_find_val_id(meta_data, variable, values_text = NULL) \item{variable}{The variable to search in.} -\item{values_text}{Character vector. The values you want to extract the IDs for. If NULL, the returned value will be "*".} +\item{values_text}{Character vector. The values you want to extract the IDs +for. If NULL, the returned value will be "*".} } \description{ -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. } diff --git a/man/dst_get_all_data.Rd b/man/dst_get_all_data.Rd index d8439ff..82e926e 100644 --- a/man/dst_get_all_data.Rd +++ b/man/dst_get_all_data.Rd @@ -12,8 +12,8 @@ dst_get_all_data(table, lang = "da", parse_dst_tid = TRUE) \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".} +\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 diff --git a/man/dst_get_data.Rd b/man/dst_get_data.Rd index 20ea2c0..cb0c37e 100644 --- a/man/dst_get_data.Rd +++ b/man/dst_get_data.Rd @@ -23,12 +23,13 @@ dst_get_data( \item{query}{A list object with your query.} -\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".} +\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".} \item{lang}{language. "en" for english or "da" for danish.} -\item{meta_data}{Meta data for the table. If NULL the meta data will be requested.} +\item{meta_data}{Meta data for the table. If NULL the meta data will be +requested.} \item{format}{character value. "CSV" or "BULK". If you choose BULK then you need to select a value for each of the parameters.} diff --git a/man/dst_meta.Rd b/man/dst_meta.Rd index 653414b..ac9cce9 100644 --- a/man/dst_meta.Rd +++ b/man/dst_meta.Rd @@ -2,8 +2,9 @@ % Please edit documentation in R/dst_meta.R \name{dst_meta} \alias{dst_meta} -\title{This function returns meta data for a table from StatBank Denmark / Statistics Denmark. -(http://www.statistikbanken.dk/statbank5a/ or http://www.dst.dk)} +\title{This function returns meta data for a table from StatBank Denmark / +Statistics Denmark. (http://www.statistikbanken.dk/statbank5a/ or +http://www.dst.dk)} \usage{ dst_meta(table, ..., lang = "da") } @@ -15,6 +16,6 @@ dst_meta(table, ..., lang = "da") \item{lang}{You can choose "en" for english or "da" for danish.} } \description{ -This function POSTs a request for meta data on a table from Statistics Denmark -and returns a JSON object with the information. +This function POSTs a request for meta data on a table from Statistics +Denmark and returns a JSON object with the information. } diff --git a/man/dst_meta_parse.Rd b/man/dst_meta_parse.Rd index 2367cca..b924232 100644 --- a/man/dst_meta_parse.Rd +++ b/man/dst_meta_parse.Rd @@ -10,11 +10,12 @@ dst_meta_parse(meta, lang) \arguments{ \item{meta}{The returned meta data from the function meta_dst.} -\item{lang}{The language from the orignal dst_meta function (due to an error in the API)} +\item{lang}{The language from the orignal dst_meta function (due to an error +in the API)} } \description{ -This function structures the JSON data a bit an creates a basic_query. -Some tables are VERY large, so the basic request is rather simple. The -function returns a list with basic data, info on available variables -and a list with a basic request. +This function structures the JSON data a bit an creates a basic_query. Some +tables are VERY large, so the basic request is rather simple. The function +returns a list with basic data, info on available variables and a list with a +basic request. } diff --git a/man/dst_search.Rd b/man/dst_search.Rd index 14c8d0e..0bf5d07 100644 --- a/man/dst_search.Rd +++ b/man/dst_search.Rd @@ -2,8 +2,8 @@ % Please edit documentation in R/dst_search.R \name{dst_search} \alias{dst_search} -\title{This function provides a search function for the description field of available -data series} +\title{This function provides a search function for the description field of +available data series} \usage{ dst_search(string = "gpd", field = "text", lang = "da", use_cache = NULL) } @@ -14,9 +14,10 @@ dst_search(string = "gpd", field = "text", lang = "da", use_cache = NULL) \item{lang}{Character. "da" for danish or "en" for english.} -\item{use_cache}{A logical scalar. Should the function call Statbank or use a (possibly outdated) saved version of their tables?} +\item{use_cache}{A logical scalar. Should the function call Statbank or use a +(possibly outdated) saved version of their tables?} } \description{ -This function provides a search function for the description field of available -data series +This function provides a search function for the description field of +available data series } diff --git a/tests/testthat.R b/tests/testthat.R index e8ce626..eda8f15 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -2,4 +2,3 @@ library(testthat) library(dkstat) test_check("dkstat") -