Skip to content

Commit 5826445

Browse files
committed
Revise
1 parent 9122f45 commit 5826445

File tree

7 files changed

+65
-12
lines changed

7 files changed

+65
-12
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Collate:
118118
'expressions.R'
119119
'fill-variable.R'
120120
'filters.R'
121+
'findVariables.R'
121122
'folders.R'
122123
'fork-and-merge.R'
123124
'formula.R'

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ export(exportDataset)
191191
export(exportDeck)
192192
export(extendDataset)
193193
export(filter)
194+
export(findVariables)
194195
export(flattenOrder)
195196
export(flipArrays)
196197
export(folder)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# crunch 1.30.4 (Development Version)
22

3+
* New function `findVariables` accepts a Crunch dataset or variable folder and returns a data.frame whose rows correspond to variables and their location (#640).
4+
35
# crunch 1.30.3
46
* Fix typo which relied on partial argument matching when using the variable catalog cache
57
(#625, thanks @rossellhayes)

R/findVariables.R

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
#' Find variables and their paths in a Crunch dataset or folder
2-
#'
2+
#'
3+
#' Returns a data.frame whose rows correspond to Crunch variables found in \code{x}.
4+
#' By default, only top-level, non-hidden, non-private variables in \code{x} are returned.
5+
#'
36
#' @param x Crunch dataset or variable folder
4-
#' @param deep FALSE (the default) or TRUE; should subfolders
5-
#' @param include.hidden FALSE (default) or TRUE, should hidden be included in the result?
7+
#' @param deep Defaults to \code{FALSE}, \code{TRUE} recursively examines any subfolders as well
8+
#' @param include.hidden Defaults to \code{FALSE}, \code{TRUE} finds any hidden variables as well
9+
#' @param include.private Defaults to \code{FALSE}, \code{TRUE} finds any private variables as well
610
#'
7-
#' @return Data.frame with one row per Crunch variable and columns \code{alias}, \code{path}, \code{hidden}
11+
#' @return Data.frame with one row per Crunch variable and columns \code{alias} (Crunch variable alias),
12+
#' \code{path} (location of the variable, with " | " indicating nesting,
13+
#' e.g. "Foo | Bar" indicates that the variable can be found in the folder "Bar" and that "Bar" is located in folder "Foo"),
14+
#' \code{hidden} (\code{TRUE} or \code{FALSE}), \code{private} (\code{TRUE} or \code{FALSE})
815
#' @export
9-
findVariables <- function(x, deep = FALSE, include.hidden = FALSE) {
16+
findVariables <- function(x, deep = FALSE, include.hidden = FALSE, include.private = FALSE) {
1017
if (is.dataset(x)) {
1118
x <- cd(x, ".")
1219
startpath <- ""
@@ -19,27 +26,41 @@ findVariables <- function(x, deep = FALSE, include.hidden = FALSE) {
1926
halt("`deep` should be TRUE or FALSE")
2027
}
2128
if (!isTRUE(include.hidden) && !isFALSE(include.hidden)) {
22-
halt("`hidden` should be TRUE or FALSE")
29+
halt("`include.hidden` should be TRUE or FALSE")
30+
}
31+
if (!isTRUE(include.private) && !isFALSE(include.private)) {
32+
halt("`include.private` should be TRUE or FALSE")
2333
}
2434
if (!deep) {
2535
vars <- aliases(variables(x))
2636
nvars <- length(vars)
27-
res <- data.frame(alias = vars, path = rep(startpath, nvars), hidden = rep(FALSE, nvars))
37+
res <- data.frame(alias = vars, path = rep(startpath, nvars), hidden = rep(FALSE, nvars), private = rep(FALSE, nvars))
2838
return(res)
2939
}
3040
res <- .findVariables(x, startpath)
41+
res <- do.call(rbind, res)
3142
res$hidden <- rep(FALSE, nrow(res))
43+
res$private <- rep(FALSE, nrow(res))
3244
if (include.hidden) {
3345
hidden <- .findVariables(hiddenFolder(x), startpath)
46+
hidden <- do.call(rbind, hidden)
3447
hidden$hidden <- rep(TRUE, nrow(hidden))
48+
hidden$private <- rep(FALSE, nrow(hidden))
3549
res <- rbind(res, hidden)
3650
}
51+
if (include.private) {
52+
private <- .findVariables(privateFolder(x), startpath)
53+
private <- do.call(rbind, private)
54+
private$hidden <- rep(FALSE, nrow(private))
55+
private$private <- rep(TRUE, nrow(private))
56+
res <- rbind(res, private)
57+
}
3758
res
3859
}
3960

4061
.findVariables <- function(x, path) {
4162
vars <- variables(x)
42-
res <- data.frame(alias = aliases(vars), path = rep(path, length(vars)))
63+
res <- list(data.frame(alias = aliases(vars), path = rep(path, length(vars))))
4364
dirs <- x[types(x) %in% "folder"]
4465
if (length(dirs) == 0) {
4566
return(res)
@@ -53,5 +74,5 @@ findVariables <- function(x, deep = FALSE, include.hidden = FALSE) {
5374
}
5475
.findVariables(dirs[[i]], new_path)
5576
})
56-
rbind(res, do.call(rbind, res2))
77+
c(res, res2)
5778
}

R/folders.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ copyFolders <- function(source, target) {
389389
# Recursively get all variables below a folder
390390
# TODO: Use trampoline? https://community.rstudio.com/t/tidiest-way-to-do-recursion-safely-in-r/1408
391391
# My initial tests say it's slower, but is safer if we ever expect a large number of folders
392+
# NOTE: see ?findVariables for a similar exported function
392393
variablesBelowFolder <- function(folder) {
393394
vars <- variables(folder)
394395
dirs <- folder[types(folder) %in% "folder"]

man/findVariables.Rd

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/hide.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)