R/referential.R

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

#' Referential class is a class to load, check and manipulate the XLSForm 
#' @importFrom R6 R6Class
#' 
#' @export

Referential <- R6::R6Class(classname = "Referential",
                           public = list(
                             #' @description 
                             #' read the xlsx for each sheet and return a named list
                             #' @param path path to the file with the full referential
                             #' 
                             #' @importFrom readxl excel_sheets read_xlsx
                             #' 
                             #' @return named list
#' @examples
#' ref <- Referential$new(
#'   path = system.file("SurveyDesigner_Referential.xlsx", package = "surveyDesigner") 
#' )
#' 
#' head(ref$data$survey)
#' 
#' # Example by groups
#' ref$by_groups$group_intro
                             initialize = function(path){
                               # Define path
                               self$path <- path
                               
                               # Get sheets of xlsx
                               sheets <- names_of_sheet(path)
                               
                               # Read the xlsx file 
                               data <- lapply(
                                 sheets, 
                                 function(x){
                                   read_xlsx(path = path, sheet = x)}) |>
                                      setNames(nm = sheets)
                               # TODO checking survey and other sheets
                               
                               # survey have to be a xlsform

                               if(!contains_groups(data$survey)){
                                 stop("the sheet 'survey' doesn't includes groups - i.e. questions organised as module")
                               }
                               
                               self$data <- data
                               
                               # Get groups 
                               
                               self$get_groups()
                             },
                             #' @field data named list for the referential file
                             data = list(),
                             #' @field by_groups survey modules separated by begin and end to manipulate data
                             by_groups = list(),
                             #' @field path path for the xlsx file
                             path  = character(0),
                             #' @description get data by groups of begin and end
                             get_groups = function(){
                               self$by_groups <- get_groups(self$data$survey)
                               
                               message("result is store in `by_groups` sub-element")
                             }
                           ),
                           private = list(
                             
                           )
)
unhcr-americas/surveyDesigner documentation built on Sept. 29, 2023, 9:13 p.m.