1
1
# ' 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
+ # '
3
6
# ' @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
6
10
# '
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})
8
15
# ' @export
9
- findVariables <- function (x , deep = FALSE , include.hidden = FALSE ) {
16
+ findVariables <- function (x , deep = FALSE , include.hidden = FALSE , include.private = FALSE ) {
10
17
if (is.dataset(x )) {
11
18
x <- cd(x , " ." )
12
19
startpath <- " "
@@ -19,27 +26,41 @@ findVariables <- function(x, deep = FALSE, include.hidden = FALSE) {
19
26
halt(" `deep` should be TRUE or FALSE" )
20
27
}
21
28
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" )
23
33
}
24
34
if (! deep ) {
25
35
vars <- aliases(variables(x ))
26
36
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 ) )
28
38
return (res )
29
39
}
30
40
res <- .findVariables(x , startpath )
41
+ res <- do.call(rbind , res )
31
42
res $ hidden <- rep(FALSE , nrow(res ))
43
+ res $ private <- rep(FALSE , nrow(res ))
32
44
if (include.hidden ) {
33
45
hidden <- .findVariables(hiddenFolder(x ), startpath )
46
+ hidden <- do.call(rbind , hidden )
34
47
hidden $ hidden <- rep(TRUE , nrow(hidden ))
48
+ hidden $ private <- rep(FALSE , nrow(hidden ))
35
49
res <- rbind(res , hidden )
36
50
}
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
+ }
37
58
res
38
59
}
39
60
40
61
.findVariables <- function (x , path ) {
41
62
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 ) )))
43
64
dirs <- x [types(x ) %in% " folder" ]
44
65
if (length(dirs ) == 0 ) {
45
66
return (res )
@@ -53,5 +74,5 @@ findVariables <- function(x, deep = FALSE, include.hidden = FALSE) {
53
74
}
54
75
.findVariables(dirs [[i ]], new_path )
55
76
})
56
- rbind (res , do.call( rbind , res2 ) )
77
+ c (res , res2 )
57
78
}
0 commit comments