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
2 changes: 1 addition & 1 deletion R/createFileRepositoryFolder.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ createFileRepositoryFolder.redcapApiConnection <- function(rcon,
rcon$flush_fileRepository()

# Prepare Output --------------------------------------------------
NewFolder <- as.data.frame(response)
NewFolder <- as.data.frame(response, sep = rcon$csv_delimiter())

NewFolder$name <- rep(name, nrow(NewFolder))

Expand Down
22 changes: 11 additions & 11 deletions R/exportArms.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,35 @@ exportArms <- function(rcon, ...){
#' @order 4
#' @export

exportArms.redcapApiConnection <- function(rcon,
arms = character(0),
exportArms.redcapApiConnection <- function(rcon,
arms = character(0),
...)
{
if (is.numeric(arms)) arms <- as.character(arms)

# Argument Validation ---------------------------------------------
coll <- checkmate::makeAssertCollection()

checkmate::assert_class(x = rcon,
classes = "redcapApiConnection",
add = coll)

checkmate::assert_character(x = arms,
add = coll)

checkmate::reportAssertions(coll)

if(rcon$projectInformation()$is_longitudinal == 0){
return(REDCAP_ARMS_STRUCTURE) # defined in redcapDataStructure.R
}

# Build the body list ---------------------------------------------
body <- c(list(content = 'arm',
format = 'csv',
returnFormat = 'csv'),
vectorToApiBodyList(arms,
body <- c(list(content = 'arm',
format = 'csv',
returnFormat = 'csv'),
vectorToApiBodyList(arms,
parameter_name = "arms"))

# API Call --------------------------------------------------------
as.data.frame(makeApiCall(rcon, body, ...))
as.data.frame(makeApiCall(rcon, body, ...), sep = rcon$csv_delimiter())
}
40 changes: 20 additions & 20 deletions R/exportEvents.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#' @order 1
#' @export

exportEvents <- function(rcon,
exportEvents <- function(rcon,
...){
UseMethod("exportEvents")
}
Expand All @@ -11,53 +11,53 @@ exportEvents <- function(rcon,
#' @order 4
#' @export

exportEvents.redcapApiConnection <- function(rcon,
arms = NULL,
exportEvents.redcapApiConnection <- function(rcon,
arms = NULL,
...)
{
if (is.character(arms)) arms <- as.numeric(arms)

##################################################################
# Argument Validation
# Argument Validation
coll <- checkmate::makeAssertCollection()

checkmate::assert_class(x = rcon,
classes = "redcapApiConnection",
add = coll)

checkmate::assert_integerish(x = arms,
null.ok = TRUE,
add = coll)

checkmate::reportAssertions(coll)

Arms <- rcon$arms()
checkmate::assert_subset(x = arms,
choices = Arms$arm_num,

checkmate::assert_subset(x = arms,
choices = Arms$arm_num,
add = coll)

checkmate::reportAssertions(coll)

##################################################################
# Return for Classical projects

if (rcon$projectInformation()$is_longitudinal == 0){
return(REDCAP_EVENT_STRUCTURE) # Defined in redcapDataStructure.R
}

##################################################################
# Make the Body List
body <- list(content = 'event',
format = 'csv',

body <- list(content = 'event',
format = 'csv',
returnFormat = 'csv')
body <- c(body,
body <- c(body,
vectorToApiBodyList(arms, "arms"))

##################################################################
# Call the API

response <- as.data.frame(makeApiCall(rcon, body, ...))
response <- as.data.frame(makeApiCall(rcon, body, ...), sep = rcon$csv_delimiter())
if(nrow(response) == 0) REDCAP_EVENT_STRUCTURE else response
}
168 changes: 84 additions & 84 deletions R/exportExternalCoding.R
Original file line number Diff line number Diff line change
@@ -1,165 +1,165 @@
#' @name exportExternalCoding
#' @title Export Codebook Mappings for Fields with External Dependencies
#'
#' @description These methods enable `redcapAPI` to obtain a mapping of
#' codes and associated labels for fields that have external dependencies.
#' The fields include SQL fields (dependent on another project) or
#'
#' @description These methods enable `redcapAPI` to obtain a mapping of
#' codes and associated labels for fields that have external dependencies.
#' The fields include SQL fields (dependent on another project) or
#' fields that utilize the BioPortal Ontology modules.
#'
#'
#' @inheritParams common-rcon-arg
#' @inheritParams common-dot-args
#' @inheritParams common-api-args
#' @inheritParams recordsTypedMethods
#'
#' @details These methods operate by executing two API calls to export first the
#' coded values and then the labeled values of fields with external
#' dependencies. The two exports are then used to generate the code-label
#'
#' @details These methods operate by executing two API calls to export first the
#' coded values and then the labeled values of fields with external
#' dependencies. The two exports are then used to generate the code-label
#' mappings for use in casting data.
#'
#' Fields of type `sql` are dropdown fields that are populated by a SQL
#' query to another project.
#'
#' Fields of type `bioportal` are text fields that have the BioPortal
#'
#' Fields of type `sql` are dropdown fields that are populated by a SQL
#' query to another project.
#'
#' Fields of type `bioportal` are text fields that have the BioPortal
#' Ontology module enabled as the validation method.
#'
#'
#' @return
#' Returns a named list of named character vectors.
#'
#' Each element is in the list is named for the field it maps.
#'
#' The character vectors are name-value pairs where the name is the labeled
#' Returns a named list of named character vectors.
#'
#' Each element is in the list is named for the field it maps.
#'
#' The character vectors are name-value pairs where the name is the labeled
#' data and the value is the coded data.
#'
#'
#' @examples
#' \dontrun{
#' unlockREDCap(connections = c(rcon = "project_alias"),
#' url = "your_redcap_url",
#' keyring = "API_KEYs",
#' unlockREDCap(connections = c(rcon = "project_alias"),
#' url = "your_redcap_url",
#' keyring = "API_KEYs",
#' envir = globalenv())
#'
#'
#' exportExternalCoding(rcon)
#' }
#'
#'

exportExternalCoding <- function(rcon,
fields,
exportExternalCoding <- function(rcon,
fields,
...){
UseMethod("exportExternalCoding")
}

#' @rdname exportExternalCoding
#' @export

exportExternalCoding.redcapApiConnection <- function(rcon,
fields = NULL,
...,
exportExternalCoding.redcapApiConnection <- function(rcon,
fields = NULL,
...,
batch_size = 1000)
{
###################################################################
# Argument Validation ####

coll <- checkmate::makeAssertCollection()
checkmate::assert_class(x = rcon,
class = "redcapConnection",

checkmate::assert_class(x = rcon,
class = "redcapConnection",
add = coll)
checkmate::assert_character(x = fields,
null.ok = TRUE,
any.missing = FALSE,

checkmate::assert_character(x = fields,
null.ok = TRUE,
any.missing = FALSE,
add = coll)
checkmate::assert_integerish(x = batch_size,

checkmate::assert_integerish(x = batch_size,
len = 1,
lower = 1,
null.ok = TRUE,
any.missing = FALSE,
lower = 1,
null.ok = TRUE,
any.missing = FALSE,
add = coll)

checkmate::reportAssertions(coll)
checkmate::assert_subset(x = fields,
choices = rcon$metadata()$field_name,

checkmate::assert_subset(x = fields,
choices = rcon$metadata()$field_name,
add = coll)

checkmate::reportAssertions(coll)

###################################################################
# Functionality ####

MetaData <- rcon$metadata()
external_fields <-
MetaData$field_name[grepl("BIOPORTAL",
MetaData$select_choices_or_calculations,
ignore.case = TRUE) |
external_fields <-
MetaData$field_name[grepl("BIOPORTAL",
MetaData$select_choices_or_calculations,
ignore.case = TRUE) |
(!is.na(MetaData$field_type) & MetaData$field_type == "sql")]

if (is.null(fields)){
fields <- external_fields
} else {
fields <- fields[fields %in% external_fields]
}

if (length(fields) == 0){
return(list())
}
body <- c(list(content = "record",
format = "csv",
returnFormat = "csv",
type = "flat",
rawOrLabel = "raw"),

body <- c(list(content = "record",
format = "csv",
returnFormat = "csv",
type = "flat",
rawOrLabel = "raw"),
vectorToApiBodyList(fields, "fields"))

Code <-
Code <-
if (!is.null(batch_size)){
.exportRecordsTyped_Batched(rcon = rcon,
body = body,
.exportRecordsTyped_Batched(rcon = rcon,
body = body,
records = NULL,
csv_delimiter = ",",
csv_delimiter = rcon$csv_delimiter(),
batch_size = batch_size,
...)
} else {
.exportRecordsTyped_Unbatched(rcon = rcon,
body = body,
.exportRecordsTyped_Unbatched(rcon = rcon,
body = body,
records = NULL,
csv_delimiter = ",",
csv_delimiter = rcon$csv_delimiter(),
...)
}

body$rawOrLabel <- "label"
Label <-

Label <-
if (!is.null(batch_size)){
.exportRecordsTyped_Batched(rcon = rcon,
body = body,
.exportRecordsTyped_Batched(rcon = rcon,
body = body,
records = NULL,
csv_delimiter = ",",
csv_delimiter = rcon$csv_delimiter(),
batch_size = batch_size,
...)
} else {
.exportRecordsTyped_Unbatched(rcon = rcon,
body = body,
records = NULL,
csv_delimiter = ",",
.exportRecordsTyped_Unbatched(rcon = rcon,
body = body,
records = NULL,
csv_delimiter = rcon$csv_delimiter(),
...)
}

External <- vector("list", length(fields))
names(External) <- fields

for (f in fields){
ThisCode <- data.frame(code = Code[[f]],
label = Label[[f]],
ThisCode <- data.frame(code = Code[[f]],
label = Label[[f]],
stringsAsFactors = FALSE)
ThisCode <- ThisCode[!duplicated(ThisCode), ]
ThisCode <- ThisCode[!is.na(ThisCode$code), ]

mapping <- ThisCode$code
names(mapping) <- ThisCode$label

External[[f]] <- mapping
}

External
}
Loading
Loading