-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from ljwoodley/add_field_type_slider_support
Add field type slider support
- Loading branch information
Showing
17 changed files
with
254 additions
and
20 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
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
# redcapfiller (0.1.0.9001) | ||
# redcapfiller 0.1.0.9001 (released TBD) | ||
|
||
* Supports filling of checkbox, dropdown, radio, yes/no, True/False and unvalidated text fields on classic projects. | ||
* Provides a proof_of_concept.R script with write-back to a demonstration project and properly managed secrets. | ||
|
||
- Supports filling of checkbox, dropdown, radio, yes/no, True/False, sliders and unvalidated text fields on classic projects. | ||
- Provides a proof_of_concept.R script with write-back to a demonstration project and properly managed secrets. |
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,34 @@ | ||
#' @title generate slider field values | ||
#' @description | ||
#' Provide a set of values for each slider field in | ||
#' `long_slider_values` | ||
#' | ||
#' @param long_slider_values a long data set of slider values and weights. | ||
#' | ||
#' @return a tall dataframe of slider values with one row for each field | ||
#' @export | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' get_long_slider_field_values(long_slider_values) | ||
#' } | ||
get_long_slider_field_values <- function(long_slider_values) { | ||
|
||
result <- long_slider_values |> | ||
dplyr::filter(.data$field_type == "slider") |> | ||
dplyr::mutate( | ||
response_code = round(stats::rnorm(n = length(.data$mean), mean = .data$mean, sd = .data$sd)), | ||
response_code = as.character(ifelse( | ||
.data$response_code < dplyr::coalesce(as.numeric(.data$text_validation_min), 0) | | ||
.data$response_code > dplyr::coalesce(as.numeric(.data$text_validation_max), 100) , | ||
NA, | ||
.data$response_code) | ||
) | ||
) |> | ||
dplyr::group_by(.data$field_name) |> | ||
dplyr::slice_sample(n = 1, weight_by = .data$weight) |> | ||
dplyr::ungroup() |> | ||
dplyr::select("field_name", value = "response_code") | ||
|
||
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,64 @@ | ||
#' @title Get slider field ranges from a REDCap data dictionary | ||
#' | ||
#' @description | ||
#' Given a REDCap data dictionary, enumerate range of values for every slider field in that data dictionary | ||
#' | ||
#' @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{select_choices_or_calculations}{RedCap select_choices_or_calculations field} | ||
#' \item{text_validation_min}{REDCap text validation min} | ||
#' \item{text_validation_max}{REDCap text validation max} | ||
#' \item{mean}{mean of data to be generated} | ||
#' \item{sd}{standard deviation of data to be generated} | ||
#' | ||
#' } | ||
#' @export | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' long_slider_fields <- | ||
#' get_long_slider_fields(metadata_to_populate) | ||
#' } | ||
get_long_slider_fields <- function(metadata) { | ||
long_slider_values <- metadata |> | ||
# include only slider field types | ||
dplyr::filter(.data$field_type == "slider") |> | ||
dplyr::group_by(.data$field_name) |> | ||
dplyr::mutate( | ||
text_validation_max = dplyr::if_else( | ||
is.na(.data$text_validation_max), | ||
"100", | ||
as.character(.data$text_validation_max) | ||
), | ||
text_validation_min = dplyr::if_else( | ||
is.na(.data$text_validation_min), | ||
"0", | ||
as.character(.data$text_validation_min) | ||
) | ||
) |> | ||
# narrow our focus to the required columns | ||
dplyr::select( | ||
c( | ||
"field_name", | ||
"form_name", | ||
"field_type", | ||
"select_choices_or_calculations", | ||
"text_validation_min", | ||
"text_validation_max" | ||
) | ||
) |> | ||
dplyr::group_by(.data$field_type) |> | ||
dplyr::mutate( | ||
weight = 100, | ||
mean = (as.numeric(.data$text_validation_min) + as.numeric(.data$text_validation_max)) / 2, | ||
sd = (as.numeric(.data$text_validation_max) - as.numeric(.data$text_validation_min)) / 6 | ||
) |> | ||
dplyr::ungroup() | ||
|
||
return(long_slider_values) | ||
} |
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.
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.
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,6 @@ | ||
metadata_file <- testthat::test_path("get_long_slider_field_values", "metadata.csv") | ||
metadata <- readr::read_csv(metadata_file) | ||
|
||
long_slider_fields <- get_long_slider_fields(metadata) | ||
long_slider_fields |> | ||
saveRDS(testthat::test_path("get_long_slider_field_values", "input.rds")) |
14 changes: 8 additions & 6 deletions
14
...estthat/get_long_text_fields/metadata.csv → ...get_long_slider_field_values/metadata.csv
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
File renamed without changes.
11 changes: 5 additions & 6 deletions
11
..._categorical_field_responses/metadata.csv → tests/testthat/shared_testdata/metadata.csv
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,32 @@ | ||
long_slider_fields <- readRDS( | ||
testthat::test_path("get_long_slider_field_values", "input.rds") | ||
) | ||
|
||
output <- get_long_slider_field_values(long_slider_fields) | ||
|
||
testthat::test_that("get_long_slider_field_values returns the correct df with values within the slider ranges", { | ||
slider_bounds <- long_slider_fields |> | ||
dplyr::select(field_name, text_validation_min, text_validation_max) |> | ||
dplyr::group_by(field_name) |> | ||
dplyr::summarise( | ||
text_validation_min = min(text_validation_min), | ||
text_validation_max = max(text_validation_max) | ||
) | ||
|
||
result <- output %>% | ||
dplyr::left_join(slider_bounds, by = "field_name") |> | ||
dplyr::summarise(all_within_range = all(dplyr::between( | ||
as.numeric(value), | ||
as.numeric(text_validation_min), | ||
as.numeric(text_validation_max) | ||
), na.rm = TRUE)) | ||
|
||
testthat::expect_true(result$all_within_range) | ||
}) | ||
|
||
testthat::test_that("get_long_slider_field_values returns a unique number for each slider", { | ||
testthat::expect_equal( | ||
output |> dplyr::distinct(value) |> nrow(), | ||
output |> nrow() | ||
) | ||
}) |
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,33 @@ | ||
metadata_file <- testthat::test_path("shared_testdata", "metadata.csv") | ||
metadata <- readr::read_csv(metadata_file) | ||
|
||
output <- get_long_slider_fields(metadata) | ||
|
||
testthat::test_that("get_long_slider_fields: processes sliders", { | ||
testthat::expect_equal( | ||
output |> | ||
dplyr::distinct(field_type) |> | ||
dplyr::arrange(field_type) |> | ||
dplyr::pull(field_type), | ||
"slider" | ||
) | ||
}) | ||
|
||
testthat::test_that("get_long_slider_fields values are distinct within each field", { | ||
duplicate_values <- output %>% | ||
dplyr::group_by(field_name) %>% | ||
dplyr::summarise(duplicates = sum(duplicated(select_choices_or_calculations))) %>% | ||
dplyr::pull(duplicates) | ||
|
||
testthat::expect_true(all(duplicate_values == 0), "There are duplicate values within groups") | ||
}) | ||
|
||
testthat::test_that("get_long_slider_fields weights are balanced", { | ||
testthat::expect_true(output |> | ||
dplyr::group_by(field_name) |> | ||
dplyr::summarise(balanced = (min(weight) == max(weight))) |> | ||
dplyr::ungroup() |> | ||
dplyr::distinct(balanced) |> | ||
dplyr::pull(balanced)) | ||
}) | ||
|
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