updated vignette and formatting

This commit is contained in:
Andreas Gammelgaard Damsbo 2024-11-18 16:26:10 +01:00
parent 0c3286cb2f
commit 4e7af7d01f
No known key found for this signature in database
5 changed files with 98 additions and 34 deletions

View File

@ -31,11 +31,13 @@ shiny_cast <- function(...) {
#' #'
#' @examples #' @examples
#' file_extension(list.files(here::here(""))[[2]])[[1]] #' file_extension(list.files(here::here(""))[[2]])[[1]]
#' file_extension(c("file.cd..ks","file")) #' file_extension(c("file.cd..ks", "file"))
file_extension <- function(filenames) { file_extension <- function(filenames) {
sub(pattern = "^(.*\\.|[^.]+)(?=[^.]*)", replacement = "", sub(
filenames, pattern = "^(.*\\.|[^.]+)(?=[^.]*)", replacement = "",
perl = TRUE) filenames,
perl = TRUE
)
} }
#' Flexible file import based on extension #' Flexible file import based on extension
@ -74,4 +76,3 @@ read_input <- function(file, consider.na = c("NA", '""', "")) {
df df
} }

View File

@ -1,4 +1,3 @@
library(REDCapCAST)
library(bslib) library(bslib)
library(shiny) library(shiny)
library(openxlsx2) library(openxlsx2)
@ -7,9 +6,14 @@ library(readODS)
library(readr) library(readr)
library(dplyr) library(dplyr)
library(here) library(here)
library(devtools)
if (!requireNamespace("REDCapCAST")){
devtools::install_github("agdamsbo/REDCapCAST", quiet = TRUE, upgrade = "never")
}
library(REDCapCAST)
server <- function(input, output, session) { server <- function(input, output, session) {
v <- shiny::reactiveValues( v <- shiny::reactiveValues(
file = NULL file = NULL
) )
@ -64,7 +68,7 @@ server <- function(input, output, session) {
output$downloadData <- shiny::downloadHandler( output$downloadData <- shiny::downloadHandler(
filename = "data_ready.csv", filename = "data_ready.csv",
content = function(file) { content = function(file) {
write.csv(purrr::pluck(dd(), "data"), file, row.names = FALSE,na = "") write.csv(purrr::pluck(dd(), "data"), file, row.names = FALSE, na = "")
} }
) )
@ -72,13 +76,13 @@ server <- function(input, output, session) {
output$downloadMeta <- shiny::downloadHandler( output$downloadMeta <- shiny::downloadHandler(
filename = "datadictionary_ready.csv", filename = "datadictionary_ready.csv",
content = function(file) { content = function(file) {
write.csv(purrr::pluck(dd(), "meta"), file, row.names = FALSE,na = "") write.csv(purrr::pluck(dd(), "meta"), file, row.names = FALSE, na = "")
} }
) )
# Downloadable .zip of instrument ---- # Downloadable .zip of instrument ----
output$downloadInstrument <- shiny::downloadHandler( output$downloadInstrument <- shiny::downloadHandler(
filename = paste0("REDCapCAST_instrument",Sys.Date(),".zip"), filename = paste0("REDCapCAST_instrument", Sys.Date(), ".zip"),
content = function(file) { content = function(file) {
export_redcap_instrument(purrr::pluck(dd(), "meta"), file) export_redcap_instrument(purrr::pluck(dd(), "meta"), file)
} }
@ -88,12 +92,15 @@ server <- function(input, output, session) {
output_staging$meta <- output_staging$data <- NA output_staging$meta <- output_staging$data <- NA
shiny::observeEvent(input$upload.meta,{ upload_meta() }) shiny::observeEvent(input$upload.meta, {
upload_meta()
})
shiny::observeEvent(input$upload.data,{ upload_data() }) shiny::observeEvent(input$upload.data, {
upload_data()
upload_meta <- function(){ })
upload_meta <- function() {
shiny::req(input$uri) shiny::req(input$uri)
shiny::req(input$api) shiny::req(input$api)
@ -102,11 +109,10 @@ server <- function(input, output, session) {
ds = purrr::pluck(dd(), "meta"), ds = purrr::pluck(dd(), "meta"),
redcap_uri = input$uri, redcap_uri = input$uri,
token = input$api token = input$api
)|> purrr::pluck("success") ) |> purrr::pluck("success")
} }
upload_data <- function(){ upload_data <- function() {
shiny::req(input$uri) shiny::req(input$uri)
shiny::req(input$api) shiny::req(input$api)
@ -122,4 +128,8 @@ server <- function(input, output, session) {
output$upload.data.print <- renderText(output_staging$data) output$upload.data.print <- renderText(output_staging$data)
# session$onSessionEnded(function() {
# # cat("Session Ended\n")
# unlink("www",recursive = TRUE)
# })
} }

View File

@ -19,20 +19,27 @@ ui <-
".ods" ".ods"
) )
), ),
shiny::helpText("Have a look at the preview panels to show download options."), shiny::actionButton(
inputId = "load_data",
label = "Load data",
icon = shiny::icon("circle-down")
),
shiny::helpText("Have a look at the preview panels to validate the data dictionary and imported data."),
# For some odd reason this only unfolds when the preview panel is shown.. # For some odd reason this only unfolds when the preview panel is shown..
# This has been solved by adding an arbitrary button to load data
shiny::conditionalPanel( shiny::conditionalPanel(
condition = "output.uploaded=='yes'", # condition = "output.uploaded=='yes'",
shiny::helpText("Below you can download the dataset formatted for upload and the condition = "input.load_data",
corresponding data dictionary for a new data base, if you want to upload manually."), # shiny::helpText("Below you can download the dataset formatted for upload and the
# corresponding data dictionary for a new data base, if you want to upload manually."),
# Button # Button
shiny::downloadButton("downloadData", "Download renamed data"), shiny::downloadButton(outputId = "downloadData", label = "Download renamed data"),
# Button # Button
shiny::downloadButton("downloadMeta", "Download data dictionary"), shiny::downloadButton(outputId = "downloadMeta", label = "Download data dictionary"),
# Button # Button
shiny::downloadButton("downloadInstrument", "Download as instrument"), shiny::downloadButton(outputId = "downloadInstrument", label = "Download as instrument"),
# Horizontal line ---- # Horizontal line ----
shiny::tags$hr(), shiny::tags$hr(),
@ -108,3 +115,4 @@ ui <-
) )
) )
) )

View File

@ -17,5 +17,5 @@ DEPRECATED Helper to import files correctly
} }
\examples{ \examples{
file_extension(list.files(here::here(""))[[2]])[[1]] file_extension(list.files(here::here(""))[[2]])[[1]]
file_extension(c("file.cd..ks","file")) file_extension(c("file.cd..ks", "file"))
} }

View File

@ -23,25 +23,70 @@ REDCapCAST::shiny_cast()
The app primarily wraps one function: `ds2dd_detailed()`. The app primarily wraps one function: `ds2dd_detailed()`.
```{r} ```{r}
REDCap_split( library(REDCapCAST)
ds <- REDCap_split(
records = redcapcast_data, records = redcapcast_data,
metadata = redcapcast_meta, metadata = redcapcast_meta,
forms = "all" forms = "all"
) |> ) |>
sanitize_split() |> sanitize_split() |>
redcap_wider() |> redcap_wider()
ds2dd_detailed()|> str(ds)
purrr::pluck("data") |>
readr::type_convert(
col_types = readr::cols(.default = readr::col_guess()))
``` ```
```{r}
ds|>
ds2dd_detailed()|>
purrr::pluck("data") |>
str()
```
```{r}
ds|>
ds2dd_detailed()|>
purrr::pluck("meta") |>
head(10)
```
Different data formats are accepted, which all mostly implements the `readr::col_guess()` functionality to parse column classes.
To ensure uniformity in data import this parsing has been implemented on its own to use with `ds2dd_detailed()` or any other data set for that matter:
```{r}
ds_parsed <- redcapcast_data |>
dplyr::mutate(dplyr::across(dplyr::everything(),as.character)) |>
parse_data()
str(ds_parsed)
```
It will ignore specified columns, which is neat for numeric-looking strings like cpr-with a leading 0:
```{r} ```{r}
redcapcast_data |> redcapcast_data |>
dplyr::mutate(dplyr::across(dplyr::everything(),as.character)) |> dplyr::mutate(dplyr::across(dplyr::everything(),as.character)) |>
readr::type_convert( parse_data(ignore.vars = c("record_id","cpr")) |>
col_types = readr::cols(.default = readr::col_guess())) str()
``` ```
```{r}
```
Column classes can be passed to `parse_data()`.
Making a few crude assumption for factorising data, `numchar2fct()` factorises numerical and character vectors based on a set threshold for unique values:
```{r}
mtcars |> str()
mtcars |>
numchar2fct(numeric.threshold = 6) |>
str()
```
```{r}
ds_parsed|>
numchar2fct(numeric.threshold = 2) |>
str()
```