-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28dfeff
commit 8803b2d
Showing
258 changed files
with
17,573 additions
and
5,935 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# WARNING - Generated by {fusen} from dev/utilities.Rmd: do not edit by hand | ||
|
||
#' fct_build_map | ||
#' | ||
#' Quick helper to reformat the data from the mapping file into the mapping file | ||
#' to use with each indicator calculation. T | ||
#' | ||
#' his needs to be done in two steps: | ||
#' one for `ind` for individual level and | ||
#' one for `main` for household level | ||
#' | ||
#' @param mappingfile full path to the xlsx file with the revise variable mapping generated with `fct_var_mapping()` | ||
#' | ||
#' @param IndicatorRequirementFile path to the file where the standard mapping is depending on form version | ||
#' | ||
#' @param thisMeasureLevel can be `ind` for individual level or `main` for household level | ||
#' | ||
#' @import readxl | ||
#' @import dplyr | ||
#' | ||
#' @return a list | ||
#' | ||
#' @export | ||
#' @examples | ||
#' mappingfile <- system.file("RMS_CAPI_v2_mapping.xlsx", | ||
#' package = "IndicatorCalc") | ||
#' | ||
#' IndicatorRequirementFile <- system.file("RMS_CAPI_v2_mapper.xlsx", | ||
#' package = "IndicatorCalc") | ||
#' | ||
#' mappermain <- fct_build_map(mappingfile = mappingfile, | ||
#' IndicatorRequirementFile = IndicatorRequirementFile, | ||
#' thisMeasureLevel = "main") | ||
#' | ||
#' mapperind <- fct_build_map(mappingfile = mappingfile, | ||
#' IndicatorRequirementFile = IndicatorRequirementFile, | ||
#' thisMeasureLevel = "ind") | ||
#' | ||
fct_build_map <- function(mappingfile, | ||
IndicatorRequirementFile, | ||
thisMeasureLevel){ | ||
|
||
IndicMap <- readxl::read_excel( IndicatorRequirementFile) | ||
## Merge the map... | ||
MeasureLevelQ <- IndicMap |> | ||
dplyr::select(MeasureLevel, QuestionVar) |> | ||
dplyr::filter(MeasureLevel == thisMeasureLevel)|> | ||
dplyr::distinct() | ||
|
||
revisemapIndic <- readxl::read_excel( mappingfile, | ||
sheet = "result_matchInd") | ||
|
||
revisemapIndic2 <- MeasureLevelQ |> | ||
dplyr::left_join(revisemapIndic, by = c("QuestionVar") ) |> | ||
dplyr::rename(#QuestionVar, | ||
#label, | ||
mappattern = "best_name") |> | ||
dplyr::select(QuestionVar, label, mappattern)|> | ||
dplyr::distinct() | ||
|
||
# names( IndicMap) | ||
# names(revisemapIndic2) | ||
# names(revisemapMod) | ||
|
||
|
||
revisemapMod <- readxl::read_excel( mappingfile, | ||
sheet = "result_matchMod") | ||
revisemapMod2 <- MeasureLevelQ |> | ||
dplyr::left_join(revisemapMod, by = c("QuestionVar") ) |> | ||
dplyr::rename(#variable = "QuestionVar", | ||
# label= "label_mod" , | ||
# standard = "name_mod" , | ||
map = "best_name") |> | ||
dplyr::select(QuestionVar, label_mod, name_mod, map) | ||
|
||
|
||
|
||
filemap <- tempfile() | ||
filemaplab <- tempfile() | ||
#filemap <- "dev/test1.R" | ||
if (file.exists(filemap)) file.remove(filemap) | ||
|
||
cat( paste0( "thismapper = list("), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " hierarchy = \"", thisMeasureLevel,"\","), file = filemap , sep = "\n", append = TRUE) | ||
|
||
|
||
## Mapping variables | ||
thisvar <- revisemapIndic2 |> | ||
dplyr::select(QuestionVar, label, mappattern) |> | ||
dplyr::distinct() | ||
cat( paste0( " variablemap = data.frame("), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " label = "), file = filemap , sep = "\n", append = TRUE) | ||
## Looping around labels | ||
dput( thisvar |> dplyr::pull(label) , file = filemaplab ) | ||
file_str <- paste(readLines(filemaplab), collapse="\n") | ||
cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " mappattern = "), file = filemap , sep = "\n", append = TRUE) | ||
## Looping around mappattern | ||
dput( thisvar |> dplyr::pull(mappattern) , file = filemaplab ) | ||
file_str <- paste(readLines(filemaplab), collapse="\n") | ||
cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " variable = "), file = filemap , sep = "\n", append = TRUE) | ||
## Looping around variable | ||
dput( thisvar |> dplyr::pull(QuestionVar), file = filemaplab ) | ||
file_str <- paste(readLines(filemaplab), collapse="\n") | ||
cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " ),"), file = filemap , sep = "\n", append = TRUE) | ||
|
||
## Mapping modality | ||
thismod <- revisemapMod2 |> | ||
dplyr::select(QuestionVar, label_mod, name_mod, map) |> | ||
dplyr::filter(! (is.null(name_mod)) ) |> | ||
dplyr::distinct() | ||
cat( paste0( " modalitymap = data.frame("), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " variable = "), file = filemap , sep = "\n", append = TRUE) | ||
## Looping around variable for modalities | ||
dput( thismod |> dplyr::pull(QuestionVar) , file = filemaplab ) | ||
file_str <- paste(readLines(filemaplab), collapse="\n") | ||
cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " label = "), file = filemap , sep = "\n", append = TRUE) | ||
## Looping around labels for modalities | ||
dput( thismod |> dplyr::pull(label_mod) , file = filemaplab ) | ||
file_str <- paste(readLines(filemaplab), collapse="\n") | ||
cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " map = "), file = filemap , sep = "\n", append = TRUE) | ||
## Looping around labels for modalities | ||
dput( thismod |> dplyr::pull(map) , file = filemaplab ) | ||
file_str <- paste(readLines(filemaplab), collapse="\n") | ||
cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " standard = "), file = filemap , sep = "\n", append = TRUE) | ||
## Looping around standard for modalities | ||
dput( thismod |> dplyr::pull(name_mod) , file = filemaplab ) | ||
file_str <- paste(readLines(filemaplab), collapse="\n") | ||
cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " ))"), file = filemap , sep = "\n", append = TRUE) | ||
|
||
source(filemap) | ||
|
||
return(thismapper) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
# WARNING - Generated by {fusen} from dev/utilities.Rmd: do not edit by hand | ||
|
||
#' fct_build_requirement | ||
#' | ||
#' Write in a file the variable requirement for each single indicator functions | ||
#' | ||
#' The function is used to conveniently keep the mapping between the excel | ||
#' tables used to show variable requirements for each indicator calculation | ||
#' | ||
#' It expects a specific table within the xlsform called `Indicator_to_question` | ||
|
||
#' @param xlsformpath path to the xlsform | ||
#' @return print in console | ||
#' | ||
#' @export | ||
#' @examples | ||
#' ## Write in dev/mapper.R each of the function parameters - as recorded in system.file("IndicMap.xlsx", package = "IndicatorCalc") | ||
#' xlsformpath <- system.file("RMS_CAPI_v2.xlsx", package = "IndicatorCalc") | ||
#' RMS_CAPI_v2_mapper <- fct_build_requirement(xlsformpath ) | ||
#' # openxlsx::write.xlsx(RMS_CAPI_v2_mapper, here::here("inst", "RMS_CAPI_v2_mapper.xlsx")) | ||
#' | ||
#' xlsformpath <- system.file("RMS_CAPI_v3.xlsx", package = "IndicatorCalc") | ||
#' RMS_CAPI_v3_mapper <- fct_build_requirement(xlsformpath ) | ||
#' # openxlsx::write.xlsx(RMS_CAPI_v3_mapper, here::here("inst", "RMS_CAPI_v3_mapper.xlsx")) | ||
#' | ||
#' ## CATI | ||
#' xlsformpath <- system.file("RMS_CATI_v0.xlsx", package = "IndicatorCalc") | ||
#' RMS_CATI_v0_mapper <- fct_build_requirement(xlsformpath ) | ||
#' # openxlsx::write.xlsx(RMS_CATI_v0_mapper, here::here("inst", "RMS_CATI_v0_mapper.xlsx")) | ||
#' | ||
#' xlsformpath <- system.file("RMS_CATI_v3.xlsx", package = "IndicatorCalc") | ||
#' RMS_CATI_v3_mapper <- fct_build_requirement(xlsformpath ) | ||
#' # openxlsx::write.xlsx(RMS_CATI_v3_mapper, here::here("inst", "RMS_CATI_v3_mapper.xlsx")) | ||
#' | ||
fct_build_requirement <- function(xlsformpath){ | ||
|
||
mappers <- readxl::read_excel(xlsformpath, | ||
sheet = "Indicator_to_question") |> | ||
dplyr::select(IndName, MeasureLevel, QuestionVar) | ||
|
||
dico <- kobocruncher::kobo_dico( xlsformpath ) | ||
|
||
# View(dico[["variables"]]) | ||
# names(dico[["variables"]]) | ||
|
||
mappers <- mappers |> | ||
dplyr::left_join( dico[["variables"]] |> | ||
dplyr::select(name, label, | ||
type, list_name, name_or), | ||
by= c("QuestionVar" = "name_or")) | ||
|
||
|
||
IndicMap <- mappers |> | ||
dplyr::left_join( dico[["modalities"]] |> | ||
dplyr::select(name, label, list_name ) |> | ||
dplyr::rename( name_mod = name, | ||
label_mod = label), | ||
by= c("list_name")) | ||
|
||
#writexl::write_xlsx(IndicMap, here::here( "inst", "IndicMap.xlsx")) | ||
# IndicMap <- readxl::read_excel( system.file("IndicMap.xlsx", | ||
# package = "IndicatorCalc")) | ||
|
||
## Below is the format we are trying to replicate | ||
|
||
# mapper = list( | ||
# hierarchy = "main", | ||
# variablemap = data.frame( | ||
# label = c("Does this household use anything for lighting?", | ||
# "What source of electricity is used most of the time in this household?"), | ||
# variable = c("LIGHT01", | ||
# "LIGHT03"), | ||
# mappattern = c("LIGHT01", | ||
# "LIGHT03") ), | ||
# modalitymap = data.frame( | ||
# variable = c( "LIGHT01", | ||
# "LIGHT03", "LIGHT03", "LIGHT03"), | ||
# label = c( "yes", | ||
# "No electricity in household", "Other, specify", "Don't know"), | ||
# standard = c( "1", | ||
# "1", "96", "98"), | ||
# map = c("yes", | ||
# "Noelec", "Other", "Dontknow") | ||
# ) | ||
# ) | ||
|
||
filemap <- tempfile() | ||
filemaplab <- tempfile() | ||
listind <- IndicMap |> dplyr::select(IndName) |> dplyr::distinct() |> dplyr::pull() | ||
if (file.exists(filemap)) file.remove(filemap) | ||
## Roving around indicators | ||
for ( thisInd in listind ) { | ||
# thisInd <- listind[1] | ||
cat( paste0("# --- ", thisInd, "\n"), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( "# -----------------------------\n"), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( "mapper = list("), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " hierarchy = \"", | ||
IndicMap |> | ||
dplyr::filter(IndName == thisInd) |> | ||
dplyr::select(MeasureLevel) |> | ||
dplyr::distinct() |> | ||
dplyr::pull(), | ||
"\","), file = filemap , sep = "\n", append = TRUE) | ||
## Mapping variables | ||
thisvar <- IndicMap |> | ||
dplyr::filter(IndName == thisInd) |> | ||
dplyr::select(QuestionVar, label) |> | ||
dplyr::distinct() | ||
cat( paste0( " variablemap = data.frame("), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " label = "), file = filemap , sep = "\n", append = TRUE) | ||
## Looping around labels | ||
dput( thisvar |> dplyr::pull(label) , file = filemaplab ) | ||
file_str <- paste(readLines(filemaplab), collapse="\n") | ||
cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " variable = "), file = filemap , sep = "\n", append = TRUE) | ||
## Looping around variable | ||
dput( thisvar |> dplyr::pull(QuestionVar), file = filemaplab ) | ||
file_str <- paste(readLines(filemaplab), collapse="\n") | ||
cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " ),"), file = filemap , sep = "\n", append = TRUE) | ||
## Mapping modality | ||
thismod <- IndicMap |> | ||
dplyr::filter(IndName == thisInd) |> | ||
dplyr::select(QuestionVar, label_mod, name_mod) |> | ||
dplyr::filter(! (is.null(name_mod)) ) |> | ||
dplyr::distinct() | ||
cat( paste0( " modalitymap = data.frame("), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " variable = "), file = filemap , sep = "\n", append = TRUE) | ||
## Looping around variable for modalities | ||
dput( thismod |> dplyr::pull(QuestionVar) , file = filemaplab ) | ||
file_str <- paste(readLines(filemaplab), collapse="\n") | ||
cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " label = "), file = filemap , sep = "\n", append = TRUE) | ||
## Looping around labels for modalities | ||
dput( thismod |> dplyr::pull(label_mod) , file = filemaplab ) | ||
file_str <- paste(readLines(filemaplab), collapse="\n") | ||
cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " standard = "), file = filemap , sep = "\n", append = TRUE) | ||
## Looping around standard for modalities | ||
dput( thismod |> dplyr::pull(name_mod) , file = filemaplab ) | ||
file_str <- paste(readLines(filemaplab), collapse="\n") | ||
cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( " ))"), file = filemap , sep = "\n", append = TRUE) | ||
cat( paste0( "# -----------------------------\n"), file = filemap , sep = "\n", append = TRUE) | ||
|
||
} | ||
cat(filemap) | ||
return(IndicMap) | ||
|
||
} |
Oops, something went wrong.