Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
...
Expand Down
2 changes: 2 additions & 0 deletions R/crunch.R
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down
11 changes: 6 additions & 5 deletions R/export-dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,22 @@ 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(
pluralize("Duplicate variable reference", length(dup.aliases)), ": ",
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)
Expand Down
1 change: 1 addition & 0 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
1 change: 1 addition & 0 deletions man/envOrOption.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions tests/testthat/test-append-subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand All @@ -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}]}}}'
)
Expand Down
28 changes: 9 additions & 19 deletions tests/testthat/test-crunchbox.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)", {
Expand All @@ -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", {
Expand All @@ -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", {
Expand Down
28 changes: 10 additions & 18 deletions tests/testthat/test-export-dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -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}}'
)
})

Expand All @@ -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")
Expand All @@ -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(
Expand Down
7 changes: 2 additions & 5 deletions tests/testthat/test-merge-datasets.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, "}")
Expand Down
8 changes: 3 additions & 5 deletions tests/testthat/test-multitables.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
Loading