Skip to content

Commit

Permalink
First semi-working version
Browse files Browse the repository at this point in the history
  • Loading branch information
Edouard-Legoupil committed Nov 13, 2023
1 parent 28dfeff commit 8803b2d
Show file tree
Hide file tree
Showing 258 changed files with 17,573 additions and 5,935 deletions.
10 changes: 5 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ Imports:
ggtext,
glue,
golem,
grid,
janitor,
kobocruncher,
labelled,
openxlsx2,
plyr,
purrr,
readxl,
Expand All @@ -39,6 +41,7 @@ Imports:
sjlabelled,
sjmisc,
stats,
stringdist,
stringr,
sysfonts,
systemfonts,
Expand All @@ -50,13 +53,10 @@ Imports:
utils,
withr
Suggests:
here,
knitr,
openxlsx,
testthat
knitr
VignetteBuilder:
knitr
Remotes:
Remotes:
edouard-legoupil/kobocruncher,
edouard-legoupil/riddle,
edouard-legoupil/unhcrshiny,
Expand Down
38 changes: 29 additions & 9 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(fct_build_map)
export(fct_build_requirement)
export(fct_check_map)
export(fct_compass_table)
export(fct_get_all_variable_names)
Expand All @@ -9,17 +11,39 @@ export(fct_plot_rbm_sdg)
export(fct_re_map)
export(fct_var_dummy)
export(fct_var_mapping)
export(impact2_3)
export(impact3_2a)
export(impact3_2b)
export(impact3_3)
export(impact_2_2)
export(inter_drinkingwater)
export(inter_electricity)
export(inter_healthcare)
export(inter_shelter)
export(outcome10_1)
export(outcome10_2)
export(outcome12_1)
export(outcome12_2)
export(outcome13_1)
export(outcome13_2)
export(outcome13_3)
export(outcome14_1)
export(outcome16_1)
export(outcome16_2)
export(outcome1_2)
export(outcome1_3)
export(outcome4_1)
export(outcome4_2)
export(outcome5_2)
export(outcome8_2)
export(outcome9_1)
export(outcome9_2)
export(run_app)
export(var_dummy)
import(cli)
import(dplyr)
import(ggplot2)
import(golem)
import(readxl)
import(refugees)
import(shiny)
import(shinydashboard)
import(stringdist)
import(tidyverse)
importFrom(SDGsR,get_indicator)
importFrom(cli,cli_alert_info)
Expand All @@ -33,10 +57,6 @@ importFrom(dplyr,rename)
importFrom(dplyr,row_number)
importFrom(dplyr,tibble)
importFrom(ggforce,geom_arc_bar)
importFrom(ggplot2,aes)
importFrom(ggplot2,expansion)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,labs)
importFrom(ggtext,geom_richtext)
importFrom(golem,activate_js)
importFrom(golem,add_resource_path)
Expand Down
145 changes: 145 additions & 0 deletions R/fct_build_map.R
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)
}
153 changes: 153 additions & 0 deletions R/fct_build_requirement.R
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)

}
Loading

0 comments on commit 8803b2d

Please sign in to comment.