R/get_eurostat_dic.R

Defines functions get_eurostat_dic

Documented in get_eurostat_dic

#' @title Download Eurostat Dictionary
#' @description Download a Eurostat dictionary.
#' @details For given coded variable from Eurostat
#'    <https://ec.europa.eu/eurostat/>. The dictionaries link codes with
#'    human-readable labels. To translate codes to labels, use
#'    [label_eurostat()].
#' @param dictname A character, dictionary for the variable to be downloaded.
#' @param lang A character, language code. Options: "en" (default), "fr", "de".
#' @return tibble with two columns: code names and full names.
#' @export
#' @seealso [label_eurostat()], [get_eurostat()],
#'          [search_eurostat()].
#' @references
#' See `citation("eurostat")`:
#'
#' ```{r, echo=FALSE, comment="#" }
#' citation("eurostat")
#' ```
#'
#' @author Przemyslaw Biecek and Leo Lahti <leo.lahti@@iki.fi>. Thanks to
#' Wietse Dol for contributions. Updated by Pyry Kantanen to support XML
#' codelists.
#' @examplesIf check_access_to_data()
#' \donttest{
#' get_eurostat_dic("crop_pro")
#'
#' # Try another language
#' get_eurostat_dic("crop_pro", lang = "fr")
#' }
#'
#' @importFrom readr read_tsv cols col_character
#'
#' @keywords utilities database
get_eurostat_dic <- function(dictname, lang = "en") {
  
  lang <- check_lang(lang)
  
  dictlang <- paste0(dictname, "_", lang)
  if (!exists(dictlang, envir = .EurostatEnv)) {
    url <- getOption("eurostat_url")
    tname <- paste0(
      url,
      "api/dissemination/sdmx/2.1/codelist/ESTAT/", dictname, 
      "?format=TSV&lang=", lang
    )
    dict <- readr::read_tsv(url(tname),
      col_names = c("code_name", "full_name"),
      col_types = readr::cols(.default = readr::col_character())
    )
    dict$full_name <- gsub(
      pattern = "\u0092", replacement = "'",
      dict$full_name
    )

    assign(dictlang, dict, envir = .EurostatEnv)
  }
  get(dictlang, envir = .EurostatEnv)
}
ropengov/eurostat documentation built on Jan. 19, 2024, 8:10 p.m.