From 822e2975abe0d1bcf7f1a171c759af09d6acf8e5 Mon Sep 17 00:00:00 2001 From: Greg Freedman Ellis Date: Wed, 24 Feb 2021 11:33:34 -0600 Subject: [PATCH 1/3] allow `is.derived` on var tuples --- R/variable-derivation.R | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/R/variable-derivation.R b/R/variable-derivation.R index 9aa12393a..64f2a7cdf 100644 --- a/R/variable-derivation.R +++ b/R/variable-derivation.R @@ -141,9 +141,17 @@ setMethod("derivation<-", c("CrunchVariable", "NULL"), integrateDerivedVar) #' @aliases is.derived #' @export setMethod("is.derived", "CrunchVariable", function(x) { - isTRUE(tuple(x)$derived) + is.derived(tuple(x)) }) +#' @rdname derivations +#' @aliases is.derived +#' @export +setMethod("is.derived", "VariableTuple", function(x) { + isTRUE(x$derived) +}) + + #' @rdname derivations #' @aliases is.derived<- #' @export From 06eec1d4cb3792c4a69176fc31a68a3982ad13b2 Mon Sep 17 00:00:00 2001 From: Greg Freedman Ellis Date: Wed, 24 Feb 2021 11:47:25 -0600 Subject: [PATCH 2/3] and is.derived for vars catalog --- R/variable-derivation.R | 7 +++++++ man/derivations.Rd | 6 ++++++ .../api/datasets/1/variables-d118fa.json | 21 ++++++++++++------- tests/testthat/test-derive.R | 7 +++++++ 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/R/variable-derivation.R b/R/variable-derivation.R index 64f2a7cdf..791cab3a3 100644 --- a/R/variable-derivation.R +++ b/R/variable-derivation.R @@ -151,6 +151,13 @@ setMethod("is.derived", "VariableTuple", function(x) { isTRUE(x$derived) }) +#' @rdname derivations +#' @aliases is.derived +#' @export +setMethod("is.derived", "VariableCatalog", function(x) { + vapply(x, function(vartuple) vartuple$derived, logical(1)) +}) + #' @rdname derivations #' @aliases is.derived<- diff --git a/man/derivations.Rd b/man/derivations.Rd index 73e597151..cfea198eb 100644 --- a/man/derivations.Rd +++ b/man/derivations.Rd @@ -10,6 +10,8 @@ \alias{derivation<-,CrunchVariable,ANY-method} \alias{derivation<-,CrunchVariable,NULL-method} \alias{is.derived,CrunchVariable-method} +\alias{is.derived,VariableTuple-method} +\alias{is.derived,VariableCatalog-method} \alias{is.derived<-,CrunchVariable,logical-method} \title{Get or set a derived variable's expression} \usage{ @@ -29,6 +31,10 @@ is.derived(x) <- value \S4method{is.derived}{CrunchVariable}(x) +\S4method{is.derived}{VariableTuple}(x) + +\S4method{is.derived}{VariableCatalog}(x) + \S4method{is.derived}{CrunchVariable,logical}(x) <- value } \arguments{ diff --git a/mocks/app.crunch.io/api/datasets/1/variables-d118fa.json b/mocks/app.crunch.io/api/datasets/1/variables-d118fa.json index d5770d795..6e4d488a3 100644 --- a/mocks/app.crunch.io/api/datasets/1/variables-d118fa.json +++ b/mocks/app.crunch.io/api/datasets/1/variables-d118fa.json @@ -11,7 +11,8 @@ "type": "categorical", "id": "66ae9881e3524f7db84970d556c34552", "notes": "", - "description": "Gender" + "description": "Gender", + "derived": false }, "birthyr/": { "name": "Birth Year", @@ -20,7 +21,8 @@ "type": "numeric", "id": "f78ca47313144b57adfb495893968e70", "description": null, - "notes": "Asked instead of age" + "notes": "Asked instead of age", + "derived": false }, "starttime/": { "name": "starttime", @@ -31,7 +33,8 @@ "description": "Interview Start Time", "notes": "", "resolution": "D", - "rollup_resolution": "s" + "rollup_resolution": "s", + "derived": false }, "mymrset/": { "name": "mymrset", @@ -47,7 +50,8 @@ "mymrset/subvariables/subvar1/", "mymrset/subvariables/subvar3/" ], - "subvariables_catalog": "mymrset/subvariables/" + "subvariables_catalog": "mymrset/subvariables/", + "derived": false }, "catarray/": { "name": "Cat Array", @@ -62,7 +66,8 @@ "mymrset/subvariables/subvar1/", "mymrset/subvariables/subvar3/" ], - "subvariables_catalog": "mymrset/subvariables/" + "subvariables_catalog": "mymrset/subvariables/", + "derived": false }, "textVar/": { "name": "Text variable ftw", @@ -71,7 +76,8 @@ "type": "text", "id": "text", "description": "Budějovický Budvar", - "notes": "" + "notes": "", + "derived": false }, "location/": { "name": "Categorical Location", @@ -80,7 +86,8 @@ "type": "categorical", "id": "loc", "description": "Location test", - "notes": "" + "notes": "", + "derived": false } }, "orders": { diff --git a/tests/testthat/test-derive.R b/tests/testthat/test-derive.R index 7bb40b1ad..cc1bffaf1 100644 --- a/tests/testthat/test-derive.R +++ b/tests/testthat/test-derive.R @@ -17,6 +17,13 @@ with_mock_crunch({ expect_prints(derivation(birthyrPlus), "Crunch expression: birthyr + 100") }) + test_that("is.derived works on variable caatalog", { + expect_equal( + is.derived(variables(ds)), + rep(FALSE, length(variables(ds))) + ) + }) + test_that("derivation returns NULL if the variable is not derived", { expect_null(derivation(ds$birthyr)) }) From 8db0131d67d7bcc042a3554ffe348f9f32d02b81 Mon Sep 17 00:00:00 2001 From: Greg Freedman Ellis Date: Mon, 1 Mar 2021 10:27:20 -0600 Subject: [PATCH 3/3] Allow `folder()` to work on datasets --- R/folders.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/folders.R b/R/folders.R index 27fe6e2c1..c909c56c3 100644 --- a/R/folders.R +++ b/R/folders.R @@ -228,6 +228,8 @@ folder <- function(x) { cls <- class(x) } else if (is.variable(x)) { cls <- "VariableFolder" + } else if (is.dataset(x)) { + cls <- "ProjectFolder" } else { halt("No folder for object of class ", class(x)) }