-
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
8332200
commit caf107f
Showing
18 changed files
with
1,084 additions
and
119 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
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 |
---|---|---|
@@ -1,6 +1,4 @@ | ||
header <- function() { | ||
shinydashboard::dashboardHeader( | ||
title = tagList( | ||
span(class = 'logo-lg',a("IndicatorCalc",style="color:white !important",href='https://rstudio.unhcr.org/IndicatorCalc')), | ||
) ) | ||
title = "IndicatorCalc") | ||
} |
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,219 @@ | ||
# WARNING - Generated by {fusen} from dev/utilities.Rmd: do not edit by hand | ||
|
||
#' kobo_dummy | ||
#' | ||
#' @description Automatically produce an dummy dataset in line with the structure | ||
#' of an XlsForm | ||
#' Making decisions about research design and analysis strategies is often difficult before data is collected, | ||
#' because it is hard to imagine the exact form data will take. | ||
#' This function helps imagine what data will look like before they collect it. | ||
#' samplesize is set per defautl at 500 records | ||
#' | ||
#' Supported Features: | ||
#' - Generate a data set | ||
#' - respects ODK structure "`relevant`" skip logic | ||
#' (Some advanced functionality such as "coalesce()" not covered) | ||
#' - respects "`constraint`" | ||
#' - respects "`repeat`" adds ID column to link hierarchical data based on "`repeat_count`" | ||
#' This function is a rewriting of | ||
#' https://unhcr.github.io/koboloadeR/docs/reference/kobo_dummy.html | ||
#' it also build on https://thinkr-open.github.io/fakir/ and | ||
#' https://docs.ropensci.org/charlatan/ | ||
#' | ||
#' @param form file path of the XlsForm | ||
#' @param n number of main records to be generated | ||
#' @param nrepeat max random number of repeat records to be generated | ||
#' when repeat_count is not mentionned | ||
#' @param file file as xlsx where to save to the | ||
#' | ||
#' @importFrom kobocruncher kobo_dico | ||
#' | ||
#' | ||
#' @return a data list with a series of dummy data | ||
#' | ||
#' @export | ||
#' @examples | ||
#' form <- system.file("RMSCAPI.xlsx", package = "IndicatorCalc") | ||
#' datalist <- kobo_dummy(form, | ||
#' n = 384, | ||
#' file = NULL) | ||
kobo_dummy <- function(form, | ||
n = 384, | ||
file){ | ||
|
||
dico <- kobocruncher::kobo_dico(xlsformpath = form) | ||
|
||
## Get the variables to extract | ||
frame <- "main" | ||
|
||
conf <- data.frame( | ||
name <- dico[["variables"]] |> | ||
dplyr::filter(repeatvar == frame) |> | ||
dplyr::select(name), | ||
|
||
# dico[["variables"]]|> | ||
# dplyr::filter(repeatvar == frame) |> | ||
# dplyr::distinct(type) | ||
type <- dico[["variables"]]|> | ||
dplyr::filter(repeatvar == frame) |> | ||
dplyr::select(type), | ||
|
||
## pulling list options... | ||
list_opt <- dico[["variables"]] |> | ||
dplyr::filter(repeatvar == frame) |> | ||
dplyr::select(list_name) , | ||
#dplyr::left_join(modal, by = c("list_name")) |> | ||
# dplyr::select( list_opt) | ||
|
||
|
||
constraint <- dico[["variables"]]|> | ||
dplyr::filter(repeatvar == frame) |> | ||
dplyr::select(constraint) | ||
) | ||
|
||
## We start with the generation of the main frame | ||
## Build the main table and initiate it an index..... | ||
main <- dplyr::tibble( | ||
index = paste0( "ID-", | ||
purrr::as_vector( | ||
purrr::map(n, sample(LETTERS, 4)) |> | ||
purrr::map(paste0, collapse = "")), | ||
"-", | ||
formatC(1:n, width = nchar(n) + 1, flag = "0") | ||
)) | ||
|
||
## then apply var_dummy interactively... | ||
for(i in (1:nrow(conf)) ) { | ||
# i <- 6 | ||
cat(paste0(i, "-", conf[i, c("type")], "-", conf[i, c("name")], "\n")) | ||
|
||
## manage specific case when list name is not defined in ccoices but pulled from data.. | ||
## in such case we replace type by select_one by text | ||
this.type <- conf[i, c("type")] | ||
this.name <- conf[i, c("name")] | ||
this.listname <- conf[i , c("list_name")] | ||
this.constraint = conf[i, c("constraint")] | ||
|
||
if( !(this.listname %in% c( dico[["modalities"]] |> dplyr::pull(list_name))) ) { | ||
this.listname <- NULL | ||
this.type <- "text" | ||
} | ||
|
||
main <- var_dummy( | ||
frame = main, | ||
name = this.name, | ||
type = this.type, | ||
list_opt = c(dico[["modalities"]] |> | ||
dplyr::filter(list_name == this.listname ) |> | ||
dplyr::pull(name) ), | ||
constraint = this.constraint ) | ||
} | ||
|
||
## Still struggling with purrr | ||
# test <- purrr::map( main, | ||
# name, | ||
# type, | ||
# list_opt$list_opt , | ||
# constraint , | ||
# var_dummy) | ||
|
||
## TODO -- apply the relevance statement... | ||
## define when the variable should exist based on other elements | ||
## Need to convert the xlsform statement into R syntax | ||
# relevant <- dico[["variables"]]|> | ||
# dplyr::filter(repeatvar == "main") |> | ||
# dplyr::pull(relevant) | ||
|
||
## replace \" by ' | ||
## replace '=' by '==" | ||
## replace '!==' by '!=" (to fix previsous replace) | ||
## replace '${' '}' with nothing.. | ||
## replace 'or' with '|'.. | ||
## replace 'and' with '&'.. | ||
|
||
## Now store in the list... | ||
datalist <- list ( "main" = main) | ||
|
||
## treat cases with repeat table.. ##### | ||
repeatvar <- dico[["variables"]] |> | ||
dplyr::select(repeatvar) |> | ||
dplyr::filter(repeatvar != "main") |> | ||
dplyr::distinct() |> | ||
dplyr::pull() | ||
## need to separate the different repeat elements wthin the form... | ||
for ( rep in repeatvar ) { | ||
|
||
confrep <- data.frame( | ||
name <- dico[["variables"]] |> | ||
dplyr::filter(repeatvar == rep) |> | ||
dplyr::select(name), | ||
|
||
# dico[["variables"]]|> | ||
# dplyr::filter(repeatvar == rep) |> | ||
# dplyr::distinct(type) | ||
type <- dico[["variables"]]|> | ||
dplyr::filter(repeatvar == rep) |> | ||
dplyr::select(type), | ||
|
||
## pulling list options... | ||
list_opt <- dico[["variables"]] |> | ||
dplyr::filter(repeatvar == rep) |> | ||
dplyr::select(list_name) , | ||
#dplyr::left_join(modal, by = c("list_name")) |> | ||
# dplyr::select( list_opt) | ||
|
||
|
||
constraint <- dico[["variables"]]|> | ||
dplyr::filter(repeatvar == rep) |> | ||
dplyr::select(constraint) | ||
) | ||
|
||
## We start with the generation of the main frame | ||
## Build the main table and initiate it an index..... | ||
repframe <- dplyr::tibble( | ||
index = paste0( "ID-", | ||
purrr::as_vector( | ||
purrr::map(n, sample(LETTERS, 4)) |> | ||
purrr::map(paste0, collapse = "")), | ||
"-", | ||
formatC(1:n, width = nchar(n) + 1, flag = "0") | ||
)) | ||
|
||
## then apply var_dummy interactively... | ||
for(i in (1:nrow(confrep)) ) { | ||
# i <- 6 | ||
cat(paste0(i, " in repeat -", rep, " ///", confrep[i, c("type")], "-", confrep[i, c("name")], "\n")) | ||
|
||
## manage specific case when list name is not defined in ccoices but pulled from data.. | ||
## in such case we replace type by select_one by text | ||
this.type <- confrep[i, c("type")] | ||
this.name <- confrep[i, c("name")] | ||
this.listname <- confrep[i , c("list_name")] | ||
this.constraint = confrep[i, c("constraint")] | ||
|
||
if( !(this.listname %in% c( dico[["modalities"]] |> dplyr::pull(list_name))) ) { | ||
this.listname <- NULL | ||
this.type <- "text" | ||
} | ||
|
||
repframe <- var_dummy( | ||
frame = repframe, | ||
name = this.name, | ||
type = this.type, | ||
list_opt = c(dico[["modalities"]] |> | ||
dplyr::filter(list_name == this.listname ) |> | ||
dplyr::pull(name) ), | ||
constraint = this.constraint ) | ||
} | ||
|
||
## TODO -- check if we have a `repeat_count` to apply limitation.. | ||
|
||
## append to the repeat | ||
datalist[[rep]] <- repframe | ||
} | ||
|
||
|
||
|
||
|
||
return(datalist) | ||
} |
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,60 @@ | ||
#' Module UI | ||
|
||
#' @title mod_apply_calculation_ui and mod_apply_calculation_server | ||
#' @description A shiny module. | ||
#' @description A shiny module. | ||
#' @param id,input,output,session Internal parameters for {shiny}. | ||
#' | ||
#' @noRd | ||
#' @import shiny | ||
#' @import shinydashboard | ||
#' @keywords internal | ||
|
||
mod_apply_calculation_ui <- function(id) { | ||
ns <- NS(id) | ||
tabItem( | ||
tabName = "apply_calculation", | ||
fluidRow( | ||
column( | ||
width = 12, | ||
h2('Now apply the calculations'), | ||
p("You can now apply the standard calculations") | ||
) | ||
), | ||
|
||
fluidRow( | ||
shinydashboard::box( | ||
title = "Module Step", | ||
# status = "primary", | ||
status = "info", | ||
solidHeader = FALSE, | ||
collapsible = TRUE, | ||
width = 12, | ||
"Content" | ||
## do not forget that all elements ID of the GUI needs to be called with ns("ID").... | ||
) | ||
|
||
) | ||
) | ||
} | ||
|
||
#' Module Server | ||
#' @noRd | ||
#' @import shiny | ||
#' @import tidyverse | ||
#' @keywords internal | ||
|
||
mod_apply_calculation_server <- function(input, output, session, AppReactiveValue) { | ||
ns <- session$ns | ||
## add here the server logic part of your module.... | ||
} | ||
|
||
## copy to body.R | ||
# mod_apply_calculation_ui("apply_calculation_ui_1") | ||
|
||
## copy to sidebar.R | ||
# shinydashboard::menuItem("displayName",tabName = "apply_calculation",icon = icon("user")) | ||
|
||
## and copy to app_server.R | ||
# callModule(mod_apply_calculation_server, "apply_calculation_ui_1", AppReactiveValue) | ||
|
Oops, something went wrong.