2024-11-18 08:17:55 +01:00
|
|
|
library(bslib)
|
|
|
|
library(shiny)
|
|
|
|
library(openxlsx2)
|
|
|
|
library(haven)
|
|
|
|
library(readODS)
|
|
|
|
library(readr)
|
|
|
|
library(dplyr)
|
2024-11-18 16:26:10 +01:00
|
|
|
library(devtools)
|
2024-11-19 12:55:09 +01:00
|
|
|
if (!requireNamespace("REDCapCAST")) {
|
2024-11-18 16:26:10 +01:00
|
|
|
devtools::install_github("agdamsbo/REDCapCAST", quiet = TRUE, upgrade = "never")
|
|
|
|
}
|
|
|
|
library(REDCapCAST)
|
2024-11-18 08:17:55 +01:00
|
|
|
|
2024-02-26 20:32:26 +01:00
|
|
|
|
2024-11-18 16:26:10 +01:00
|
|
|
server <- function(input, output, session) {
|
2024-11-18 08:17:55 +01:00
|
|
|
v <- shiny::reactiveValues(
|
|
|
|
file = NULL
|
|
|
|
)
|
2024-11-15 20:42:25 +01:00
|
|
|
|
2024-02-26 20:32:26 +01:00
|
|
|
dat <- shiny::reactive({
|
|
|
|
shiny::req(input$ds)
|
|
|
|
|
2024-11-19 12:55:09 +01:00
|
|
|
out <- read_input(input$ds$datapath)
|
|
|
|
|
|
|
|
# Saves labels to reapply later
|
2024-11-20 12:41:48 +01:00
|
|
|
# labels <- lapply(out, get_attr)
|
2024-11-19 12:55:09 +01:00
|
|
|
|
|
|
|
out <- out |>
|
|
|
|
## Parses data with readr functions
|
|
|
|
parse_data() |>
|
|
|
|
## Converts logical to factor, which overwrites attributes
|
|
|
|
##
|
2024-11-20 12:10:33 +01:00
|
|
|
dplyr::mutate(dplyr::across(dplyr::where(is.logical), as_factor))
|
2024-11-19 12:55:09 +01:00
|
|
|
|
|
|
|
if (!is.null(input$factor_vars)) {
|
|
|
|
out <- out |>
|
|
|
|
dplyr::mutate(
|
|
|
|
dplyr::across(
|
|
|
|
dplyr::all_of(input$factor_vars),
|
2024-11-20 12:10:33 +01:00
|
|
|
as_factor
|
2024-11-19 12:55:09 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
# Old attributes are appended
|
2024-11-20 12:41:48 +01:00
|
|
|
# out <- purrr::imap(out,\(.x,.i){
|
|
|
|
# set_attr(.x,labels[[.i]])
|
|
|
|
# }) |>
|
|
|
|
# dplyr::bind_cols()
|
2024-11-19 12:55:09 +01:00
|
|
|
|
|
|
|
out
|
2024-02-26 20:32:26 +01:00
|
|
|
})
|
|
|
|
|
2024-11-18 08:17:55 +01:00
|
|
|
# getData <- reactive({
|
|
|
|
# if(is.null(input$ds$datapath)) return(NULL)
|
|
|
|
# })
|
|
|
|
# output$uploaded <- reactive({
|
|
|
|
# return(!is.null(getData()))
|
|
|
|
# })
|
|
|
|
|
2024-11-18 16:50:03 +01:00
|
|
|
dd <- shiny::reactive({
|
|
|
|
shiny::req(input$ds)
|
|
|
|
v$file <- "loaded"
|
2024-11-20 12:10:33 +01:00
|
|
|
ds2dd_detailed(
|
|
|
|
data = dat(),
|
|
|
|
add.auto.id = input$add_id=="yes"
|
|
|
|
)
|
2024-11-18 16:50:03 +01:00
|
|
|
})
|
2024-11-18 08:17:55 +01:00
|
|
|
|
|
|
|
output$uploaded <- shiny::reactive({
|
|
|
|
if (is.null(v$file)) {
|
|
|
|
"no"
|
|
|
|
} else {
|
|
|
|
"yes"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
shiny::outputOptions(output, "uploaded", suspendWhenHidden = FALSE)
|
|
|
|
|
2024-11-19 12:55:09 +01:00
|
|
|
output$factor_vars <- shiny::renderUI({
|
|
|
|
shiny::req(input$ds)
|
|
|
|
selectizeInput(
|
|
|
|
inputId = "factor_vars",
|
|
|
|
selected = colnames(dat())[sapply(dat(), is.factor)],
|
|
|
|
label = "Covariables to format as categorical",
|
|
|
|
choices = colnames(dat()),
|
|
|
|
multiple = TRUE
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2024-11-20 12:10:33 +01:00
|
|
|
## Specify ID if necessary
|
|
|
|
# output$id_var <- shiny::renderUI({
|
|
|
|
# shiny::req(input$ds)
|
|
|
|
# selectizeInput(
|
|
|
|
# inputId = "id_var",
|
|
|
|
# selected = colnames(dat())[1],
|
|
|
|
# label = "ID variable",
|
|
|
|
# choices = colnames(dat())[-match(colnames(dat()),input$factor_vars)],
|
|
|
|
# multiple = FALSE
|
|
|
|
# )
|
|
|
|
# })
|
|
|
|
|
2024-11-18 08:17:55 +01:00
|
|
|
output$data.tbl <- gt::render_gt(
|
2024-02-26 20:32:26 +01:00
|
|
|
dd() |>
|
2024-11-20 16:15:41 +01:00
|
|
|
cast_data_overview()
|
2024-11-18 08:17:55 +01:00
|
|
|
)
|
2024-02-26 20:32:26 +01:00
|
|
|
|
2024-11-18 08:17:55 +01:00
|
|
|
output$meta.tbl <- gt::render_gt(
|
2024-02-26 20:32:26 +01:00
|
|
|
dd() |>
|
2024-11-20 16:15:41 +01:00
|
|
|
cast_meta_overview()
|
2024-11-18 08:17:55 +01:00
|
|
|
)
|
2024-02-26 20:32:26 +01:00
|
|
|
|
|
|
|
# Downloadable csv of dataset ----
|
|
|
|
output$downloadData <- shiny::downloadHandler(
|
|
|
|
filename = "data_ready.csv",
|
|
|
|
content = function(file) {
|
2024-11-18 16:26:10 +01:00
|
|
|
write.csv(purrr::pluck(dd(), "data"), file, row.names = FALSE, na = "")
|
2024-02-26 20:32:26 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
# Downloadable csv of data dictionary ----
|
|
|
|
output$downloadMeta <- shiny::downloadHandler(
|
2024-11-19 12:55:09 +01:00
|
|
|
filename = paste0("REDCapCAST_DataDictionary_", Sys.Date(), ".csv"),
|
2024-02-26 20:32:26 +01:00
|
|
|
content = function(file) {
|
2024-11-18 16:26:10 +01:00
|
|
|
write.csv(purrr::pluck(dd(), "meta"), file, row.names = FALSE, na = "")
|
2024-02-26 20:32:26 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-10-24 11:37:40 +02:00
|
|
|
# Downloadable .zip of instrument ----
|
|
|
|
output$downloadInstrument <- shiny::downloadHandler(
|
2024-11-18 16:26:10 +01:00
|
|
|
filename = paste0("REDCapCAST_instrument", Sys.Date(), ".zip"),
|
2024-10-24 11:37:40 +02:00
|
|
|
content = function(file) {
|
2024-11-20 12:10:33 +01:00
|
|
|
export_redcap_instrument(purrr::pluck(dd(), "meta"),
|
|
|
|
file = file,
|
|
|
|
record.id = ifelse(input$add_id=="none",NA,names(dat())[1]))
|
2024-10-24 11:37:40 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-02-26 20:32:26 +01:00
|
|
|
output_staging <- shiny::reactiveValues()
|
2024-11-15 20:42:25 +01:00
|
|
|
|
2024-11-18 08:17:55 +01:00
|
|
|
output_staging$meta <- output_staging$data <- NA
|
2024-02-26 20:32:26 +01:00
|
|
|
|
2024-11-18 16:26:10 +01:00
|
|
|
shiny::observeEvent(input$upload.meta, {
|
|
|
|
upload_meta()
|
|
|
|
})
|
2024-02-26 20:32:26 +01:00
|
|
|
|
2024-11-18 16:26:10 +01:00
|
|
|
shiny::observeEvent(input$upload.data, {
|
|
|
|
upload_data()
|
|
|
|
})
|
2024-02-26 20:32:26 +01:00
|
|
|
|
2024-11-18 16:26:10 +01:00
|
|
|
upload_meta <- function() {
|
2024-02-26 20:32:26 +01:00
|
|
|
shiny::req(input$uri)
|
|
|
|
|
|
|
|
shiny::req(input$api)
|
|
|
|
|
|
|
|
output_staging$meta <- REDCapR::redcap_metadata_write(
|
|
|
|
ds = purrr::pluck(dd(), "meta"),
|
|
|
|
redcap_uri = input$uri,
|
|
|
|
token = input$api
|
2024-11-18 16:26:10 +01:00
|
|
|
) |> purrr::pluck("success")
|
2024-02-26 20:32:26 +01:00
|
|
|
}
|
|
|
|
|
2024-11-18 16:26:10 +01:00
|
|
|
upload_data <- function() {
|
2024-02-26 20:32:26 +01:00
|
|
|
shiny::req(input$uri)
|
|
|
|
|
|
|
|
shiny::req(input$api)
|
|
|
|
|
|
|
|
output_staging$data <- REDCapR::redcap_write(
|
|
|
|
ds = purrr::pluck(dd(), "data"),
|
|
|
|
redcap_uri = input$uri,
|
|
|
|
token = input$api
|
|
|
|
) |> purrr::pluck("success")
|
|
|
|
}
|
|
|
|
|
|
|
|
output$upload.meta.print <- renderText(output_staging$meta)
|
|
|
|
|
|
|
|
output$upload.data.print <- renderText(output_staging$data)
|
|
|
|
|
2024-11-18 16:26:10 +01:00
|
|
|
# session$onSessionEnded(function() {
|
|
|
|
# # cat("Session Ended\n")
|
|
|
|
# unlink("www",recursive = TRUE)
|
|
|
|
# })
|
2024-02-26 20:32:26 +01:00
|
|
|
}
|