diff --git a/NAMESPACE b/NAMESPACE index e49b6eacb..0a1a6cc60 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -576,7 +576,6 @@ importFrom(crayon,italic) importFrom(crayon,make_style) importFrom(crayon,red) importFrom(crayon,underline) -importFrom(curl,curl_version) importFrom(grDevices,col2rgb) importFrom(grDevices,colors) importFrom(grDevices,rgb) diff --git a/R/api.R b/R/api.R index 0b65bee80..650cc55e5 100644 --- a/R/api.R +++ b/R/api.R @@ -306,14 +306,13 @@ set_crunch_config <- function(cfg = c( #' globally with [set_crunch_config()]. #' @export #' @keywords internal -#' @importFrom curl curl_version crunch_user_agent <- function(...) { ## Cf. httr:::default_ua ## Include versions of any of these packages, if attached pkgs <- ua_packages[ua_packages %in% loadedNamespaces()] ua <- c( # Also include the libcurl version - paste0("libcurl/", curl_version()$version), + paste0("libcurl/", curl::curl_version()$version), mapply(packageUA, pkgs, names(pkgs)), # And any extra bits provided ... diff --git a/R/crunch.R b/R/crunch.R index 0ac151c45..2674cde24 100644 --- a/R/crunch.R +++ b/R/crunch.R @@ -3,6 +3,8 @@ #' @importFrom httr config add_headers .onLoad <- function(lib, pkgname = "crunch") { + if (envOrOption("crunch.skip.startup", FALSE, expect_lgl = TRUE)) return() + setIfNotAlready( httpcache.on = TRUE, crunch.api = "https://app.crunch.io/api/", diff --git a/R/export-dataset.R b/R/export-dataset.R index d6a1f8723..58c0356d3 100644 --- a/R/export-dataset.R +++ b/R/export-dataset.R @@ -86,13 +86,11 @@ variablesFilter <- function(dataset, include.hidden = FALSE) { ## query from self. Adding it here so that we hit cache. dsvars <- ShojiCatalog(crGET(self(allvars), query = list(relative = "on"))) if (include.hidden || (length(allvars) != length(dsvars))) { - v <- structure(lapply(urls(allvars), function(x) list(variable = x)), - .Names = ids(allvars) - ) + v <- ids(allvars) ## Make sure that duplicate variables haven't been referenced (surely ## by accident). ## TODO: move this to a ShojiCatalog subset method? - dupes <- duplicated(names(v)) + dupes <- duplicated(v) if (any(dupes)) { dup.aliases <- unique(aliases(allvars[dupes])) halt( @@ -100,7 +98,10 @@ variablesFilter <- function(dataset, include.hidden = FALSE) { serialPaste(dup.aliases) ) } - return(list(`function` = "select", args = list(list(map = v)))) + return(list( + `function` = "frame_subset", + args = list(list(frame = "primary"), list(value = I(v)), list(value = NULL)) + )) } ## Else, return NULL return(NULL) diff --git a/R/misc.R b/R/misc.R index 312edba8d..3e4c4d742 100644 --- a/R/misc.R +++ b/R/misc.R @@ -216,6 +216,7 @@ vectorOrList <- function(obj, type) { #' | crunch.delimiter | R_CRUNCH_DELIMITER | "/" | What to use as a delimiter when printing folder paths | #' | crunch.check.updates | R_CRUNCH_CHECK_UPDATES | TRUE | Whether to check for updates to the crunch package | #' | crunch.debug | R_CRUNCH_DEBUG | FALSE | Whether to print verbose information for debugging | +#' | crunch.skip.startup | R_CRUNCH_SKIP_STARTUP | FALSE | Whether to skip package startup (useful for webR) | #' | test.verify.ssl | R_TEST_VERIFY_SSL | TRUE | Whether to verify ssl in curl during crunch tests | #' | crunch.stabilize.query | R_CRUNCH_STABILIZE_QUERY | FALSE | Whether to stabilize JSON objects for saving as `httptest` objects | #' | crunch.namekey.dataset | R_CRUNCH_NAMEKEY_DATASET | "alias" | What variable identifier (alias or name) to use for a dataset's variables | diff --git a/man/envOrOption.Rd b/man/envOrOption.Rd index 58ea195e0..8741586a4 100644 --- a/man/envOrOption.Rd +++ b/man/envOrOption.Rd @@ -56,6 +56,7 @@ the option name is in all capital letters, with "." replaced with crunch.delimiter \tab R_CRUNCH_DELIMITER \tab "/" \tab What to use as a delimiter when printing folder paths \cr crunch.check.updates \tab R_CRUNCH_CHECK_UPDATES \tab TRUE \tab Whether to check for updates to the crunch package \cr crunch.debug \tab R_CRUNCH_DEBUG \tab FALSE \tab Whether to print verbose information for debugging \cr + crunch.skip.startup \tab R_CRUNCH_SKIP_STARTUP \tab FALSE \tab Whether to skip package startup (useful for webR) \cr test.verify.ssl \tab R_TEST_VERIFY_SSL \tab TRUE \tab Whether to verify ssl in curl during crunch tests \cr crunch.stabilize.query \tab R_CRUNCH_STABILIZE_QUERY \tab FALSE \tab Whether to stabilize JSON objects for saving as \code{httptest} objects \cr crunch.namekey.dataset \tab R_CRUNCH_NAMEKEY_DATASET \tab "alias" \tab What variable identifier (alias or name) to use for a dataset's variables \cr diff --git a/tests/testthat/test-append-subset.R b/tests/testthat/test-append-subset.R index 814868ec8..8516c9a41 100644 --- a/tests/testthat/test-append-subset.R +++ b/tests/testthat/test-append-subset.R @@ -27,11 +27,9 @@ with_mock_crunch({ "https://app.crunch.io/api/datasets/1/batches/", '{"element":"shoji:entity","body":', '{"dataset":"https://app.crunch.io/api/datasets/3/",', - '"where":{"function":"select","args":[{"map":', - '{"66ae9881e3524f7db84970d556c34552":{"variable":', - '"https://app.crunch.io/api/datasets/3/variables/gender/"},', - '"f78ca47313144b57adfb495893968e70":{"variable":', - '"https://app.crunch.io/api/datasets/3/variables/birthyr/"}}}]}}}' + '"where":{"function":"frame_subset","args":[{"frame":', + '"primary"},{"value":["66ae9881e3524f7db84970d556c34552",', + '"f78ca47313144b57adfb495893968e70"]},{"value":null}]}}}' ) }) test_that("Append with variable selection and filter", { @@ -40,11 +38,9 @@ with_mock_crunch({ "https://app.crunch.io/api/datasets/1/batches/", '{"element":"shoji:entity","body":', '{"dataset":"https://app.crunch.io/api/datasets/3/",', - '"where":{"function":"select","args":[{"map":', - '{"66ae9881e3524f7db84970d556c34552":{"variable":', - '"https://app.crunch.io/api/datasets/3/variables/gender/"},', - '"f78ca47313144b57adfb495893968e70":{"variable":', - '"https://app.crunch.io/api/datasets/3/variables/birthyr/"}}}]},', + '"where":{"function":"frame_subset","args":[{"frame":', + '"primary"},{"value":["66ae9881e3524f7db84970d556c34552",', + '"f78ca47313144b57adfb495893968e70"]},{"value":null}]},', '"filter":{"function":"==","args":[{"variable":', '"https://app.crunch.io/api/datasets/3/variables/gender/"},{"value":1}]}}}' ) diff --git a/tests/testthat/test-crunchbox.R b/tests/testthat/test-crunchbox.R index 950a9977f..89da3b4ad 100644 --- a/tests/testthat/test-crunchbox.R +++ b/tests/testthat/test-crunchbox.R @@ -148,15 +148,9 @@ with_mock_crunch({ crunchBox(ds[2:5], filters = NULL), "https://app.crunch.io/api/datasets/1/boxdata/", '{"element":"shoji:entity","body":{"filters":[],"weight":null,', - '"where":{"function":"select","args":[{"map":{', - '"66ae9881e3524f7db84970d556c34552":', - '{"variable":"https://app.crunch.io/api/datasets/1/variables/gender/"},', - '"loc":', - '{"variable":"https://app.crunch.io/api/datasets/1/variables/location/"},', - '"949d2dc7e7a24e6090cc88bb92e1d2fb":', - '{"variable":"https://app.crunch.io/api/datasets/1/variables/mymrset/"},', - '"text":{"variable":"https://app.crunch.io/api/datasets/1/variables/textVar/"}', - "}}]}}}" + '"where":{"function":"frame_subset","args":[{"frame":"primary"},', + '{"value":["66ae9881e3524f7db84970d556c34552","loc",', + '"949d2dc7e7a24e6090cc88bb92e1d2fb","text"]},{"value":null}]}}}' ) }) test_that("Hidden variables are automatically 'selected' out (and weight is used)", { @@ -165,11 +159,9 @@ with_mock_crunch({ "https://app.crunch.io/api/datasets/3/boxdata/", '{"element":"shoji:entity","body":{"filters":[],', '"weight":"https://app.crunch.io/api/datasets/3/variables/birthyr/",', - '"where":{"function":"select","args":[{"map":{', - '"66ae9881e3524f7db84970d556c34552":', - '{"variable":"https://app.crunch.io/api/datasets/3/variables/gender/"},', - '"d7c21314ca9e453c93069168681a285c":', - '{"variable":"https://app.crunch.io/api/datasets/3/variables/starttime/"}}}]}}}' + '"where":{"function":"frame_subset","args":[{"frame":"primary"},', + '{"value":["66ae9881e3524f7db84970d556c34552","d7c21314ca9e453c93069168681a285c"]},', + '{"value":null}]}}}' ) }) test_that("Can override the current weight", { @@ -178,11 +170,9 @@ with_mock_crunch({ "https://app.crunch.io/api/datasets/3/boxdata/", '{"element":"shoji:entity","body":{"filters":[],', '"weight":null,', - '"where":{"function":"select","args":[{"map":{', - '"66ae9881e3524f7db84970d556c34552":', - '{"variable":"https://app.crunch.io/api/datasets/3/variables/gender/"},', - '"d7c21314ca9e453c93069168681a285c":', - '{"variable":"https://app.crunch.io/api/datasets/3/variables/starttime/"}}}]}}}' + '"where":{"function":"frame_subset","args":[{"frame":"primary"},', + '{"value":["66ae9881e3524f7db84970d556c34552","d7c21314ca9e453c93069168681a285c"]},', + '{"value":null}]}}}' ) }) test_that("Select filters in box", { diff --git a/tests/testthat/test-export-dataset.R b/tests/testthat/test-export-dataset.R index cbf8ad625..cf47cb428 100644 --- a/tests/testthat/test-export-dataset.R +++ b/tests/testthat/test-export-dataset.R @@ -82,10 +82,9 @@ with_mock_crunch({ expect_POST( write.csv(ds["gender"], file = ""), "https://app.crunch.io/api/datasets/1/export/csv/", - '{"filter":null,"where":{"function":"select",', - '"args":[{"map":{"66ae9881e3524f7db84970d556c34552":', - '{"variable":"https://app.crunch.io/api/datasets/1/variables/gender/"}}}]},', - '"options":{"use_category_ids":false}}' + '{"filter":null,"where":{"function":"frame_subset",', + '"args":[{"frame":"primary"},{"value":["66ae9881e3524f7db84970d556c34552"]},', + '{"value":null}]},"options":{"use_category_ids":false}}' ) }) @@ -103,14 +102,10 @@ with_mock_crunch({ url <- "https://app.crunch.io/api/datasets/3/export/csv/" post_request <- paste0( '{"filter":null,', - '"where":{"function":"select",', - '"args":[{"map":{', - '"66ae9881e3524f7db84970d556c34552":', - '{"variable":"https://app.crunch.io/api/datasets/3/variables/gender/"},', - '"f78ca47313144b57adfb495893968e70":', - '{"variable":"https://app.crunch.io/api/datasets/3/variables/birthyr/"},', - '"d7c21314ca9e453c93069168681a285c":', - '{"variable":"https://app.crunch.io/api/datasets/3/variables/starttime/"}}}]' + '"where":{"function":"frame_subset","args":[{"frame":"primary"},', + '{"value":["66ae9881e3524f7db84970d556c34552",', + '"f78ca47313144b57adfb495893968e70","d7c21314ca9e453c93069168681a285c"', + ']},{"value":null}]}' ) test_that("exporting hidden variables", { ds <- cachedLoadDataset("ECON.sav") @@ -130,12 +125,9 @@ with_mock_crunch({ subset_post <- paste0( '{"filter":null,', - '"where":{"function":"select",', - '"args":[{"map":{', - '"66ae9881e3524f7db84970d556c34552":', - '{"variable":"https://app.crunch.io/api/datasets/3/variables/gender/"},', - '"f78ca47313144b57adfb495893968e70":', - '{"variable":"https://app.crunch.io/api/datasets/3/variables/birthyr/"}}}]}' + '"where":{"function":"frame_subset","args":[{"frame":"primary"},', + '{"value":["66ae9881e3524f7db84970d556c34552",', + '"f78ca47313144b57adfb495893968e70"]},{"value":null}]}' ) # Hidden variables can be exported by name without include.hidden expect_POST( diff --git a/tests/testthat/test-merge-datasets.R b/tests/testthat/test-merge-datasets.R index 57f11f428..ac548e5ca 100644 --- a/tests/testthat/test-merge-datasets.R +++ b/tests/testthat/test-merge-datasets.R @@ -20,11 +20,8 @@ with_mock_crunch({ genderFilter, "}" ) testSubsetPayloadPart1 <- paste0( - '{"function":"select","args":[{"map":{', - '"66ae9881e3524f7db84970d556c34552":', - '{"variable":"https://app.crunch.io/api/datasets/3/variables/gender/"},', - '"f78ca47313144b57adfb495893968e70":', - '{"variable":"https://app.crunch.io/api/datasets/3/variables/birthyr/"}}}],', + '{"function":"frame_subset","args":[{"frame":"primary"},{"value":', + '["66ae9881e3524f7db84970d556c34552","f78ca47313144b57adfb495893968e70"]},{"value":null}],', '"frame":' ) testSubsetPayload <- paste0(testSubsetPayloadPart1, testPayload, "}") diff --git a/tests/testthat/test-multitables.R b/tests/testthat/test-multitables.R index 0074f42ba..1a7946f4d 100644 --- a/tests/testthat/test-multitables.R +++ b/tests/testthat/test-multitables.R @@ -379,11 +379,9 @@ if (tolower(Sys.info()[["sysname"]]) != "windows") { "https://app.crunch.io/api/datasets/3/multitables/ed30c4/export/", '{\"filter\":null,', '\"weight\":"https://app.crunch.io/api/datasets/3/variables/birthyr/",', - '\"options\":[],"where":{"function":"select","args":[{"map":', - '{"66ae9881e3524f7db84970d556c34552":', - '{"variable":"https://app.crunch.io/api/datasets/3/variables/gender/"},', - '"d7c21314ca9e453c93069168681a285c"', - ':{"variable":"https://app.crunch.io/api/datasets/3/variables/starttime/"}}}]}' + '\"options\":[],"where":{"function":"frame_subset","args":[{"frame":', + '"primary"},{"value":["66ae9881e3524f7db84970d556c34552",', + '"d7c21314ca9e453c93069168681a285c"]},{"value":null}]}}' ), "Accept: application/json" )