diff --git a/DESCRIPTION b/DESCRIPTION index 6a86596..035bf86 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,15 +9,16 @@ Description: What the package does (one paragraph). License: MIT + file LICENSE Imports: config (>= 0.3.1), + dashboardthemes, dplyr, - dashboardthemes, - shinydashboard, golem (>= 0.4.0), magrittr, purrr, R6, readxl, - shiny (>= 1.7.3) + shiny (>= 1.7.3), + shinydashboard, + stats Suggests: knitr, prettydoc, diff --git a/NAMESPACE b/NAMESPACE index 8764827..094fecf 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,3 +25,4 @@ importFrom(readxl,read_xlsx) importFrom(shiny,NS) importFrom(shiny,shinyApp) importFrom(shiny,tagList) +importFrom(stats,setNames) diff --git a/R/has_variables_for_indicators.R b/R/has_variables_for_indicators.R new file mode 100644 index 0000000..744f87f --- /dev/null +++ b/R/has_variables_for_indicators.R @@ -0,0 +1,71 @@ +# WARNING - Generated by {fusen} from /dev/flat_r6_referential.Rmd: do not edit by hand + +#' function to check that if an indicator is defined it should map with at +#' least one variable from the survey worksheet +#' +#' @param path path to the xlsform +#' +#' @importFrom readxl excel_sheets +#' +#' @noRd +has_variables_for_indicators <- function(path){ + + # if(all(indicator_disaggregation %in% get("names_indicator_disaggregation", envir = survey_designer)) ){ + # return(indicator_disaggregation) + # } else { + # stop("Problem with the name of sheet for indicator_disaggregation") + # } +} + +#' function to check that an indicator should map with at least one population +#' +#' @param path path to the xlsform +#' +#' @importFrom readxl excel_sheets +#' +#' @noRd +indicator_linked_population <- function(path){ + + # if(all(indicator_disaggregation %in% get("names_indicator_disaggregation", envir = survey_designer)) ){ + # return(indicator_disaggregation) + # } else { + # stop("Problem with the name of sheet for indicator_disaggregation") + # } +} + +#' function to check that if a relation between one population and one indicator +#' is recorded, the indicator should also be defined in the indicator frame +#' +#' @param path path to the xlsform +#' +#' @importFrom readxl excel_sheets +#' +#' @noRd +population_linked_indicator <- function(path){ + + # if(all(indicator_disaggregation %in% get("names_indicator_disaggregation", envir = survey_designer)) ){ + # return(indicator_disaggregation) + # } else { + # stop("Problem with the name of sheet for indicator_disaggregation") + # } +} + +#' function to check that if an indicator is defined to map with variables +#' from the survey worksheet, then all those variables should actually be +#' present in the survey frame +#' +#' @param path path to the xlsform +#' +#' @importFrom readxl excel_sheets +#' +#' @noRd +indicator_linked_variable <- function(path){ + + # if(all(indicator_disaggregation %in% get("names_indicator_disaggregation", envir = survey_designer)) ){ + # return(indicator_disaggregation) + # } else { + # stop("Problem with the name of sheet for indicator_disaggregation") + # } +} + + diff --git a/R/names_of_sheet.R b/R/names_of_sheet.R new file mode 100644 index 0000000..a20a96e --- /dev/null +++ b/R/names_of_sheet.R @@ -0,0 +1,219 @@ +# WARNING - Generated by {fusen} from /dev/flat_r6_referential.Rmd: do not edit by hand + +survey_designer <- new.env() + +assign( + "names_sheets", + c("referential_type", + "survey", + "choices", + "indicator", + "indicator_survey", + "indicator_choices", + "indicator_population", + "indicator_disaggregation"), + envir = survey_designer) + +assign( + "names_referential_type", + c("referential_type", "type", "description" ), + envir = survey_designer) + +assign( + "names_survey", + c("referential_id", "type" , "name", + "label", "hint", "required", + "required_message", "constraint", "constraint_message" , + "relevant" , "appearance" , "calculation", + "trigger" , "parameters" , "repeat_count" , + "default", "read_only" , "choice_filter" , + #"media::image" , + #"$given_name", + "contextualize" , + "contextualize_instruction", "block", "block_sequence" , + "sequence", "mode" , "check" , + "accuracy", "chapter", "subchapter" , + "labelReport", "hintReport", "keyword" ), + envir = survey_designer) + +assign( + "names_choices", + c( "referential_id", "list_name", "name" , + "label" , "order" , "contextualize" , + "contextualize_instruction", "labelReport" ), + envir = survey_designer) + +assign( + "names_indicator", + c( "referential_id", "type" , "name", "labelReport" , "hintReport" , "list_name", + "repeatvar", "ind_type", "sequence" , "block", "chapter", "subchapter" , + "calculation" , "unit" , "accuracy", "mode_CAPI", "mode_CATI" , + "mode_CAWI", "metadata", "link" , "keyword" + ), + envir = survey_designer) + +assign( + "names_indicator_survey", + c( "referential_id", "name" , "name_survey" ), + envir = survey_designer) + +assign( + "names_indicator_choices", + c( "referential_id", "name" , "name_choices" ), + envir = survey_designer) + +assign( + "names_indicator_population", + c( "referential_id", "name", "name_poulation" ), + envir = survey_designer) + +assign( + "names_indicator_disaggregation", + c( "referential_id", "name" , "name_dissagregation" ), + envir = survey_designer) + +#' function to check name of sheets +#' +#' @param path path to the xlsform +#' +#' @importFrom readxl excel_sheets +#' @importFrom stats setNames +#' +#' @noRd +names_of_sheet <- function(path){ + sheets <- excel_sheets(path) + if(all(sheets == get("names_sheets", envir = survey_designer))){ + return(sheets) + } else { + stop("Problem with the name of sheets in the xls file used to load the referential") + } + + # Read the xlsx file + data <- lapply( sheets, function(x){ read_xlsx(path = path, sheet = x)}) |> + setNames(nm = sheets) + + referential_type <- names(data$referential_type) + if(all(referential_type %in% get("names_referential_type", envir = survey_designer)) ){ + return(referential_type) + } else { + stop("Problem with the name of sheet for referential_type") + } + + survey <- names(data$survey) + if(all(survey %in% get("names_survey", envir = survey_designer)) ){ + return(survey) + } else { + stop("Problem with the name of sheet for survey") + } + + choices <- names(data$choices) + if(all(choices %in% get("names_choices", envir = survey_designer)) ){ + return(choices) + } else { + stop("Problem with the name of sheet for choices") + } + + indicator <- names(data$indicator) + if(all(indicator %in% get("names_indicator", envir = survey_designer)) ){ + return(indicator) + } else { + stop("Problem with the name of sheet for indicator") + } + + indicator_survey <- names(data$indicator_survey) + if(all(indicator_survey %in% get("names_indicator_survey", envir = survey_designer)) ){ + return(indicator_survey) + } else { + stop("Problem with the name of sheet for indicator_survey") + } + + indicator_choices <- names(data$indicator_choices) + if(all(indicator_choices %in% get("names_indicator_choices", envir = survey_designer)) ){ + return(indicator_choices) + } else { + stop("Problem with the name of sheet for indicator_choices") + } + + indicator_population <- names(data$indicator_population) + if(all(indicator_population %in% get("names_indicator_population", envir = survey_designer)) ){ + return(indicator_population) + } else { + stop("Problem with the name of sheet for indicator_population") + } + + indicator_disaggregation <- names(data$indicator_disaggregation) + if(all(indicator_disaggregation %in% get("names_indicator_disaggregation", envir = survey_designer)) ){ + return(indicator_disaggregation) + } else { + stop("Problem with the name of sheet for indicator_disaggregation") + } +} + + +#' Get groups form begin and end into a list with data and information +#' +#' @param data data from the survey sheet +#' +#' @importFrom purrr map2 set_names map +#' @importFrom dplyr slice filter +#' +#' @return list +#' +get_groups <- function(data){ + # only on survey + begin_start <- grep(x = data[["type"]], "begin_") + end_stop <- grep(x = data[["type"]], "end_") + + if(length(begin_start) != length(end_stop)){ + stop("Miss one begin or stop in the data") + } + + if(!all(begin_start < end_stop)){ + stop("One begin is before a end") + } + + by_begin_end <- map2(begin_start, end_stop, + function(x,y){ + + data_to_get <- data %>% + slice(x:y) + by_groups <- list(data = data_to_get %>% + filter(!type %in% c("begin_group", "end_group")), + information = data_to_get %>% + filter(type %in% c("begin_group", "end_group")) + ) + # names(by_groups) <- by_groups[["information"]][["name"]] + + by_groups + }) %>% + purrr::set_names(nm = purrr::map(., ~ .x[["information"]][["name"]][1])) + + return(by_begin_end) +} + +#' Get choices for one question +#' +#' @param survey data from the choices sheet +#' @param full_name the full name (i.e. concatenating groups) for the variable +#' +#' @importFrom dplyr filter select contains +#' +#' @return a data.frame to join +get_choices_for_question <- function(survey, full_name){ + survey %>% + filter(list_name == full_name) %>% + select(list_name, name, label) +} + +#' function to find if we manipulate a xlsform +#' +#' @param data data of the survey +#' +#' @noRd + + +contains_groups <- function(data){ + any(grepl(x = data[["type"]], pattern = 'begin_group|begin_repeat|end_group|end_repeat')) +} + + diff --git a/dev/flat_mod_home.Rmd b/dev/flat_modules_golem.Rmd similarity index 87% rename from dev/flat_mod_home.Rmd rename to dev/flat_modules_golem.Rmd index 58e246f..7316cd2 100644 --- a/dev/flat_mod_home.Rmd +++ b/dev/flat_modules_golem.Rmd @@ -1,5 +1,5 @@ --- -title: "flat_mod_home.Rmd empty" +title: "Golem Modules for Survey Designer" output: html_document editor_options: chunk_output_type: console @@ -65,6 +65,6 @@ test_that("mod_home works", { ```{r development-inflate, eval=FALSE} # Run but keep eval=FALSE to avoid infinite loop # Execute in the console directly -fusen::inflate(flat_file = "dev/flat_mod_home.Rmd", vignette_name = "Mon module home") +fusen::inflate(flat_file = "dev/flat_modules_golem", vignette_name = "Golem Modules for Survey Designer") ``` diff --git a/dev/flat_r6_context.Rmd b/dev/flat_r6_context.Rmd new file mode 100644 index 0000000..e558176 --- /dev/null +++ b/dev/flat_r6_context.Rmd @@ -0,0 +1,158 @@ +--- +title: "context Object" +output: html_document +editor_options: + chunk_output_type: console +--- + +```{r development, include=FALSE} +library(testthat) +``` + +```{r development-load} +# Load already included functions if relevant +pkgload::load_all(export_all = FALSE) +library(readxl) +library(dplyr) +library(purrr) + +``` + +# r6_context + + +A context is created as list that includes: + * a summary of the annual survey cycle for a specific context + * one or multiple [XlsForm](http://xlsform.org) that will be then used to collect information. + +A context object is created out of a global referential and includes in addition: + * a geographic referential based on UNHCR geodb + * a list of required languages + * the translation and contextualization of questions and responses + * an additional set of ad-hoc questions + + + +```{r function-r6_context} +#' context class is a class to load, check and manipulate one or more than one XLSForm +#' @importFrom R6 R6Class +#' +#' @export + +context <- R6::R6Class(classname = "context", + public = list( + #' @description + #' read the xlsx for each sheet and return a named list + #' @param path path to the xlsForm + #' + #' @importFrom readxl excel_sheets read_xlsx + #' + #' @return named list + 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(!is_a_xlsfrom(data$survey)){ + stop("the sheet 'survey' dosen't seem to be a xlsform") + } + + self$data <- data + + # Get groups + + self$get_groups() + } + ), + private = list( + + ) +) +``` + +```{r development-test} +ref <- context$new( + path = system.file("household_survey_americas.xlsx", package = "surveyDesigner") +) +``` + + +```{r examples-r6_context} +ref <- context$new( + path = system.file("household_survey_americas.xlsx", package = "surveyDesigner") +) + +head(ref$data$survey) + +# Example by groups +ref$by_groups$group_intro +``` + +```{r tests-r6_context} +test_that("r6_context works", { + ref <- context$new( + path = system.file("household_survey_americas.xlsx", package = "surveyDesigner") + ) + + expect_true( inherits(ref, "R6") ) + + expect_error( + context$new( + path = "not_good_sheet.xlsx" + ) + ) + + expect_error( + context$new( + path = "wt_xlsform_in_survey.xlsx" + ) + ) + +}) +``` + + +# Utils for xlsx + +```{r development-utils, eval = FALSE} + + + + +``` + + +```{r function-utils_xlsform} + + +``` + + +```{r tests-utils_xlsform} +test_that("utils_xlsform works", { + + +}) +``` + + +```{r development-inflate, eval=FALSE} +# Run but keep eval=FALSE to avoid infinite loop +# Execute in the console directly +fusen::inflate( + flat_file = "dev/flat_r6_context.Rmd", + vignette_name = "Class R6 for the context" + ) +``` + diff --git a/dev/flat_r6_questionnaire.Rmd b/dev/flat_r6_questionnaire.Rmd index d7bc4c0..8f0a0d5 100644 --- a/dev/flat_r6_questionnaire.Rmd +++ b/dev/flat_r6_questionnaire.Rmd @@ -21,7 +21,15 @@ library(purrr) # r6_Questionnaire -A questionnaire is created a list of one or multiple [XlsForm](http://xlsform.org) +A questionnaire is created as a list that includes: + * a summary of the annual survey cycle for a specific context + * one or multiple [XlsForm](http://xlsform.org) that will be then used to collect information. + +A questionnaire object is created out of two objects: + * a global referential + * a specific context + + ```{r function-r6_questionnaire} #' questionnaire class is a class to load, check and manipulate one or more than one XLSForm @@ -124,7 +132,6 @@ test_that("r6_questionnaire works", { ```{r function-utils_xlsform} -survey_designer <- new.env() assign( "names_sheets", diff --git a/dev/flat_r6_referential.Rmd b/dev/flat_r6_referential.Rmd index 494004a..3559730 100644 --- a/dev/flat_r6_referential.Rmd +++ b/dev/flat_r6_referential.Rmd @@ -20,6 +20,18 @@ library(purrr) # r6_referential +A referential is initially created through the import from an excel file. + +For each Operational purpose (Household Survey, Flow Monitoring, Key Informants...), there's a distinct referential. + +This file at first defines the mapping between: + + * Indicators; + * Target population; + * Disaggregation variables; + * Survey Variables; + * Survey modalities, aka the choices. + The filtering of the referential should take in account the order/sequence of questions and modules. A specific method is implemented to separate our file with begin and end group @@ -128,8 +140,19 @@ test_that("r6_referential works", { # Utils for referential manipulation - +Utilities functions are created to ensure the full integrity of the referential + +## Utilities in relation with xlsform + + * `names_of_sheet` The referential file in excel should includes all required worksheet. When loading the referential - it should comply with xlsform and further referential-specific expected parameters - see - also https://github.com/XLSForm/pyxform + + * `contains_groups` the survey worksheet should include groups to bring together questions + + * `get_groups` utilities to pull groups from the survey worksheet + + * `get_choices_for_question` utilities to pull all choices from select_one and select_all functions + ```{r function-utils_xlsform} survey_designer <- new.env() @@ -142,10 +165,144 @@ assign( "indicator_survey", "indicator_choices", "indicator_population", - "indicator_disaggregation" + "indicator_disaggregation"), + envir = survey_designer) + +assign( + "names_referential_type", + c("referential_type", "type", "description" ), + envir = survey_designer) + +assign( + "names_survey", + c("referential_id", "type" , "name", + "label", "hint", "required", + "required_message", "constraint", "constraint_message" , + "relevant" , "appearance" , "calculation", + "trigger" , "parameters" , "repeat_count" , + "default", "read_only" , "choice_filter" , + #"media::image" , + #"$given_name", + "contextualize" , + "contextualize_instruction", "block", "block_sequence" , + "sequence", "mode" , "check" , + "accuracy", "chapter", "subchapter" , + "labelReport", "hintReport", "keyword" ), + envir = survey_designer) + +assign( + "names_choices", + c( "referential_id", "list_name", "name" , + "label" , "order" , "contextualize" , + "contextualize_instruction", "labelReport" ), + envir = survey_designer) + +assign( + "names_indicator", + c( "referential_id", "type" , "name", "labelReport" , "hintReport" , "list_name", + "repeatvar", "ind_type", "sequence" , "block", "chapter", "subchapter" , + "calculation" , "unit" , "accuracy", "mode_CAPI", "mode_CATI" , + "mode_CAWI", "metadata", "link" , "keyword" ), envir = survey_designer) +assign( + "names_indicator_survey", + c( "referential_id", "name" , "name_survey" ), + envir = survey_designer) + +assign( + "names_indicator_choices", + c( "referential_id", "name" , "name_choices" ), + envir = survey_designer) + +assign( + "names_indicator_population", + c( "referential_id", "name", "name_poulation" ), + envir = survey_designer) + +assign( + "names_indicator_disaggregation", + c( "referential_id", "name" , "name_dissagregation" ), + envir = survey_designer) + +#' function to check name of sheets +#' +#' @param path path to the xlsform +#' +#' @importFrom readxl excel_sheets +#' @importFrom stats setNames +#' +#' @noRd +names_of_sheet <- function(path){ + sheets <- excel_sheets(path) + if(all(sheets == get("names_sheets", envir = survey_designer))){ + return(sheets) + } else { + stop("Problem with the name of sheets in the xls file used to load the referential") + } + + # Read the xlsx file + data <- lapply( sheets, function(x){ read_xlsx(path = path, sheet = x)}) |> + setNames(nm = sheets) + + referential_type <- names(data$referential_type) + if(all(referential_type %in% get("names_referential_type", envir = survey_designer)) ){ + return(referential_type) + } else { + stop("Problem with the name of sheet for referential_type") + } + + survey <- names(data$survey) + if(all(survey %in% get("names_survey", envir = survey_designer)) ){ + return(survey) + } else { + stop("Problem with the name of sheet for survey") + } + + choices <- names(data$choices) + if(all(choices %in% get("names_choices", envir = survey_designer)) ){ + return(choices) + } else { + stop("Problem with the name of sheet for choices") + } + + indicator <- names(data$indicator) + if(all(indicator %in% get("names_indicator", envir = survey_designer)) ){ + return(indicator) + } else { + stop("Problem with the name of sheet for indicator") + } + + indicator_survey <- names(data$indicator_survey) + if(all(indicator_survey %in% get("names_indicator_survey", envir = survey_designer)) ){ + return(indicator_survey) + } else { + stop("Problem with the name of sheet for indicator_survey") + } + + indicator_choices <- names(data$indicator_choices) + if(all(indicator_choices %in% get("names_indicator_choices", envir = survey_designer)) ){ + return(indicator_choices) + } else { + stop("Problem with the name of sheet for indicator_choices") + } + + indicator_population <- names(data$indicator_population) + if(all(indicator_population %in% get("names_indicator_population", envir = survey_designer)) ){ + return(indicator_population) + } else { + stop("Problem with the name of sheet for indicator_population") + } + + indicator_disaggregation <- names(data$indicator_disaggregation) + if(all(indicator_disaggregation %in% get("names_indicator_disaggregation", envir = survey_designer)) ){ + return(indicator_disaggregation) + } else { + stop("Problem with the name of sheet for indicator_disaggregation") + } +} + #' Get groups form begin and end into a list with data and information #' @@ -158,34 +315,31 @@ assign( #' get_groups <- function(data){ # only on survey - -begin_start <- grep(x = data[["type"]], "begin_") -end_stop <- grep(x = data[["type"]], "end_") - - - -if(length(begin_start) != length(end_stop)){ - stop("Miss one begin or stop in the data") -} - -if(!all(begin_start < end_stop)){ - stop("One begin is before a end") -} - -by_begin_end <- map2(begin_start, end_stop, - function(x,y){ - - data_to_get <- data %>% - slice(x:y) - by_groups <- list(data = data_to_get %>% - filter(!type %in% c("begin_group", "end_group")), - information = data_to_get %>% - filter(type %in% c("begin_group", "end_group")) - ) - # names(by_groups) <- by_groups[["information"]][["name"]] - - by_groups - }) %>% + begin_start <- grep(x = data[["type"]], "begin_") + end_stop <- grep(x = data[["type"]], "end_") + + if(length(begin_start) != length(end_stop)){ + stop("Miss one begin or stop in the data") + } + + if(!all(begin_start < end_stop)){ + stop("One begin is before a end") + } + + by_begin_end <- map2(begin_start, end_stop, + function(x,y){ + + data_to_get <- data %>% + slice(x:y) + by_groups <- list(data = data_to_get %>% + filter(!type %in% c("begin_group", "end_group")), + information = data_to_get %>% + filter(type %in% c("begin_group", "end_group")) + ) + # names(by_groups) <- by_groups[["information"]][["name"]] + + by_groups + }) %>% purrr::set_names(nm = purrr::map(., ~ .x[["information"]][["name"]][1])) return(by_begin_end) @@ -216,31 +370,25 @@ contains_groups <- function(data){ any(grepl(x = data[["type"]], pattern = 'begin_group|begin_repeat|end_group|end_repeat')) } -#' function to check name of sheets -#' -#' @param path path to the xlsform -#' -#' @noRd -names_of_sheet <- function(path){ - sheets <- excel_sheets(path) - if(all(sheets == get("names_sheets", envir = survey_designer))){ - return(sheets) - }else{ - stop("Problem with the name of sheets") - } -} + ``` - - + ```{r development-utils, eval = FALSE} data <- ref$data$survey +# names(ref$data) +# names(ref$data$referential_type) +# names(ref$data$survey) +# names(ref$data$choices) +# names(ref$data$indicator) +# names(ref$data$indicator_survey) +# names(ref$data$indicator_choices) +# names(ref$data$indicator_population) +# names(ref$data$indicator_disaggregation) begin_start <- grep(x = , "begin_") end_stop <- grep(x = data[["type"]], "end_") - - if(length(begin_start) != length(end_stop)){ stop("Miss one begin or stop in the data") } @@ -268,29 +416,133 @@ by_begin_end <- purrr::map2(begin_start, end_stop, ``` - - ```{r tests-utils_xlsform} test_that("utils_xlsform works", { + expect_true(inherits(get_groups, "function")) -ref <- Referential$new( - path = system.file("SurveyDesigner_Referential.xlsx", package = "surveyDesigner") -) + ref <- Referential$new( + path = system.file("SurveyDesigner_Referential.xlsx", package = "surveyDesigner") + ) + result <- get_groups(ref$data$survey) + expect_named(result[[1]], c("data", "information")) + expect_type(result, "list") + + + expect_true(inherits(get_choices_for_question, "function")) + get_choices <- get_choices_for_question(ref$data$choices, "pop_groups") + + expect_type(get_choices, "list") -result <- get_groups(ref$data$survey) +}) +``` -expect_named(result[[1]], c("data", "information")) -expect_type(result, "list") +## Utilities in relation with indicators mapping + + * `has_variables_for_indicators` If an indicator is defined it should map with at least one variable from the survey worksheet + + * `indicator_linked_population` An indicator should map with at least one population + + * `population_linked_indicator` If a relation between one population and one indicator is recorded, the indicator should also be defined in the indicator frame + + * `indicator_linked_variable` If an indicator is defined to map with variables from the survey worksheet, then all those variables should actually be present in the survey frame + + + +```{r function-utils_indicator_map} +#' function to check that if an indicator is defined it should map with at +#' least one variable from the survey worksheet +#' +#' @param path path to the xlsform +#' +#' @importFrom readxl excel_sheets +#' +#' @noRd +has_variables_for_indicators <- function(path){ + + # if(all(indicator_disaggregation %in% get("names_indicator_disaggregation", envir = survey_designer)) ){ + # return(indicator_disaggregation) + # } else { + # stop("Problem with the name of sheet for indicator_disaggregation") + # } +} +#' function to check that an indicator should map with at least one population +#' +#' @param path path to the xlsform +#' +#' @importFrom readxl excel_sheets +#' +#' @noRd +indicator_linked_population <- function(path){ + + # if(all(indicator_disaggregation %in% get("names_indicator_disaggregation", envir = survey_designer)) ){ + # return(indicator_disaggregation) + # } else { + # stop("Problem with the name of sheet for indicator_disaggregation") + # } +} -expect_true(inherits(get_choices_for_question, "function")) -get_choices <- get_choices_for_question(ref$data$choices, "pop_groups") +#' function to check that if a relation between one population and one indicator +#' is recorded, the indicator should also be defined in the indicator frame +#' +#' @param path path to the xlsform +#' +#' @importFrom readxl excel_sheets +#' +#' @noRd +population_linked_indicator <- function(path){ + + # if(all(indicator_disaggregation %in% get("names_indicator_disaggregation", envir = survey_designer)) ){ + # return(indicator_disaggregation) + # } else { + # stop("Problem with the name of sheet for indicator_disaggregation") + # } +} -expect_type(get_choices, "list") +#' function to check that if an indicator is defined to map with variables +#' from the survey worksheet, then all those variables should actually be +#' present in the survey frame +#' +#' @param path path to the xlsform +#' +#' @importFrom readxl excel_sheets +#' +#' @noRd +indicator_linked_variable <- function(path){ + + # if(all(indicator_disaggregation %in% get("names_indicator_disaggregation", envir = survey_designer)) ){ + # return(indicator_disaggregation) + # } else { + # stop("Problem with the name of sheet for indicator_disaggregation") + # } +} + + +``` + + +```{r development-utils, eval = FALSE} + +``` + + + +```{r tests-utils_indicator_map} +test_that("utils_indicator_map works", { + expect_true(inherits(has_variables_for_indicators, "function")) + + expect_true(inherits(indicator_linked_population, "function")) + + expect_true(inherits(population_linked_indicator, "function")) + + expect_true(inherits(indicator_linked_variable, "function")) }) ``` + + + ```{r development-inflate, eval=FALSE} diff --git a/man/get_choices_for_question.Rd b/man/get_choices_for_question.Rd index 9b4cbc0..edb8e85 100644 --- a/man/get_choices_for_question.Rd +++ b/man/get_choices_for_question.Rd @@ -1,9 +1,11 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/get_groups.R +% Please edit documentation in R/get_groups.R, R/names_of_sheet.R \name{get_choices_for_question} \alias{get_choices_for_question} \title{Get choices for one question} \usage{ +get_choices_for_question(survey, full_name) + get_choices_for_question(survey, full_name) } \arguments{ @@ -12,8 +14,12 @@ get_choices_for_question(survey, full_name) \item{full_name}{the full name (i.e. concatenating groups) for the variable} } \value{ +a data.frame to join + a data.frame to join } \description{ +Get choices for one question + Get choices for one question } diff --git a/man/get_groups.Rd b/man/get_groups.Rd index 4fcb1ce..8c2400b 100644 --- a/man/get_groups.Rd +++ b/man/get_groups.Rd @@ -1,17 +1,23 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/get_groups.R +% Please edit documentation in R/get_groups.R, R/names_of_sheet.R \name{get_groups} \alias{get_groups} \title{Get groups form begin and end into a list with data and information} \usage{ +get_groups(data) + get_groups(data) } \arguments{ \item{data}{data from the survey sheet} } \value{ +list + list } \description{ +Get groups form begin and end into a list with data and information + Get groups form begin and end into a list with data and information } diff --git a/vignettes/class-r6-for-the-referential.Rmd b/vignettes/class-r6-for-the-referential.Rmd index a21a6cb..498e5be 100644 --- a/vignettes/class-r6-for-the-referential.Rmd +++ b/vignettes/class-r6-for-the-referential.Rmd @@ -40,8 +40,39 @@ ref$by_groups$group_intro # Utils for referential manipulation - +## Utilities in relation with xlsform + + + * `names_of_sheet` The referential file in excel should includes all required worksheet. When loading the referential - it should comply with xlsform and further referential-specific expected parameters - see - also https://github.com/XLSForm/pyxform + + * `contains_groups` the survey worksheet should include groups to bring together questions + + + * `get_groups` utilities to pull groups from the survey worksheet + + * `get_choices_for_question` utilities to pull all choices from select_one and select_all functions + + + + + + + + +## Utilities in relation with indicators mapping + + + * `has_variables_for_indicators` If an indicator is defined it should map with at least one variable from the survey worksheet + + * `indicator_linked_population` An indicator should map with at least one population + + * `population_linked_indicator` If a relation between one population and one indicator is recorded, the indicator should also be defined in the indicator frame + + * `indicator_linked_variable` If an indicator is defined to map with variables from the survey worksheet, then all those variables should actually be present in the survey frame + + +