Skip to content

Commit

Permalink
add convert_logical_to_integer parameter
Browse files Browse the repository at this point in the history
closes #305
  • Loading branch information
wibeasley committed Sep 27, 2020
1 parent 74e991f commit 6f4cac0
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 18 deletions.
7 changes: 5 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ Version 0.11 (Released 2020-04-20)

* [`redcap_metadata_write()`](https://ouhscbbmc.github.io/REDCapR/reference/redcap_metadata_write.html) writes to the project's metadata. (#274, @felixetorres)

* [`redcap_survey_link_export_oneshot()`](https://ouhscbbmc.github.io/REDCapR/reference/redcap_survey_link_export_oneshot.html) retrieves the URL to a specific record's suvey (*e.g.*, "https://bbmc.ouhsc.edu/redcap/surveys/?s=8KuzSLMHf6") (#293)
* [`redcap_survey_link_export_oneshot()`](https://ouhscbbmc.github.io/REDCapR/reference/redcap_survey_link_export_oneshot.html) retrieves the URL to a specific record's survey (*e.g.*, "https://bbmc.ouhsc.edu/redcap/surveys/?s=8KuzSLMHf6") (#293)

* `convert_logical_to_integer` is a new parameter for [`redcap_write()`](https://ouhscbbmc.github.io/REDCapR/reference/redcap_write.html) and [`redcap_write_oneshot()`](https://ouhscbbmc.github.io/REDCapR/reference/redcap_write_oneshot.html). If `TRUE`, all [base::logical] columns in `ds` are cast to an integer before uploading to REDCap. Boolean values are typically represented as 0/1 in REDCap radio buttons. Defaults to `FALSE` to maintain backwards compatibility. (#305)

### Stability Features

Expand All @@ -35,8 +37,9 @@ Version 0.11 (Released 2020-04-20)

### Corrections

* 'checkmate' package is now imported, not suggested (Thanks @dtenenba, #255)
* 'checkmate' package is now imported, not suggested (Thanks @dtenenba, #255).

* Allow more than one `httr::config()` parameter to be passed (Thanks @BastienRance, #307).

Version 0.10 (Released 2019-09-22)
==========================================================
Expand Down
6 changes: 4 additions & 2 deletions R/project-simple.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ populate_project_simple <- function(batch = FALSE) {
ds = ds_to_write,
redcap_uri = project$redcap_uri,
token = project$token,
verbose = TRUE
verbose = TRUE,
convert_logical_to_integer = T
)
} else {
REDCapR::redcap_write_oneshot(
ds = ds_to_write,
redcap_uri = project$redcap_uri,
token = project$token,
verbose = TRUE
verbose = TRUE,
convert_logical_to_integer = T
)
}
)
Expand Down
12 changes: 11 additions & 1 deletion R/redcap-write-oneshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#' project. Required.
#' @param token The user-specific string that serves as the password for a
#' project. Required.
#' @param convert_logical_to_integer If `TRUE`, all [base::logical] columns
#' in `ds` are cast to an integer before uploading to REDCap.
#' Boolean values are typically represented as 0/1 in REDCap radio buttons.
#' Optional.
#' @param verbose A boolean value indicating if `message`s should be printed
#' to the R console during the operation. The verbose output might contain
#' sensitive information (*e.g.* PHI), so turn this off if the output might
Expand Down Expand Up @@ -80,11 +84,11 @@ redcap_write_oneshot <- function(
ds,
redcap_uri,
token,
convert_logical_to_integer = FALSE,
verbose = TRUE,
config_options = NULL
) {

# TODO: automatically convert boolean/logical class to integer/bit class
csv_elements <- NULL #This prevents the R CHECK NOTE: 'No visible binding for global variable Note in R CMD check'; Also see if( getRversion() >= "2.15.1" ) utils::globalVariables(names=c("csv_elements")) #http://stackoverflow.com/questions/8096313/no-visible-binding-for-global-variable-note-in-r-cmd-check; http://stackoverflow.com/questions/9439256/how-can-i-handle-r-cmd-check-no-visible-binding-for-global-variable-notes-when

checkmate::assert_character(redcap_uri, any.missing=FALSE, len=1, pattern="^.{1,}$")
Expand All @@ -93,6 +97,12 @@ redcap_write_oneshot <- function(
token <- sanitize_token(token)
verbose <- verbose_prepare(verbose)

if (convert_logical_to_integer) {
ds <-
ds %>%
dplyr::mutate_if(is.logical, as.integer)
}

con <- base::textConnection(
object = "csv_elements",
open = "w",
Expand Down
6 changes: 6 additions & 0 deletions R/redcap-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#' project. Required.
#' @param token The user-specific string that serves as the password for a
#' project. Required.
#' @param convert_logical_to_integer If `TRUE`, all [base::logical] columns
#' in `ds` are cast to an integer before uploading to REDCap.
#' Boolean values are typically represented as 0/1 in REDCap radio buttons.
#' Optional.
#' @param verbose A boolean value indicating if `message`s should be printed
#' to the R console during the operation. The verbose output might contain
#' sensitive information (*e.g.* PHI), so turn this off if the output might
Expand Down Expand Up @@ -96,6 +100,7 @@ redcap_write <- function(
continue_on_error = FALSE,
redcap_uri,
token,
convert_logical_to_integer = FALSE,
verbose = TRUE,
config_options = NULL
) {
Expand Down Expand Up @@ -141,6 +146,7 @@ redcap_write <- function(
ds = ds_to_write[selected_indices, ],
redcap_uri = redcap_uri,
token = token,
convert_logical_to_integer = convert_logical_to_integer,
verbose = verbose,
config_options = config_options
)
Expand Down
26 changes: 13 additions & 13 deletions inst/test-data/project-simple/simple-data.csv
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"record_id","name_first","name_last","address","telephone","email","dob","age","sex","demographics_complete","height","weight","bmi","comments","mugshot","health_complete","race___1","race___2","race___3","race___4","race___5","race___6","ethnicity","race_and_ethnicity_complete"
1,"Nutmeg","Nutmouse","14 Rose Cottage St.
Kenning UK, 323232","(405) 321-1111","[email protected]","2003-08-30",11,0,2,7,1,204.1,"Character in a book, with some guessing","[document]",1,0,0,0,0,1,0,1,2
2,"Tumtum","Nutmouse","14 Rose Cottage Blvd.
Kenning UK 34243","(405) 321-2222","[email protected]","2003-03-10",11,1,2,6,1,277.8,"A mouse character from a good book","[document]",0,0,0,1,0,1,0,1,0
3,"Marcus","Wood","243 Hill St.
Guthrie OK 73402","(405) 321-3333","[email protected]","1934-04-09",80,1,2,180,80,24.7,"completely made up","[document]",2,0,0,0,1,1,0,0,2
4,"Trudy","DAG","342 Elm
Duncanville TX, 75116","(405) 321-4444","[email protected]","1952-11-02",61,0,2,165,54,19.8,"This record doesn't have a DAG assigned
record_id,name_first,name_last,address,telephone,email,dob,age,sex,demographics_complete,height,weight,bmi,comments,mugshot,health_complete,race___1,race___2,race___3,race___4,race___5,race___6,ethnicity,race_and_ethnicity_complete
1,Nutmeg,Nutmouse,"14 Rose Cottage St.
Kenning UK, 323232",(405) 321-1111,[email protected],2003-08-30,11,FALSE,2,7,1,204.1,"Character in a book, with some guessing",[document],1,0,0,0,0,1,0,1,2
2,Tumtum,Nutmouse,"14 Rose Cottage Blvd.
Kenning UK 34243",(405) 321-2222,[email protected],2003-03-10,11,TRUE,2,6,1,277.8,A mouse character from a good book,[document],0,0,0,1,0,1,0,1,0
3,Marcus,Wood,"243 Hill St.
Guthrie OK 73402",(405) 321-3333,[email protected],1934-04-09,80,TRUE,2,180,80,24.7,completely made up,[document],2,0,0,0,1,1,0,0,2
4,Trudy,DAG,"342 Elm
Duncanville TX, 75116",(405) 321-4444,[email protected],1952-11-02,61,FALSE,2,165,54,19.8,"This record doesn't have a DAG assigned

So call up Trudy on the telephone
Send her a letter in the mail","[document]",2,0,1,0,0,1,0,1,2
5,"John Lee","Walker","Hotel Suite
New Orleans LA, 70115","(405) 321-5555","[email protected]","1955-04-15",59,1,2,193.04,104,27.9,"Had a hand for trouble and a eye for cash
Send her a letter in the mail",[document],2,0,1,0,0,1,0,1,2
5,John Lee,Walker,"Hotel Suite
New Orleans LA, 70115",(405) 321-5555,[email protected],1955-04-15,59,TRUE,2,193.04,104,27.9,"Had a hand for trouble and a eye for cash

He had a gold watch chain and a black mustache","[document]",0,1,0,0,0,0,1,2,2
He had a gold watch chain and a black mustache",[document],0,1,0,0,0,0,1,2,2
6 changes: 6 additions & 0 deletions man/redcap_write.Rd

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

6 changes: 6 additions & 0 deletions man/redcap_write_oneshot.Rd

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

0 comments on commit 6f4cac0

Please sign in to comment.