Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1 as dev i would need to have an initial documented referential #13

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ vignettes/*.pdf
surveyDesigner.Rproj
!.github/*/*
inst/doc
rsconnect
29 changes: 14 additions & 15 deletions R/get_groups.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ survey_designer <- new.env()

assign(
"names_sheets",
c("survey",
c("referential_type",
"survey",
"choices",
"Indicator",
"Indicator_survey",
"indicator_choices",
"indicator",
"indicator_survey",
"indicator_choices",
"indicator_population",
"indicator_dissagregation",
"country_language"
"indicator_disaggregation"
),
envir = survey_designer)


#' Get groups form begin and end into a list with data and information
#'
#' @param data data form the survey sheet
#' @param data data from the survey sheet
#'
#' @importFrom purrr map2 set_names map
#' @importFrom dplyr slice filter
Expand Down Expand Up @@ -62,17 +62,16 @@ by_begin_end <- map2(begin_start, end_stop,

#' Get choices for one question
#'
#' @param data data of choices
#' @param indicator the name of the indicator for the question
#' @param lg language to use
#' @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(data, indicator, lg){
data %>%
filter(list_name == indicator) %>%
select(list_name, name, contains(lg))
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
Expand All @@ -81,7 +80,7 @@ get_choices_for_question <- function(data, indicator, lg){
#'
#' @noRd

is_a_xlsfrom <- function(data){
contains_groups <- function(data){
any(grepl(x = data[["type"]], pattern = 'begin_group|begin_repeat|end_group|end_repeat'))
}

Expand Down
12 changes: 6 additions & 6 deletions R/referential.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ Referential <- R6::R6Class(classname = "Referential",
public = list(
#' @description
#' read the xlsx for each sheet and return a named list
#' @param path path to the xlsForm
#' @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("household_survey_americas.xlsx", package = "surveyDesigner")
#' path = system.file("SurveyDesigner_Referential.xlsx", package = "surveyDesigner")
#' )
#'
#' head(ref$data$survey)
Expand All @@ -39,8 +39,8 @@ Referential <- R6::R6Class(classname = "Referential",
# 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")
if(!contains_groups(data$survey)){
stop("the sheet 'survey' doesn't includes groups")
}

self$data <- data
Expand All @@ -49,9 +49,9 @@ Referential <- R6::R6Class(classname = "Referential",

self$get_groups()
},
#' @field data named list for the xlsx file
#' @field data named list for the referential file
data = list(),
#' @field by_groups survey data separate by begin and end to manipulate data
#' @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),
Expand Down
57 changes: 29 additions & 28 deletions dev/flat_r6_referential.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "flat_r6_referential.Rmd empty"
title: "Class R6 for the referential"
output: html_document
editor_options:
chunk_output_type: console
Expand All @@ -20,7 +20,9 @@ library(purrr)

# r6_referential

With XlsForm, we can not filter as we want because the order inside the file is important. For me the first thing to do is to separate our file with begin and end group
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

```{r function-r6_referential}
#' Referential class is a class to load, check and manipulate the XLSForm
Expand All @@ -32,7 +34,7 @@ Referential <- R6::R6Class(classname = "Referential",
public = list(
#' @description
#' read the xlsx for each sheet and return a named list
#' @param path path to the xlsForm
#' @param path path to the file with the full referential
#'
#' @importFrom readxl excel_sheets read_xlsx
#'
Expand All @@ -53,8 +55,8 @@ Referential <- R6::R6Class(classname = "Referential",
# 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")
if(!contains_groups(data$survey)){
stop("the sheet 'survey' doesn't includes groups")
}

self$data <- data
Expand All @@ -63,9 +65,9 @@ Referential <- R6::R6Class(classname = "Referential",

self$get_groups()
},
#' @field data named list for the xlsx file
#' @field data named list for the referential file
data = list(),
#' @field by_groups survey data separate by begin and end to manipulate data
#' @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),
Expand All @@ -84,14 +86,14 @@ Referential <- R6::R6Class(classname = "Referential",

```{r development-test}
ref <- Referential$new(
path = system.file("household_survey_americas.xlsx", package = "surveyDesigner")
path = system.file("SurveyDesigner_Referential.xlsx", package = "surveyDesigner")
)
```


```{r examples-r6_referential}
ref <- Referential$new(
path = system.file("household_survey_americas.xlsx", package = "surveyDesigner")
path = system.file("SurveyDesigner_Referential.xlsx", package = "surveyDesigner")
)

head(ref$data$survey)
Expand All @@ -103,7 +105,7 @@ ref$by_groups$group_intro
```{r tests-r6_referential}
test_that("r6_referential works", {
ref <- Referential$new(
path = system.file("household_survey_americas.xlsx", package = "surveyDesigner")
path = system.file("SurveyDesigner_Referential.xlsx", package = "surveyDesigner")
)

expect_true( inherits(ref, "R6") )
Expand All @@ -124,7 +126,7 @@ test_that("r6_referential works", {
```


# Utils for xlsx
# Utils for referential manipulation

```{r development-utils, eval = FALSE}

Expand Down Expand Up @@ -168,21 +170,21 @@ survey_designer <- new.env()

assign(
"names_sheets",
c("survey",
c("referential_type",
"survey",
"choices",
"Indicator",
"Indicator_survey",
"indicator_choices",
"indicator",
"indicator_survey",
"indicator_choices",
"indicator_population",
"indicator_dissagregation",
"country_language"
"indicator_disaggregation"
),
envir = survey_designer)


#' Get groups form begin and end into a list with data and information
#'
#' @param data data form the survey sheet
#' @param data data from the survey sheet
#'
#' @importFrom purrr map2 set_names map
#' @importFrom dplyr slice filter
Expand Down Expand Up @@ -226,17 +228,16 @@ by_begin_end <- map2(begin_start, end_stop,

#' Get choices for one question
#'
#' @param data data of choices
#' @param indicator the name of the indicator for the question
#' @param lg language to use
#' @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(data, indicator, lg){
data %>%
filter(list_name == indicator) %>%
select(list_name, name, contains(lg))
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
Expand All @@ -245,7 +246,7 @@ get_choices_for_question <- function(data, indicator, lg){
#'
#' @noRd

is_a_xlsfrom <- function(data){
contains_groups <- function(data){
any(grepl(x = data[["type"]], pattern = 'begin_group|begin_repeat|end_group|end_repeat'))
}

Expand All @@ -270,7 +271,7 @@ test_that("utils_xlsform works", {
expect_true(inherits(get_groups, "function"))

ref <- Referential$new(
path = system.file("household_survey_americas.xlsx", package = "surveyDesigner")
path = system.file("SurveyDesigner_Referential.xlsx", package = "surveyDesigner")
)

result <- get_groups(ref$data$survey)
Expand All @@ -293,7 +294,7 @@ expect_type(get_chocies, "list")
# Execute in the console directly
fusen::inflate(
flat_file = "dev/flat_r6_referential.Rmd",
vignette_name = "Class R6 for the referencial"
vignette_name = "Class R6 for the referential"
)
```

Binary file added inst/SurveyDesigner_Referential.xlsx
Binary file not shown.
Binary file removed inst/household_survey_americas.xlsx
Binary file not shown.
10 changes: 5 additions & 5 deletions man/Referential.Rd

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

8 changes: 3 additions & 5 deletions man/get_choices_for_question.Rd

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

2 changes: 1 addition & 1 deletion man/get_groups.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-get_groups.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test_that("utils_xlsform works", {
expect_true(inherits(get_groups, "function"))

ref <- Referential$new(
path = system.file("household_survey_americas.xlsx", package = "surveyDesigner")
path = system.file("SurveyDesigner_Referential.xlsx", package = "surveyDesigner")
)

result <- get_groups(ref$data$survey)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-referential.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

expect_true( inherits(ref, "R6") )
Expand Down
Loading