Skip to content

Commit

Permalink
redcap_read_oneshot_eav() uses kernel
Browse files Browse the repository at this point in the history
ref #213
  • Loading branch information
wibeasley committed May 29, 2018
1 parent ffa46f1 commit a3c1bf2
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions R/redcap-read-oneshot-eav.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ redcap_read_oneshot_eav <- function(
) {
#TODO: NULL verbose parameter pulls from getOption("verbose")

start_time <- Sys.time()
# start_time <- Sys.time()

checkmate::assert_character(redcap_uri , any.missing=F, len=1, pattern="^.{1,}$")
checkmate::assert_character(token , any.missing=F, len=1, pattern="^.{1,}$")
Expand Down Expand Up @@ -168,31 +168,34 @@ redcap_read_oneshot_eav <- function(
if( nchar(forms_collapsed ) > 0 ) post_body$forms <- forms_collapsed
if( nchar(events_collapsed ) > 0 ) post_body$events <- events_collapsed

result <- httr::POST(
url = redcap_uri,
body = post_body,
config = config_options
)

status_code <- result$status
success <- (status_code==200L)
raw_text <- httr::content(result, "text")
raw_text <- gsub("\r\n", "\n", raw_text)
elapsed_seconds <- as.numeric(difftime(Sys.time(), start_time, units="secs"))
# This is the important line that communicates with the REDCap server.
kernel <- kernel_api(redcap_uri, post_body, config_options)

# raw_text <- "The hostname (redcap-db.hsc.net.ou.edu) / username (redcapsql) / password (XXXXXX) combination could not connect to the MySQL server. \r\n\t\tPlease check their values."
regex_cannot_connect <- "^The hostname \\((.+)\\) / username \\((.+)\\) / password \\((.+)\\) combination could not connect.+"
regex_empty <- "^\\s+$"

success <- (success & !any(grepl(regex_cannot_connect, raw_text)) & !any(grepl(regex_empty, raw_text)))
# result <- httr::POST(
# url = redcap_uri,
# body = post_body,
# config = config_options
# )
#
# status_code <- result$status
# success <- (status_code==200L)
# raw_text <- httr::content(result, "text")
# raw_text <- gsub("\r\n", "\n", raw_text)
# elapsed_seconds <- as.numeric(difftime(Sys.time(), start_time, units="secs"))
#
# # raw_text <- "The hostname (redcap-db.hsc.net.ou.edu) / username (redcapsql) / password (XXXXXX) combination could not connect to the MySQL server. \r\n\t\tPlease check their values."
# regex_cannot_connect <- "^The hostname \\((.+)\\) / username \\((.+)\\) / password \\((.+)\\) combination could not connect.+"
# regex_empty <- "^\\s+$"
#
# success <- (success & !any(grepl(regex_cannot_connect, raw_text)) & !any(grepl(regex_empty, raw_text)))

ds_metadata <- REDCapR::redcap_metadata_read(redcap_uri, token, forms_collapsed=forms_collapsed)$data
ds_variable <- REDCapR::redcap_variables(redcap_uri, token)$data

if( success ) {
if( kernel$success ) {
try (
{
ds_eav <- readr::read_csv(raw_text)
ds_eav <- readr::read_csv(kernel$raw_text)

ds_metadata_expanded <- ds_metadata %>%
dplyr::select_("field_name", "select_choices_or_calculations", "field_type") %>%
Expand Down Expand Up @@ -265,23 +268,22 @@ redcap_read_oneshot_eav <- function(
outcome_message <- paste0(
format( nrow(ds), big.mark=",", scientific=FALSE, trim=TRUE), " records and ",
format(length(ds), big.mark=",", scientific=FALSE, trim=TRUE), " columns were read from REDCap in ",
round(elapsed_seconds, 1), " seconds. The http status code was ", status_code, "."
round(kernel$elapsed_seconds, 1), " seconds. The http status code was ", kernel$status_code, "."
)

# If an operation is successful, the `raw_text` is no longer returned to save RAM. The content is not really necessary with httr's status message exposed.
raw_text <- ""
kernel$raw_text <- ""
} else {
success <- FALSE #Override the 'success' determination from the http status code.
ds_2 <- tibble::tibble() #Return an empty data.frame
outcome_message <- paste0("The REDCap read failed. The http status code was ", status_code, ". The 'raw_text' returned was '", raw_text, "'.")
outcome_message <- paste0("The REDCap read failed. The http status code was ", kernel$status_code, ". The 'raw_text' returned was '", kernel$raw_text, "'.")
}
}
else {
} else {
ds_2 <- tibble::tibble() #Return an empty data.frame
outcome_message <- if( any(grepl(regex_empty, raw_text)) ) {
outcome_message <- if( any(grepl(kernel$regex_empty, kernel$raw_text)) ) {
"The REDCapR read/export operation was not successful. The returned dataset was empty."
} else {
paste0("The REDCapR read/export operation was not successful. The error message was:\n", raw_text)
paste0("The REDCapR read/export operation was not successful. The error message was:\n", kernel$raw_text)
}
}

Expand All @@ -290,14 +292,14 @@ redcap_read_oneshot_eav <- function(

return( list(
data = ds_2,
success = success,
status_code = status_code,
success = kernel$success,
status_code = kernel$status_code,
outcome_message = outcome_message,
records_collapsed = records_collapsed,
fields_collapsed = fields_collapsed,
filter_logic = filter_logic,
events_collapsed = events_collapsed,
elapsed_seconds = elapsed_seconds,
raw_text = raw_text
elapsed_seconds = kernel$elapsed_seconds,
raw_text = kernel$raw_text
) )
}

0 comments on commit a3c1bf2

Please sign in to comment.