forked from ctsit/redcapfiller
-
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.
Add support for unvalidated text fields
Address issues ctsit#3, ctsit#7. Add get_long_text_fields(). Add get_long_text_field_values(). Modify get_long_categorical_field_response_values(). Modify get_one_rectangle_of_values() Update proof_of_concept(). Update README to reflect the new feature.
- Loading branch information
Showing
21 changed files
with
449 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,8 @@ Suggests: | |
Config/testthat/edition: 3 | ||
Imports: | ||
dplyr, | ||
lorem, | ||
purrr, | ||
rlang, | ||
tidyr | ||
RoxygenNote: 7.3.2 | ||
|
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#' @title generate text field values | ||
#' @description | ||
#' Provide a set of values for each field in the output of | ||
#' `get_long_text_fields` | ||
#' | ||
#' @param long_text_fields a long data set of text | ||
#' fields, their parameters, and weights. | ||
#' | ||
#' @return a long dataframe of text field values with one row for each value set. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' get_long_text_field_values(long_text_fields) | ||
#' } | ||
get_long_text_field_values <- function(long_text_fields) { | ||
tvt_na <- function(df) { | ||
df |> | ||
dplyr::filter(.data$tvt == "tvt_na") |> | ||
dplyr::mutate(value = replicate(length(.data$field_name), lorem::ipsum_words(round(stats::rnorm(mean = .data$mean, sd = .data$sd, n = 1))))) | ||
# |> | ||
# dplyr::slice_sample(prop = 0.5, weight_by = .data$weight) | ||
} | ||
|
||
tvt_types <- c( | ||
"tvt_na" | ||
) | ||
|
||
process_one_text_validation_type <- function(my_tvt, df) { | ||
# exec (run) the function named in `alert` on the rows of data that | ||
# have an alert_type of `alert` | ||
result <- rlang::exec(my_tvt, df |> | ||
dplyr::filter(.data$tvt == my_tvt)) | ||
return(result) | ||
} | ||
|
||
text_field_values <- | ||
purrr::map(tvt_types, | ||
process_one_text_validation_type, | ||
long_text_fields |> dplyr::filter(.data$field_type == "text") | ||
) |> | ||
dplyr::bind_rows() | ||
|
||
result <- text_field_values |> | ||
dplyr::select("field_name", "value") | ||
|
||
return(result) | ||
} |
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,85 @@ | ||
#' @title Get every text field response from a REDCap data dictionary | ||
#' | ||
#' @description | ||
#' Given a REDCap data dictionary, enumerate every text field in that data dictionary and return a dataset with default weights | ||
#' | ||
#' @param metadata A REDCap data dictionary | ||
#' | ||
#' @returns a dataframe with these columns | ||
#' \describe{ | ||
#' \item{field_name}{REDCap field name} | ||
#' \item{form_name}{REDCap form name} | ||
#' \item{field_type}{REDCap field type} | ||
#' \item{text_validation_type}{REDCap text validation type} | ||
#' \item{text_validation_min}{REDCap text validation min} | ||
#' \item{text_validation_max}{REDCap text validation max} | ||
#' \item{tvt}{text validation type function name} | ||
#' \item{weight}{a default weight for the field} | ||
#' \item{mean}{mean of data to be generated} | ||
#' \item{sd}{standard deviation of data to be generated} | ||
#' } | ||
#' @export | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' long_text_fields <- | ||
#' get_long_text_fields(metadata_to_populate) | ||
#' } | ||
get_long_text_fields <- function(metadata) { | ||
text_fields <- | ||
metadata |> | ||
# include only categorical field types | ||
dplyr::filter(.data$field_type %in% c("text")) |> | ||
# excluding anything displayed by branching logic | ||
dplyr::filter(is.na(.data$branching_logic)) |> | ||
# narrow our focus to the required columns | ||
dplyr::select(c( | ||
"field_name", | ||
"form_name", | ||
"field_type", | ||
"text_validation_type_or_show_slider_number", | ||
"text_validation_min", | ||
"text_validation_max" | ||
)) |> | ||
dplyr::rename(text_validation_type = "text_validation_type_or_show_slider_number") |> | ||
dplyr::mutate(tvt = dplyr::case_when( | ||
is.na(.data$text_validation_type) ~ "tvt_na", | ||
TRUE ~ "tvt_unsupported" | ||
)) |> | ||
# set weights for each response | ||
dplyr::mutate(weight = 100) | ||
|
||
tvt_na <- function(text_fields) { | ||
result <- | ||
text_fields |> | ||
dplyr::filter(is.na(.data$text_validation_type)) |> | ||
dplyr::mutate(mean = 1.5, sd = 0.8) | ||
return(result) | ||
} | ||
|
||
tvt_unsupported <- function(text_fields) { | ||
result <- | ||
text_fields |> | ||
dplyr::filter(F) | ||
return(result) | ||
} | ||
|
||
tvt_types <- c( | ||
"tvt_na", | ||
"tvt_unsupported" | ||
) | ||
|
||
process_one_text_validation_type <- function(my_tvt, df) { | ||
# exec (run) the function named in `alert` on the rows of data that | ||
# have an alert_type of `alert` | ||
result <- rlang::exec(my_tvt, df |> | ||
dplyr::filter(.data$tvt == my_tvt)) | ||
return(result) | ||
} | ||
|
||
text_fields_and_weights <- | ||
purrr::map(tvt_types, process_one_text_validation_type, text_fields) |> | ||
dplyr::bind_rows() | ||
|
||
return(text_fields_and_weights) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Binary file not shown.
25 changes: 25 additions & 0 deletions
25
tests/testthat/get_long_text_field_values/make_test_data.R
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,25 @@ | ||
# Download the data dictionary from a copy of REDCapR's validation-types-1 project. | ||
# See https://ctsit.github.io/redcapfiller/articles/demonstration_and_testing.html for details | ||
|
||
library(tidyverse) | ||
library(REDCapR) | ||
# Read environment from ".env" in the project directory | ||
library(dotenv) | ||
load_dot_env(".env") | ||
|
||
# Get our credentials using environment variables to locate | ||
# the credentials file and describe the project in it | ||
path_credential <- Sys.getenv("path_credential") | ||
credentials <- REDCapR::retrieve_credential_local( | ||
path_credential, | ||
project_id = Sys.getenv("filler_demo_pid") | ||
) | ||
|
||
metadata <- REDCapR::redcap_metadata_read( | ||
token = credentials$token, | ||
redcap_uri = credentials$redcap_uri | ||
)$data | ||
|
||
long_text_fields <- metadata |> get_long_text_fields() | ||
|
||
long_text_fields |> saveRDS(testthat::test_path("get_long_text_field_values", "input.rds")) |
Oops, something went wrong.