Skip to content

Commit

Permalink
add ridl connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Edouard-Legoupil committed Oct 24, 2023
1 parent 4c60a66 commit c8fd06a
Show file tree
Hide file tree
Showing 8 changed files with 453 additions and 108 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export(template_1_exploration)
import(dplyr)
import(ggplot2)
import(golem)
import(kobocruncher)
import(riddle)
import(shiny)
import(shinydashboard)
Expand Down
4 changes: 3 additions & 1 deletion R/app_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
app_server <- function(input, output, session) {

## add a reactive value object to pass by elements between objects
AppReactiveValue <- reactiveValues()
AppReactiveValue <- reactiveValues(
showridl = FALSE
)
# pins::board_register() # connect to pin board if needed
callModule(mod_home_server, "home_ui_1")
callModule(mod_document_server, "document_ui_1", AppReactiveValue)
Expand Down
1 change: 1 addition & 0 deletions R/body.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
body <- function() {
shinydashboard::dashboardBody(
unhcrshiny::theme_shinydashboard_unhcr(),
golem::activate_js(),
tags$head(
tags$script(src = "custom.js")
),
Expand Down
11 changes: 7 additions & 4 deletions R/header.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
header <- function() {
shinydashboard::dashboardHeader(
title = tagList(
span(class = 'logo-lg',a("kobocruncher",style="color:white !important",href='https://rstudio.unhcr.org/kobocruncher')),
) )

shinydashboard::dashboardHeader( title = "KoboCruncher" )

# shinydashboard::dashboardHeader(
# title = tagList(
# span(class = 'logo-lg',a("kobocruncher",style="color:white !important",href='https://rstudio.unhcr.org/kobocruncher')),
# ) )
}
187 changes: 174 additions & 13 deletions R/mod_configure.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod_configure_ui <- function(id) {
width = 12,
br(),
p("First upload your form and set up the language you would like to use
for the analysis from within your form")
for the analysis from within your form. Then add the data")
)
) ,

Expand All @@ -30,7 +30,7 @@ mod_configure_ui <- function(id) {


shinydashboard::box(
title = "Iterate ",
title = "Initial Setting ",
# status = "primary",
status = "info",
solidHeader = FALSE,
Expand All @@ -40,10 +40,26 @@ mod_configure_ui <- function(id) {
fluidRow(
column(
width = 6,
h3("Form"),
div(
id = ns("show_ridl2"),
selectInput( inputId = ns("ridlform"),
label = "Confirm the attachment that contains the form
or an already extended version of the form",
choice = c("Waiting for selected project..."=".."),
width = "100%" ),

fileInput(inputId = ns("xlsform"),
label = "Load your XlsForm",
multiple = F),
actionButton( inputId = ns("pull3"),
label = " Pull in session!",
icon = icon("upload"),
width = "100%" )
),
div(
id = ns("noshow_ridl2"),
fileInput(inputId = ns("xlsform"),
label = "Load your XlsForm",
multiple = F)
),

selectInput(inputId = ns("language"),
label = " Select Language to use from within the form",
Expand All @@ -54,18 +70,31 @@ mod_configure_ui <- function(id) {
"Arabic (ar)" = "Arabic (ar)",
"Portuguese (pt)"= "Portuguese (pt)"),
selected = NULL,
width = '400px'),


width = "100%" ),
),

column(
width = 6,
downloadButton(ns("downloadform"),
"Download back your extended form"),
hr(),
"You can work offline on the extended form and re-upload it to
regenerate a new exploration report"
h3("Data"),
div(
id = ns("show_ridl3"),

selectInput( inputId = ns("ridldata"),
label = "Confirm the attachment that contains the right data version",
choice = c("Waiting for selected project..."=".."),
width = "100%" ) ,
actionButton( inputId = ns("pull4"),
label = " Pull in session!",
icon = icon("upload"),
width = "100%" )
),
div(
id = ns("noshow_ridl3"),
fileInput(inputId = ns("dataupload"),
label = "Load your data",
multiple = F,
width = "100%" )
)
)
)
)
Expand All @@ -76,13 +105,94 @@ mod_configure_ui <- function(id) {
#' Module Server
#' @noRd
#' @import shiny
#' @import golem
#' @import tidyverse
#' @importFrom XlsFormUtil fct_xlsfrom_language
#' @keywords internal

mod_configure_server <- function(input, output, session, AppReactiveValue) {
ns <- session$ns

## Manage visibility for RIDL mode....
observeEvent(AppReactiveValue$showridl, {
if(isTRUE(AppReactiveValue$showridl)) {
golem::invoke_js("show", paste0("#", ns("show_ridl2")))
golem::invoke_js("hide", paste0("#", ns("noshow_ridl2")))
golem::invoke_js("show", paste0("#", ns("show_ridl3")))
golem::invoke_js("hide", paste0("#", ns("noshow_ridl3")))
} else {
golem::invoke_js("hide", paste0("#", ns("show_ridl2")))
golem::invoke_js("show", paste0("#", ns("noshow_ridl2")))
golem::invoke_js("hide", paste0("#", ns("show_ridl3")))
golem::invoke_js("show", paste0("#", ns("noshow_ridl3")))
}
})




observeEvent(input$dataupload,{
req(input$dataupload)
message("Please upload a file")
AppReactiveValue$datauploadpath <- input$dataupload$datapath
AppReactiveValue$thistempfolder <- dirname(AppReactiveValue$datauploadpath)
AppReactiveValue$datauploadname <- input$dataupload$name

## Create a sub folder data-raw and paste data there
dir.create(file.path(AppReactiveValue$thistempfolder, "data-raw"), showWarnings = FALSE)
file.copy( AppReactiveValue$datauploadpath,
paste0(AppReactiveValue$thistempfolder,
"/data-raw/",
# fs::path_file(AppReactiveValue$datauploadpath)),
AppReactiveValue$datauploadname),
overwrite = TRUE)
})



observeEvent(input$ridldata, {
AppReactiveValue$ridldata <- input$ridldata
})

observeEvent(input$pull4, {
## some message for user...
data_message <- utils::capture.output({
### So let's fetch the resource and create the corresponding reactive objects
# for the rest of the flow...

showModal(modalDialog("Please wait, pulling all the files from the server at the moment...", footer=NULL))

## now the data
req(AppReactiveValue$ridldata)
ridldata <- tempfile()
resource_fetch(url = AppReactiveValue$ridldata,
path = ridldata)
AppReactiveValue$datalist <- kobocruncher::kobo_data(datapath = ridldata)

removeModal()

}, type = "message")

if(is.null(AppReactiveValue$datalist )){
# not successful
shinyWidgets::sendSweetAlert(
session = session,
title = "Problem with Data",
text = "Please check your access rights...",
type = "warning"
)
} else {

shinyWidgets::sendSweetAlert(
session = session,
title = "You are done!",
text = paste0("Session loaded: the dataset includes ",
nrow(AppReactiveValue$datalist$main),
" records"),
type = "success" )
}
})


observeEvent(input$language, {
AppReactiveValue$language <- input$language
Expand Down Expand Up @@ -133,6 +243,57 @@ mod_configure_server <- function(input, output, session, AppReactiveValue) {
content <- function(file) { file.copy( AppReactiveValue$expandedform , file)}
)

observeEvent(input$ridlform, {
AppReactiveValue$ridlform <- input$ridlform
})

observeEvent(input$pull3, {
## some message for user...
data_message <- utils::capture.output({
### So let's fetch the resource and create the corresponding reactive objects
# for the rest of the flow...

showModal(modalDialog("Please wait, pulling all the files from the server at the moment...", footer=NULL))

req(AppReactiveValue$ridlform)
ridlformfile <- tempfile()
riddle::resource_fetch(url = AppReactiveValue$ridlform,
path = ridlformfile)

## Now let's load with koboloader
kobocruncher::kobo_prepare_form(xlsformpath = ridlformfile,
label_language = NULL,
xlsformpathout = ridlformfile )

## Let's extract the analysis plan from the xlsform - or extend the current one
AppReactiveValue$dico <- kobocruncher::kobo_dico(xlsformpath = ridlformfile)


removeModal()

}, type = "message")

if(is.null(AppReactiveValue$dico )){
# not successful
shinyWidgets::sendSweetAlert(
session = session,
title = "Problem with Form",
text = "Please check your access rights...",
type = "warning"
)
} else {

shinyWidgets::sendSweetAlert(
session = session,
title = "You are done!",
text = paste0("Session loaded: the form includes ",
nrow(AppReactiveValue$dico$variables),
" questions"),
type = "success" )
}
})


}

## copy to body.R
Expand Down
Loading

0 comments on commit c8fd06a

Please sign in to comment.