query <- parseQueryString(session$clientData$url_search)
tlfb_id(query$id)
tlfb_v(query$v)
null_query <- (is.null(tlfb_id()) | is.null(tlfb_v()))
if (null_query) {
if(local_development) {
# tlfb_id("TEST_TLFBG_1")
tlfb_id("TEST_TLFB0002")
# tlfb_id("TEST_TLFB_NOSUBSTANCE")
tlfb_v(2)
null_query <- FALSE
}
if(! local_development) {
# Stop if missing parameters in URL.
shinyalert::shinyalert(
title = "Missing Parameters in URL",
text =
"No id and/or version provided in the URL.</br>
Try again adding '?v=XXX&id=YYY' at the end of the URL</br>
for example for v1:</br>
<a href = 'https://ucsd-cmig.shinyapps.io/hbcd-tlfb-dev/?v=1&id=TEST_TLFB0002'>https://ucsd-cmig.shinyapps.io/hbcd-tlfb-dev/?v=1&id=TEST_TLFB0002</a></br>
or for v2:</br>
<a href = 'https://ucsd-cmig.shinyapps.io/hbcd-tlfb-dev/?v=2&id=TEST_TLFB0002'>https://ucsd-cmig.shinyapps.io/hbcd-tlfb-dev/?v=2&id=TEST_TLFB0002</a>",
size = "m",
animation = FALSE,
closeOnEsc = FALSE,
closeOnClickOutside = FALSE,
showConfirmButton = FALSE,
html = TRUE,
type = "error",
immediate = TRUE
)
Sys.sleep(3)
stopApp("Missing Parameters in URL")
}
}
if (! null_query) {
# Connect.
shinyalert::shinyalert(
title = "Connection in Progress...",
text = "It might take a few seconds.",
size = "m",
animation = FALSE,
closeOnEsc = FALSE,
closeOnClickOutside = FALSE,
showConfirmButton = FALSE,
html = TRUE,
type = "info",
)
redcap_dta <- try(REDCapR::redcap_read(
redcap_uri = Sys.getenv("REDCAP_TLFB_URI"),
fields = selection_names_redcap,
token = Sys.getenv("REDCAP_TLFB_TOKEN"),
records = tlfb_id())$data
)
# Exit app if no connection.
connect_redcap_test <- ! inherits(redcap_dta, "try-error")
if (! connect_redcap_test) {
shinyalert::shinyalert(
title = "Can't connect to REDCap",
text = "Please check your internet connection, if the issue persist, report to the data team.",
size = "m",
animation = FALSE,
closeOnEsc = FALSE,
closeOnClickOutside = FALSE,
showConfirmButton = FALSE,
html = TRUE,
type = "error",
immediate = TRUE
)
Sys.sleep(3)
stopApp("Can't connect to REDCap")
}
# Test record existence and completeness.
if (tlfb_v() == 1) {
test_non_null <- redcap_dta |> nrow() > 0
ifelse(test_non_null,
test_event <- "v01_arm_1" %in% redcap_dta$redcap_event_name,
{
test_event <- FALSE
test_complete <- FALSE
test_record <- FALSE
})
ifelse(test_event,
test_complete <- redcap_dta[redcap_dta$redcap_event_name == "v01_arm_1", "assist_v1_complete"] == 2,
{
test_complete <- FALSE
test_record <- FALSE
})
if(test_complete) test_record <- TRUE
}
if (tlfb_v() == 2) {
test_non_null <- redcap_dta |> nrow() > 0
ifelse(test_non_null,
test_event <- "v02_arm_1" %in% redcap_dta$redcap_event_name,
{
test_event <- FALSE
test_complete <- FALSE
test_record <- FALSE
})
ifelse(test_event,
test_complete <- redcap_dta[redcap_dta$redcap_event_name == "v02_arm_1", "assist_v2_complete"] == 2,
{
test_complete <- FALSE
test_record <- FALSE
})
if(test_complete) test_record <- TRUE
}
if (! test_record) {
shinyalert::shinyalert(
title = "ASSIST record not present/complete",
text = "Please report to the data team.</br>
You will be disconnected from the app.",
size = "m",
animation = FALSE,
closeOnEsc = FALSE,
closeOnClickOutside = FALSE,
showConfirmButton = FALSE,
html = TRUE,
type = "error",
immediate = TRUE
)
Sys.sleep(3)
stopApp("ASSIST record not present/complete")
}
# Test connection to AWS.
connect_server <- try(
aws.s3::bucket_exists(
bucket = "hbcd-tlfb",
base_url = "storjshare.io",
region = "gateway.us1"),
silent = TRUE)
connect_server_test <- ! inherits(connect_server, "try-error")
if (! connect_server_test) {
shinyalert::shinyalert(
title = "Can't connect to AWS",
text = "Please check your internet connection, if the issue persist, report to data team.",
size = "m",
animation = FALSE,
closeOnEsc = FALSE,
closeOnClickOutside = FALSE,
showConfirmButton = FALSE,
html = TRUE,
type = "error",
immediate = TRUE
)
}
if(connect_redcap_test & test_record & connect_server_test) {
redcap_dta(redcap_dta)
# Test if a record exist on AWS.
test_existing_record <- list(
complete = aws.s3::head_object(
object = glue("{tlfb_id()}_v{tlfb_v()}_COMPLETE.rds"),
bucket = "hbcd-tlfb",
base_url = "storjshare.io",
region = "gateway.us1"
),
partial = aws.s3::head_object(
object = glue("{tlfb_id()}_v{tlfb_v()}_PARTIAL.rds"),
bucket = "hbcd-tlfb",
base_url = "storjshare.io",
region = "gateway.us1"
)
)
if (test_existing_record$complete) {
aws_file <- aws.s3::get_object(
object = glue("{tlfb_id()}_v{tlfb_v()}_COMPLETE.rds"),
bucket = "hbcd-tlfb",
base_url = "storjshare.io",
region = "gateway.us1"
)
tlfb_data_session <- readRDS(file = gzcon(rawConnection(aws_file)))
existing_record(tlfb_data_session)
text_exisiting_record <- "There is already a
<strong>COMPLETE record</strong> for this ID</br>
Previousely entered substances (not events) have been loaded."
}
if (! test_existing_record$complete & test_existing_record$partial) {
aws_file <- aws.s3::get_object(
object = glue("{tlfb_id()}_v{tlfb_v()}_PARTIAL.rds"),
bucket = "hbcd-tlfb",
base_url = "storjshare.io",
region = "gateway.us1"
)
tlfb_data_session <- readRDS(file = gzcon(rawConnection(aws_file)))
existing_record(tlfb_data_session)
text_exisiting_record <- "There is already a
<strong>PARTIAL record</strong> for this ID</br>
Previousely entered substances (not events) have been loaded."
}
if (! test_existing_record$complete & ! test_existing_record$partial) {
text_exisiting_record <- "There is no exisiting record for this ID. You can start entering data"
}
# Ask user to start sesssion.
shinyalert::shinyalert(
inputId = "start_session",
title = "Welcome to the Timeline Followback App!",
text = glue(
"{icon('check')}{text_exisiting_record}</br>
{icon('check')}ID {tlfb_id()}</br>
{icon('check')}ASSIST Version {tlfb_v()}</br>
{icon('check')}Connection to AWS server established"
),
size = "m",
animation = FALSE,
closeOnEsc = FALSE,
closeOnClickOutside = FALSE,
html = TRUE,
type = "success",
confirmButtonText = "Start Session",
immediate = TRUE
)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.