Skip to content

Commit

Permalink
[feat] #22
Browse files Browse the repository at this point in the history
  • Loading branch information
Edouard-Legoupil committed May 1, 2023
1 parent c09ed84 commit 3f17e85
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 54 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export("%>%")
export(Referential)
export(questionnaire)
export(run_app)
import(shiny)
import(shinydashboard)
Expand Down
46 changes: 46 additions & 0 deletions R/questionnaire.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# WARNING - Generated by {fusen} from /dev/flat_r6_questionnaire.Rmd: do not edit by hand

#' questionnaire class is a class to load, check and manipulate one or more than one XLSForm
#' @importFrom R6 R6Class
#'
#' @export

questionnaire <- R6::R6Class(classname = "questionnaire",
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
#' @importFrom stats setNames
#'
#' @return named list
#' @examples
#' ref <- questionnaire$new(
#' path = system.file("household_survey_americas.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
}
),
private = list(

)
)
52 changes: 7 additions & 45 deletions dev/flat_r6_questionnaire.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ questionnaire <- R6::R6Class(classname = "questionnaire",
#' @param path path to the xlsForm
#'
#' @importFrom readxl excel_sheets read_xlsx
#' @importFrom stats setNames
#'
#' @return named list
initialize = function(path){
Expand All @@ -60,17 +61,6 @@ questionnaire <- R6::R6Class(classname = "questionnaire",
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(
Expand Down Expand Up @@ -121,7 +111,7 @@ test_that("r6_questionnaire works", {
```


# Utils for xlsx
# Utils for questionnaire

```{r development-utils, eval = FALSE}
Expand All @@ -131,42 +121,14 @@ test_that("r6_questionnaire works", {
```


```{r function-utils_xlsform}
assign(
"names_sheets",
c("survey",
"choices",
"Indicator",
"Indicator_survey",
"indicator_choices",
"indicator_population",
"indicator_dissagregation",
"country_language"
),
envir = survey_designer)
#' 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 function-utils_questionnaire}
```


```{r tests-utils_xlsform}
test_that("utils_xlsform works", {
```{r tests-utils_questionnaire}
test_that("utils_questionnaire works", {
})
Expand Down
85 changes: 85 additions & 0 deletions man/questionnaire.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/testthat/test-has_variables_for_indicators.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# WARNING - Generated by {fusen} from /dev/flat_r6_referential.Rmd: do not edit by hand

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"))
})
20 changes: 20 additions & 0 deletions tests/testthat/test-names_of_sheet.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# WARNING - Generated by {fusen} from /dev/flat_r6_referential.Rmd: do not edit by hand

test_that("utils_xlsform works", {

expect_true(inherits(get_groups, "function"))

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")

})
22 changes: 22 additions & 0 deletions tests/testthat/test-questionnaire.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# WARNING - Generated by {fusen} from /dev/flat_r6_questionnaire.Rmd: do not edit by hand

test_that("r6_questionnaire works", {
ref <- questionnaire$new(
path = system.file("household_survey_americas.xlsx", package = "surveyDesigner")
)

expect_true( inherits(ref, "R6") )

expect_error(
questionnaire$new(
path = "not_good_sheet.xlsx"
)
)

expect_error(
questionnaire$new(
path = "wt_xlsform_in_survey.xlsx"
)
)

})
6 changes: 6 additions & 0 deletions tests/testthat/test-utils-for-questionnaire.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# WARNING - Generated by {fusen} from /dev/flat_r6_questionnaire.Rmd: do not edit by hand

test_that("utils_questionnaire works", {


})
56 changes: 56 additions & 0 deletions vignettes/class-r6-for-the-questionnaire.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
title: "Class R6 for the questionnaire"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{class-r6-for-the-questionnaire}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```

```{r setup}
library(surveyDesigner)
```

<!-- WARNING - This vignette is generated by {fusen} from /dev/flat_r6_questionnaire.Rmd: do not edit by hand -->

# r6_Questionnaire

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 examples-r6_questionnaire}
ref <- questionnaire$new(
path = system.file("household_survey_americas.xlsx", package = "surveyDesigner")
)
head(ref$data$survey)
# Example by groups
ref$by_groups$group_intro
```

# Utils for questionnaire









24 changes: 15 additions & 9 deletions vignettes/class-r6-for-the-referential.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ library(surveyDesigner)

# 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
Expand All @@ -40,26 +52,20 @@ ref$by_groups$group_intro

# 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









## Utilities in relation with indicators mapping


Expand Down

0 comments on commit 3f17e85

Please sign in to comment.