R/dictionary.R

Defines functions search_variables get_dictionary

Documented in get_dictionary search_variables

#' Obtenir le dictionnaire des variables / Get the variable dictionary
#'
#' @param year L'annee du recensement (ex: 2023) / Census year (e.g., 2023)
#' @return A data.frame containing the variable dictionary (variable codes, labels, and descriptions) for the given census year.
#' @export
#' @examples
#' get_dictionary(2023)
get_dictionary <- function(year) {
  filename <- sprintf("dict_%s.json", year)
  dict_path <- system.file("extdata", filename, package = "sencensus")
  
  if (dict_path == "") {
    stop(sprintf("Dictionnaire introuvable pour l'annee %s", year))
  }
  
  jsonlite::fromJSON(dict_path, simplifyDataFrame = TRUE)
}

#' Chercher des variables dans le dictionnaire / Search variables in dictionary
#'
#' @param keyword Mot-cle a chercher / Search keyword
#' @param year L'annee du recensement (par defaut 2023) / Census year (default 2023)
#' @return A data.frame containing the matching variable dictionary rows.
#' @export
#' @examples
#' search_variables("ethnie", 2023)
search_variables <- function(keyword, year = 2023) {
  df <- get_dictionary(year)
  # Recherche insensible a la casse
  matches <- df[grepl(keyword, df$description, ignore.case = TRUE), ]
  return(matches)
}

Try the sencensus package in your browser

Any scripts or data that you put into this service are public.

sencensus documentation built on Sept. 26, 2026, 5:06 p.m.