-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflat_r6_questionnaire.Rmd
181 lines (133 loc) · 4.25 KB
/
flat_r6_questionnaire.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
---
title: "questionnaire 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_Questionnaire
A questionnaire object creates a list of one or multiple [XlsForm](http://xlsform.org) together with a summary of the .
The
To get to a questionnaire,
```{r function-r6_questionnaire}
#' 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
#'
#' @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 <- questionnaire$new(
path = system.file("household_survey_americas.xlsx", package = "surveyDesigner")
)
```
```{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
```
```{r tests-r6_questionnaire}
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"
)
)
})
```
# Utils for xlsx
```{r development-utils, eval = FALSE}
```
```{r function-utils_xlsform}
survey_designer <- new.env()
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 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_questionnaire.Rmd",
vignette_name = "Class R6 for the questionnaire"
)
```