R/template_regfactsheet.R

Defines functions template_RegFactsheet

Documented in template_RegFactsheet

# WARNING - Generated by {fusen} from /dev/dev_template.Rmd: do not edit by hand

# usethis::use_rmarkdown_template(
#   template_name = "regional_factsheet",
#   template_dir = NULL,
#   template_description = "Key Statistical Facts for each Country",
#   template_create_dir = TRUE
# )
#' Generate all country factsheet 
#' 
#' @param year Numeric value of the year (for instance 2020)
#' @param region Bureau that covers all the countrie factsheet to generate
#' @param lag Number of year to used as comparison base
#' @param folder folder within your project where to put the generated report. 
#'           Folder will be created if it does not exist
#'           
#' @importFrom unhcrdown paged_simple
#' @importFrom dplyr filter select pull
#' @importFrom rmarkdown render
#' @importFrom here here
#' 
#' @return nothing the file for the report is generated
#' 
#' @export 
#'

#' @examples
#' 
#' # template_RegFactsheet(year = 2022, 
#' #                       region = "Europe", lag = 10,  
#' #                       folder = "Report")
#' 
#' ## We can also generate all factsheets in a loop for 2022
#' 
#' # region <-  ForcedDisplacementStat::reference |>
#' #   dplyr::distinct(UNHCRBureau) |>
#' #   dplyr::filter(!(is.na(UNHCRBureau))) |>
#' #   dplyr::pull()
#' # 
#' # for( reg in region) {
#' #   unhcrdatapackage::template_RegFactsheet(year = 2022, 
#' #                         region = reg, lag = 10, 
#' #                         folder = "Report")
#' # }
#' 
#' 
#' 
template_RegFactsheet <- function(year = 2022,
                                 region = "Americas",
                                 lag = 10,   
                                 folder = "Report") {
  
  ## Create the outfolder if it does not exist
  output_dir <- paste0(getwd(),"/",folder)
  if (!dir.exists(output_dir)) {dir.create(output_dir)}
  
  regionname <- dplyr::case_when( region == "Americas"  ~  "Americas",
                                  region == "Asia"  ~  "Asia & the Pacific",
                                  region == "EastAfrica"  ~  "Eastern Africa",
                                  region =="Europe"  ~  "Europe",
                                  region == "MENA"  ~  "Middle East & North Africa",
                                  region == "SouthAfrica"  ~  "Southern Africa",
                                  region == "WestAfrica"  ~  "Western Africa")
  
  rmarkdown::render(
    system.file("rmarkdown/templates/regional_factsheet/skeleton/skeleton.Rmd", package = "unhcrdatapackage"),
    output_file = here::here(folder, paste0('StatFactsheet-',  region, '-', year, '.html') ),
    params = list(region=region,
                  regionname = regionname,
                  year = year,
                  lag = lag)  )
}
Edouard-Legoupil/unhcrdatapackage documentation built on Nov. 6, 2023, 6:10 p.m.