Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: REDCapDM
Type: Package
Title: 'REDCap' Data Management
Version: 1.0.0
Version: 1.0.1
Authors@R: c(
person("João", "Carmezim", email = "jcarmezim@igtp.cat", role = c("aut", "cre")),
person("Pau", "Satorra", role = c("aut")),
Expand Down Expand Up @@ -33,7 +33,8 @@ Imports:
stringi,
cli,
forcats,
lifecycle
lifecycle,
magrittr
Suggests:
knitr,
rmarkdown,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export(rd_transform)
export(redcap_data)
import(cli)
importFrom(lifecycle,deprecated)
importFrom(magrittr,`%>%`)
importFrom(rlang,":=")
importFrom(rlang,.data)
importFrom(rlang,eval_tidy)
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# REDCapDM 1.0-1

## Bug fixes

- Fixed a bug in `rd_checkbox` that mis-evaluated the branching logic of certain checkboxes if they had previously been transformed with `rd_factor`

## New features

- Added a `sep` argument to the `redcap_data()` function, allowing users to specify the field separator when reading CSV dictionary files.

# REDCapDM 1.0-0

## Bug fixes
Expand Down
29 changes: 24 additions & 5 deletions R/rd_checkbox.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @param event_form Only applicable for longitudinal projects (presence of events). Event-to-form mapping for longitudinal projects.
#' @param checkbox_labels Character vector of length 2 for labels of unchecked/checked values. Default: `c("No", "Yes")`.
#' @param checkbox_names Logical. If `TRUE` (default), checkbox columns are renamed using choice labels.
#' @param na_logic Controls how missing values are set based on branching logic. Must be one of `"none"` (do nothing), `"missing"` (set to `NA` only when the logic evaluation is `NA`), or `"eval"` (set to `NA` when the logic evaluates to `FALSE`). Defaults to `"none"`.
#' @param na_logic Controls how missing values are set based on branching logic. Must be one of `"none"` (do nothing), `"missing"` (set to `NA` only when the logic evaluation is `NA`), or `"eval"` (set to `NA` when the logic evaluates to `FALSE`). Defaults to `"missing"`.
#'
#' @return A list with:
#' \describe{
Expand Down Expand Up @@ -49,7 +49,7 @@
#' @export
#' @importFrom stats setNames na.omit

rd_checkbox <- function(project = NULL, data = NULL, dic = NULL, event_form = NULL, checkbox_labels = c("No", "Yes"), checkbox_names = TRUE, na_logic = "none") {
rd_checkbox <- function(project = NULL, data = NULL, dic = NULL, event_form = NULL, checkbox_labels = c("No", "Yes"), checkbox_names = TRUE, na_logic = "missing") {
results <- NULL
rlogic_eval <- NULL

Expand Down Expand Up @@ -121,7 +121,7 @@ rd_checkbox <- function(project = NULL, data = NULL, dic = NULL, event_form = NU
}
}

# Update results with the this transformation
# Update results with the transformation
reason <- if (na_logic == "eval")
"when the logic isn't satisfied or it's missing"
else if (na_logic == "missing")
Expand Down Expand Up @@ -162,6 +162,12 @@ rd_checkbox <- function(project = NULL, data = NULL, dic = NULL, event_form = NU
warning("The project contains repeated instruments, and this function cannot accurately evaluate the branching logic of checkboxes in such cases.", call. = FALSE)
}

# Handle the fact that a rd_factor was performed before this function
if (length(var_check_factors) == 0 & all(!grepl("data\\$", dic$branching_logic_show_field_only_if))) {
warning(
"The checkboxes are already in factor form. To properly evaluate checkbox branching logic, please run `rd_dictionary()` first.", call. = FALSE
)
}

caption <- "Checkbox variables advisable to be reviewed"
review <- NULL
Expand Down Expand Up @@ -385,10 +391,23 @@ rd_checkbox <- function(project = NULL, data = NULL, dic = NULL, event_form = NU
branching_logic_show_field_only_if = stringr::str_replace_all(.data$branching_logic_show_field_only_if, replace2)
)

# Returning checkboxes to numeric version
# Returning checkboxes to numeric version & applying the same missing values to their factor version
if (length(var_check_factors) > 0) {

correspondence <- correspondence |>
dplyr::mutate(out_factor = paste0(out, ".factor"))

data <- data |>
dplyr::mutate(dplyr::across(correspondence$out, ~ as.numeric(.x)))
dplyr::mutate(
dplyr::across(dplyr::any_of(correspondence$out), as.numeric),
dplyr::across(dplyr::any_of(correspondence$out_factor), ~{

var_fac <- data[[dplyr::cur_column()]]
var_num <- data[[sub("\\.factor$", "", dplyr::cur_column())]]

dplyr::case_when(!is.na(var_num) ~ var_fac)
})
)
}

# Apply the labels to the data
Expand Down
3 changes: 3 additions & 0 deletions R/rd_query.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ rd_query <- function(project = NULL, variables = NA, expression = NA, negate = F
data <- as.data.frame(data)
dic <- as.data.frame(dic)

# Ensure variables is a character string
variables <- as.character(variables)

# Initialize the query structure
queries <- as.data.frame(matrix(ncol = 10, nrow = 0))
colnames(queries) <- c("Identifier", "DAG", "Event", "Instrument", "Field", "Repetition", "Description", "Query", "Code", "Link")
Expand Down
5 changes: 3 additions & 2 deletions R/redcap_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#' @param event_path Path to the event-form mapping file (CSV or XLSX) for longitudinal projects (downloadable via the `Designate Instruments for My Events` tab within the `Project Setup` section of REDCap).
#' @param uri REDCap API base URI (use with `token`).
#' @param token REDCap API token (use with `uri`).
#' @param sep Character string specifying the field separator for the exported dictionary CSV file. Defaul `","`.
#' @param filter_field Optional character vector of field names to request from the API.
#' @param survey_fields Logical; include survey-related fields when pulling via API. Default `FALSE`.
#'
Expand Down Expand Up @@ -59,7 +60,7 @@
#' @export
#' @importFrom stats setNames

redcap_data <- function(data_path = NA, dic_path = NA, event_path = NA, uri = NA, token = NA, filter_field = NULL, survey_fields = FALSE) {
redcap_data <- function(data_path = NA, dic_path = NA, event_path = NA, uri = NA, token = NA, sep = ",", filter_field = NULL, survey_fields = FALSE) {

event_form <- NULL

Expand Down Expand Up @@ -121,7 +122,7 @@ redcap_data <- function(data_path = NA, dic_path = NA, event_path = NA, uri = NA
dic <- openxlsx::read.xlsx(dic_path, colNames = FALSE, detectDates = TRUE, sheet = 1)
} else if (extension_dic == "csv") {
# Read CSV file
dic <- utils::read.csv(dic_path, encoding = "UTF-8", header = FALSE)
dic <- utils::read.csv(dic_path, encoding = "UTF-8", header = FALSE, sep = sep)
} else {
stop("Unsupported dictionary format. Only CSV and XLSX are supported.", call. = FALSE)
}
Expand Down
17 changes: 9 additions & 8 deletions R/utils-transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ split_event <- function(data,dic,event_form,which=NULL){
#' @param event_form Data frame containing the correspondence of each event with each form.
#' @param which Character string specifying a form if only data for that form is desired.
#' @param wide Logical indicating if the dataset should be returned in a wide format (`TRUE`) or long format (`FALSE`).
#' @importFrom magrittr `%>%`

split_form <- function(data, dic, event_form = NULL, which = NULL, wide=FALSE){

Expand All @@ -468,7 +469,7 @@ split_form <- function(data, dic, event_form = NULL, which = NULL, wide=FALSE){
if(longitudinal & is.null(event_form)){
stop("To split the data by form the event_form has to be provided in a longitudinal project", call. = FALSE)
}

#Check if the project has repeated instruments
if("redcap_repeat_instrument" %in% names(data)) {
repeat_instrument <- dplyr::case_when(
Expand Down Expand Up @@ -554,29 +555,29 @@ split_form <- function(data, dic, event_form = NULL, which = NULL, wide=FALSE){
dplyr::mutate(df = purrr::map(.data$vars, ~ data |>
dplyr::select(tidyselect::all_of(unique(c(basic_redcap_vars, .x))))))
}

if(repeat_instrument) {
form_check <- data %>%
dplyr::distinct(redcap_repeat_instrument, redcap_repeat_instrument.factor)
dplyr::distinct(.data$redcap_repeat_instrument, .data$redcap_repeat_instrument.factor)

ndata <- ndata %>%
dplyr::left_join(form_check, by = dplyr::join_by("form" == "redcap_repeat_instrument")) %>%
dplyr::relocate("form_factor" = "redcap_repeat_instrument.factor", .after = form) %>%
dplyr::mutate(df = purrr::map2(.data$form_factor, .data$df, ~ {
if (is.na(.x)) {
.y %>%
dplyr::filter(is.na(redcap_repeat_instrument.factor)) %>%
dplyr::filter(is.na(.data$redcap_repeat_instrument.factor)) %>%
dplyr::select(-dplyr::starts_with("redcap_repeat_instrument"))
} else {
.y %>%
dplyr::filter(redcap_repeat_instrument.factor == .x) %>%
dplyr::filter(.data$redcap_repeat_instrument.factor == .x) %>%
dplyr::mutate(redcap_repeat_instrument = redcap_repeat_instrument.factor) %>%
dplyr::select(-redcap_repeat_instrument.factor)
}
})) %>%
dplyr::select(-"form_factor")
}
}


if(repeat_instrument) {
form_check <- data |>
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ install.packages("remotes") # Run this line if the 'remotes' package isn't insta
remotes::install_github("bruigtp/REDCapDM")
```


### Getting Started

To learn more about the package’s functionality, visit the [**REDCapDM website**](https://bruigtp.github.io/REDCapDM/articles/REDCapDM.html). The site includes detailed descriptions of the package's functions and access to vignettes that demonstrate how to use REDCapDM effectively in your projects.
Expand Down
4 changes: 2 additions & 2 deletions man/rd_checkbox.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/redcap_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.