Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
ref #213
  • Loading branch information
wibeasley committed May 29, 2018
1 parent a3c1bf2 commit 45e53bc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 65 deletions.
38 changes: 14 additions & 24 deletions R/redcap-metadata-read.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@
#' }

redcap_metadata_read <- function(
redcap_uri, token, forms=NULL, forms_collapsed="",
fields=NULL, fields_collapsed="",
verbose=TRUE, config_options=NULL
redcap_uri,
token,
forms = NULL, forms_collapsed = "",
fields = NULL, fields_collapsed = "",
verbose = TRUE,
config_options = NULL
) {
#TODO: NULL verbose parameter pulls from getOption("verbose")

# 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 All @@ -69,18 +71,8 @@ redcap_metadata_read <- function(
fields = fields_collapsed
)

# This is the important line that communicates with the REDCap server.
kernel <- kernel_api(redcap_uri, post_body, config_options)
# 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")
# # TODO: convert all line endings to "\n"
# elapsed_seconds <- as.numeric(difftime(Sys.time(), start_time, units="secs"))

if( kernel$success ) {
col_types <- readr::cols(field_name = readr::col_character(), .default = readr::col_character())
Expand All @@ -99,17 +91,15 @@ redcap_metadata_read <- function(
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.
kernel$raw_text <- ""
kernel$raw_text <- "" # 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.
} else {
success <- FALSE #Override the 'success' determination from the http status code.
ds <- data.frame() #Return an empty data.frame
outcome_message <- paste0("The REDCap metadata export failed. The http status code was ", kernel$status_code, ". The 'raw_text' returned was '", kernel$raw_text, "'.")
success <- FALSE #Override the 'success' determination from the http status code.
ds <- data.frame() #Return an empty data.frame
outcome_message <- paste0("The REDCap metadata export failed. The http status code was ", kernel$status_code, ". The 'raw_text' returned was '", kernel$raw_text, "'.")
}
}
else {
ds <- data.frame() #Return an empty data.frame
outcome_message <- paste0("The REDCapR metadata export operation was not successful. The error message was:\n", kernel$raw_text)
} else {
ds <- data.frame() #Return an empty data.frame
outcome_message <- paste0("The REDCapR metadata export operation was not successful. The error message was:\n", kernel$raw_text)
}

if( verbose )
Expand Down
25 changes: 2 additions & 23 deletions R/redcap-read-oneshot-eav.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ redcap_read_oneshot_eav <- function(
) {
#TODO: NULL verbose parameter pulls from getOption("verbose")

# 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,}$")
checkmate::assert_atomic(records , any.missing=T, min.len=0)
Expand Down Expand Up @@ -169,25 +167,7 @@ redcap_read_oneshot_eav <- function(
if( nchar(events_collapsed ) > 0 ) post_body$events <- events_collapsed

# This is the important line that communicates with the REDCap server.
kernel <- kernel_api(redcap_uri, post_body, config_options)

# 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)))
kernel <- kernel_api(redcap_uri, post_body, config_options)

ds_metadata <- REDCapR::redcap_metadata_read(redcap_uri, token, forms_collapsed=forms_collapsed)$data
ds_variable <- REDCapR::redcap_variables(redcap_uri, token)$data
Expand Down Expand Up @@ -271,8 +251,7 @@ redcap_read_oneshot_eav <- function(
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.
kernel$raw_text <- ""
kernel$raw_text <- "" # 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.
} else {
success <- FALSE #Override the 'success' determination from the http status code.
ds_2 <- tibble::tibble() #Return an empty data.frame
Expand Down
20 changes: 2 additions & 18 deletions R/redcap-read-oneshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,24 +155,8 @@ redcap_read_oneshot <- function(
if( nchar(forms_collapsed ) > 0 ) post_body$forms <- forms_collapsed
if( nchar(events_collapsed ) > 0 ) post_body$events <- events_collapsed

# This is the important line that communicates with the REDCap server.
kernel <- kernel_api(redcap_uri, post_body, config_options)
# 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) # Convert all line-ending to linux-style
# 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)))

if( kernel$success ) {
col_types <- if( guess_type ) NULL else readr::cols(.default=readr::col_character())
Expand Down Expand Up @@ -211,7 +195,7 @@ redcap_read_oneshot <- function(
#
# ds <- base::as.data.frame(ds)

kernel$raw_text <- "" # 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.
kernel$raw_text <- "" # 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.
} else {
kernel$success <- FALSE #Override the 'success' determination from the http status code.
ds <- data.frame() #Return an empty data.frame
Expand Down

0 comments on commit 45e53bc

Please sign in to comment.