Skip to content

Commit

Permalink
rev
Browse files Browse the repository at this point in the history
  • Loading branch information
Edouard-Legoupil committed Dec 5, 2022
1 parent 9dac60d commit 2e413ed
Show file tree
Hide file tree
Showing 22 changed files with 653 additions and 394 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ export(plot_text)
export(template_1_exploration)
importFrom(SnowballC,wordStem)
importFrom(data.table,":=")
importFrom(dplyr,distinct)
importFrom(dplyr,everything)
importFrom(dplyr,funs)
importFrom(dplyr,if_any)
importFrom(dplyr,mutate)
importFrom(dplyr,mutate_all)
importFrom(ggplot2,aes)
importFrom(ggplot2,as_labeller)
Expand Down Expand Up @@ -63,6 +65,7 @@ importFrom(shiny,runApp)
importFrom(shiny,shinyOptions)
importFrom(stats,setNames)
importFrom(tidyselect,all_of)
importFrom(tidyselect,everything)
importFrom(tidyselect,matches)
importFrom(tidyselect,where)
importFrom(tm,Corpus)
Expand Down
2 changes: 1 addition & 1 deletion R/kobo_clean.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ kobo_clean <- function(datalist,
indicatoradd = NULL) {

# 1 - load the variables,
var <- as.data.frame(dico[[1]])
var <- as.data.frame(dico[["variables"]])
# 2 - append the one from inidcatoradd if any,

# 3 - apply the indicator, i.e. do the calculation,
Expand Down
8 changes: 4 additions & 4 deletions R/kobo_cruncher.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@ kobo_cruncher <- function(datalist = datalist,
if( is.null(datasource)) {datasource <- as.character( dico[[3]]$form_title ) }

questions <- as.data.frame(dico[[4]])
disaggregation <- c(as.data.frame(dico[[1]]) |>
disaggregation <- c(as.data.frame(dico[["variables"]]) |>
dplyr::filter( !(is.na(disaggregation))) |>
dplyr::pull(name) )

correlate <- c(as.data.frame(dico[[1]]) |>
correlate <- c(as.data.frame(dico[["variables"]]) |>
dplyr::filter( !(is.na(correlate))) |>
dplyr::pull(name))


## If disaggregation not included in the function argument then use what's set up in the analysis plan
if ( length(disaggregation) == 1 ) {
if ( disaggregation == "" ) {
disaggregation <- as.data.frame(dico[[1]]) |>
disaggregation <- as.data.frame(dico[["variables"]]) |>
dplyr::filter( !(is.na(disaggregation))) |>
dplyr::pull(name) } }


# If correlate not included in the function argument then use what's set up in the analysis plan
if (length(correlate) ==1) {
if (correlate == "") { correlate <- as.data.frame(dico[[1]])|>
if (correlate == "") { correlate <- as.data.frame(dico[["variables"]])|>
dplyr::filter( !(is.na(correlate))) |>
dplyr::pull(name) }}

Expand Down
68 changes: 26 additions & 42 deletions R/kobo_dico.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
#' @examples
#' dico <- kobo_dico( xlsformpath = system.file("sample_xlsform.xlsx", package = "kobocruncher") )
#' # Survey
#' questions <- as.data.frame(dico[[1]])
#' questions <- as.data.frame(dico[["variables"]])
#' knitr::kable(utils::head(questions, 10))
#' # Choices
#' responses <- as.data.frame(dico[[2]])
#' responses <- as.data.frame(dico[["modalities"]])
#' knitr::kable(utils::head(responses, 10))
#' # Settings
#' metadata <- as.data.frame(dico[[3]])
#' metadata <- as.data.frame(dico[["settings"]])
#' knitr::kable(utils::head(metadata, 10))
#' # Report ToC
#' toc <- as.data.frame(dico[[4]])
#' toc <- as.data.frame(dico[["plan"]])
#' knitr::kable(utils::head(toc, 10))
#' # Indicator
#' indicator <- as.data.frame(dico[[5]])
#' indicator <- as.data.frame(dico[["indicator"]])
#' knitr::kable(utils::head(indicator, 10))
kobo_dico <- function(xlsformpath) {
survey <- readxl::read_excel(xlsformpath,
Expand Down Expand Up @@ -91,40 +91,7 @@ kobo_dico <- function(xlsformpath) {

## Fix when we have calculate variable - either numeric or select_one

## Add dataframe number
## Counter..
variables$dataframenew <- 0
for (i in 2:nrow(variables)){
#i <- 10
#cat( variables[i, c("repeatvar")] )
if (variables[i, c("repeatvar")] != variables[i-1, c("repeatvar")] &
variables[i, c("repeatvar")] !="" ) {
variables[i, c("dataframenew")] <- 1
} else{
variables[i, c("dataframenew")] <- 0
}
}
## Counter..
variables$dataframecount <- 1
for (i in 2:nrow(variables)){
if (variables[i, c("repeatvar")] != "") {
variables[i, c("dataframecount")] <- variables[i-1, c("dataframecount")] + variables[i, c("dataframenew")]
} else{
variables[i, c("dataframecount")] <- variables[i-1, c("dataframecount")]
}

}
## Framenumber
variables$dataframe <- 1
for (i in 2:nrow(variables)){
if (variables[i, c("repeatvar")] != "") {
variables[i, c("dataframe")] <- variables[i, c("dataframecount")]
} else{
variables[i, c("dataframe")] <- 1
}
}
variables$dataframecount <- NULL
variables$dataframenew <- NULL


if ("chapter" %in% colnames(variables)) { } else { variables$chapter <- NA }
if ("subchapter" %in% colnames( variables)) { } else { variables$subchapter <- NA }
Expand Down Expand Up @@ -213,7 +180,7 @@ kobo_dico <- function(xlsformpath) {
name = character(),
label = character(),
hint = character(),
dataframe = character(),
repeatvar = character(),
calculation = character(),
chapter = character(),
subchapter = character(),
Expand All @@ -232,7 +199,7 @@ kobo_dico <- function(xlsformpath) {
if ("name" %in% colnames(indicator)) { } else { indicator$name <- "" }
if ("label" %in% colnames(indicator)) { } else { indicator$label <- "" }
if ("hint" %in% colnames(indicator)) { } else { indicator$hint <- "" }
if ("dataframe" %in% colnames(indicator)) { } else { indicator$dataframe <- "" }
if ("repeatvar" %in% colnames(indicator)) { } else { indicator$repeatvar <- "" }
if ("calculation" %in% colnames(indicator)) { } else { indicator$calculation <- "" }
if ("chapter" %in% colnames(indicator)) { } else { indicator$chapter <- "" }
if ("subchapter" %in% colnames(indicator)) { } else { indicator$subchapter <- "" }
Expand All @@ -245,7 +212,7 @@ kobo_dico <- function(xlsformpath) {
if ("mappoly" %in% colnames(indicator)) { } else { indicator$mappoly <- ""}

indicator <- indicator[ ,c("type","name","label", "hint",
"dataframe", "calculation",
"repeatvar", "calculation",
"chapter","subchapter", "disaggregation", "correlate",
"cluster", "predict", "score", "mappoint", "mappoly")]

Expand All @@ -258,6 +225,23 @@ kobo_dico <- function(xlsformpath) {
indicator = indicator)
class(dico) <- "kobodico" # assigns a "kobodico" class to the list. Helpful for later on.
## Build dico object as list with both variables and modalities


#cat("\n******************** Summary of the Analysis Plan *********************\n \n")
## get a summary of what we have to inform user
## Check Variable lenght
# if( length(survey$label) )
## Do we have chapters? If yes how many?
## Do we have anonymisation instructions? If yes, is it correctly set? How many direct identifiers, key variables, sensitive variables
## Do we have cleaning instructions? Are they set up for the select_ variable
## Do we have disaggregation instructions (yes)? -- Should Are they set up for the select_one variable
## Do we have correlate instructions (yes)? Are they set up for the select_one variable
## Do we have cluster instructions (yes)? Are they set up for the select_one variable
## Do we have predict instructions (one unique target, at least 2 predictor)? Are they set up for the select_one variable
## Do we have mappoint, mappoly (yes)? Are they set up for the select_one variable



return(dico )
}

2 changes: 1 addition & 1 deletion R/kobo_frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ kobo_frame <- function(datalist,
var) {

## Identify the right frame number
dataframen <- as.data.frame(dico[[1]]) |>
dataframen <- as.data.frame(dico[["variables"]]) |>
dplyr::filter(name == var) |>
dplyr::pull(repeatvar)
## Pull the data
Expand Down
Loading

0 comments on commit 2e413ed

Please sign in to comment.