diff --git a/R/assertions.R b/R/assertions.R index 2aa8a09..2f87b4f 100644 --- a/R/assertions.R +++ b/R/assertions.R @@ -100,8 +100,23 @@ assert_timeZone_string <- function(time_zone) { # Check if include_questions are string(s) assert_includedQuestionIds_string <- function(include_questions) { - assertthat::assert_that(mode(include_questions) == "character", - msg = "'include_questions' must be a character vector." + assertthat::assert_that(mode(include_questions) == "character" | + (length(include_questions) == 1 && is.na(include_questions[1])), + msg = "if present, 'include_questions' must be a character vector or NA." + ) +} +# Check if include_embedded are string(s) +assert_includedembeddedDataIds_string <- function(include_embedded) { + assertthat::assert_that(mode(include_embedded) == "character" | + (length(include_embedded) == 1 && is.na(include_embedded[1])), + msg = "if present, 'include_embedded' must be a character vector or NA." + ) +} +# Check if include_metadata are string(s) +assert_includedsurveyMetadataIds_string <- function(include_metadata) { + assertthat::assert_that(mode(include_metadata) == "character" | + (length(include_metadata) == 1 && is.na(include_metadata[1])), + msg = "if present, 'include_metadata' must be a character vector or NA." ) } diff --git a/R/fetch_survey.R b/R/fetch_survey.R index 8629e75..edcc8be 100644 --- a/R/fetch_survey.R +++ b/R/fetch_survey.R @@ -107,6 +107,8 @@ fetch_survey <- function(surveyID, include_display_order = TRUE, limit = NULL, include_questions = NULL, + include_embedded = NULL, + include_metadata = NULL, save_dir = NULL, force_request = FALSE, verbose = TRUE, @@ -136,6 +138,8 @@ fetch_survey <- function(surveyID, start_date = start_date, end_date = end_date, include_questions = include_questions, + include_embedded = include_embedded, + include_metadata = include_metadata, save_dir = save_dir, unanswer_recode = unanswer_recode, unanswer_recode_multi = unanswer_recode_multi, @@ -177,6 +181,8 @@ fetch_survey <- function(surveyID, limit = limit, time_zone = time_zone, include_questions = include_questions, + include_embedded = include_embedded, + include_metadata = include_metadata, breakout_sets = breakout_sets ) diff --git a/R/utils.R b/R/utils.R index 33425b6..cc4e358 100644 --- a/R/utils.R +++ b/R/utils.R @@ -180,12 +180,24 @@ check_params <- function(...) { assert_limit_abovezero(args$limit) } } - # Check if includedQuestionIds is a string + # Check if include_questions is a string if ("include_questions" %in% names(args)) { if (!is.null(args$include_questions)) { assert_includedQuestionIds_string(args$include_questions) } } + # Check if include_embedded is a string + if ("include_questions" %in% names(args)) { + if (!is.null(args$include_embedded)) { + assert_includedembeddedDataIds_string(args$include_embedded) + } + } + # Check if include_metadata is a string + if ("include_questions" %in% names(args)) { + if (!is.null(args$include_metadata)) { + assert_includedsurveyMetadataIds_string(args$include_metadata) + } + } } #' Generate URL for specific API query by type and (if appropriate) ID @@ -272,6 +284,8 @@ create_raw_payload <- function(label = TRUE, unanswer_recode_multi = NULL, include_display_order = TRUE, include_questions = NULL, + include_embedded = NULL, + include_metadata = NULL, breakout_sets = NULL) { params <- as.list(environment()) @@ -286,19 +300,34 @@ create_raw_payload <- function(label = TRUE, unanswer_recode_multi = "multiselectSeenUnansweredRecode", include_display_order = "includeDisplayOrder", include_questions = "questionIds", + include_embedded = "embeddedDataIds", + include_metadata = "surveyMetadataIds", breakout_sets = "breakoutSets") - + # Fix a few parameters: if(!is.null(params$start_date)){ params$start_date <- paste0(start_date, "T00:00:00Z") } if(!is.null(params$end_date)){ params$end_date <- paste0(end_date, "T00:00:00Z") } + # If these are NA, the user wants none of this content, + # so exclude it via making it into an empty string vector: + if(length(params$include_questions) == 1 && + is.na(params$include_questions[1])){ + params$include_questions <- character() + } + if(length(params$include_embedded) == 1 && + is.na(params$include_embedded[1])){ + params$include_embedded <- character() + } + if(length(params$include_metadata) == 1 && + is.na(params$include_metadata[1])){ + params$include_metadata <- character() + } # Adjust names to fit API names: - names(params) <- names_crosswalk[names(params)] # Add in format param: @@ -310,8 +339,10 @@ create_raw_payload <- function(label = TRUE, } ) - # But "questionIds" needs to be boxed + # But the variable inclusion items needs to be boxed: params_ub["questionIds"] <- params["questionIds"] + params_ub["embeddedDataIds"] <- params["embeddedDataIds"] + params_ub["surveyMetadataIds"] <- params["surveyMetadataIds"] # Drop any NULL elements: params_ub <- purrr::discard(params_ub, ~ is.null(.x))