R/oecd_structure.R

Defines functions oecd_structure

Documented in oecd_structure

#' Get the variable names and code/decode pairs for an OECD dataset
#'
#' @param dataset the name of the dataset on the OECD data system. For a list of
#' OECD datasets, use OECD::get_datasets()
#' @param fancy do I want the output to come as one big data frame (`fancy=F`) or as
#' a list of dataframe, with each dataframe tailored to one part of the structure
#'
#' 
#'
#' @examples
#' oecd_structure("NAAG")
#' oecd_structure("SNA_TABLE8A")
#' oecd_structure("SNA_TABLE8A", fancy=FALSE)
#' @export
oecd_structure <- function(dataset, fancy=T){
    url <- paste0("https://stats.oecd.org/restsdmx/sdmx.ashx/GetDataStructure/", 
        dataset)
    data_structure <- readsdmx::read_sdmx(url)
    if(! fancy) return(data_structure) ## Basic data frame 
  else { ## Return a list of slightly-tidied-up data frames
    ds2 <- data_structure |>
      tibble::as_tibble() |> 
      dplyr::select(-id, -agencyID, -fr, -fr_description) |> 
      dplyr::relocate(en_description, .after=last_col()) |> 
      split(~en, drop=T) |> 
      purrr::map(\(x) select(x, -en))
    return(ds2) 
  }
}
bbcuffer/rcutils documentation built on Nov. 10, 2023, 12:08 p.m.