File: listObjects.R

package info (click to toggle)
r-bioc-alabaster.base 1.6.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,652 kB
  • sloc: cpp: 11,377; sh: 29; makefile: 2
file content (96 lines) | stat: -rw-r--r-- 2,994 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#' List objects in a directory
#'
#' List all objects in a directory, along with their types.
#' 
#' @param dir String containing a path to a staging directory.
#' @param include.children Logical scalar indicating whether to include child objects.
#' 
#' @return \linkS4class{DataFrame} where each row corresponds to an object and contains;
#' \itemize{
#' \item \code{path}, the relative path to the object's subdirectory inside \code{dir}.
#' \item \code{type}, the type of the object
#' \item \code{child}, whether or not the object is a child of another object.
#' }
#'
#' If \code{include.children=FALSE}, metadata is only returned for non-child objects. 
#'
#' @author Aaron Lun
#'
#' @examples
#' tmp <- tempfile()
#' dir.create(tmp)
#'
#' library(S4Vectors)
#' df <- DataFrame(A=1:10, B=LETTERS[1:10])
#' saveObject(df, file.path(tmp, "whee"))
#'
#' ll <- list(A=1, B=LETTERS, C=DataFrame(X=1:5))
#' saveObject(ll, file.path(tmp, "stuff"))
#'
#' listObjects(tmp)
#' listObjects(tmp, include.children=TRUE)
#'
#' @export
#' @aliases listLocalObjects listDirectory
#' @importFrom S4Vectors DataFrame
listObjects <- function(dir, include.children=FALSE) {
    DataFrame(.traverse_directory_listing(dir, ".", include.children=include.children))
}

.traverse_directory_listing <- function(root, dir, already.child=FALSE, include.children=FALSE) {
    full <- file.path(root, dir)
    is.obj <- file.exists(file.path(full, "OBJECT"))
    if (is.obj) {
        paths <- dir
        types <- readObjectFile(full)$type
        childs <- already.child
    } else {
        paths <- character(0)
        types <- character(0)
        childs <- logical(0)
    }

    if (include.children || !is.obj) {
        more.dirs <- list.dirs(full, recursive=FALSE, full.names=FALSE)
        for (k in more.dirs) {
            if (dir != ".") {
                subdir <- file.path(dir, k)
            } else {
                subdir <- k
            }

            sub <- .traverse_directory_listing(root, subdir, already.child=(already.child || is.obj), include.children=include.children)
            paths <- c(paths, sub$path)
            types <- c(types, sub$type)
            childs <- c(childs, sub$child)
        }
    }

    list(path=paths, type=types, child=childs)
}


#######################################
########### OLD STUFF HERE ############
#######################################

#' @export
#' @importFrom jsonlite fromJSON
listDirectory <- function(dir, ignore.children = TRUE) {
    all.json <- list.files(dir, pattern="\\.json$", recursive=TRUE)
    out <- lapply(file.path(dir, all.json), fromJSON, simplifyVector=FALSE)
    names(out) <- vapply(out, function(x) x$path, "")

    if (ignore.children) {
        child <- vapply(out, function(x) isTRUE(x$is_child), TRUE)
        out <- out[!child]
    }

    out
}


# Soft-deprecated back-compatibility fixes.

#' @export
listLocalObjects <- function(..., ignoreChildren = TRUE) listDirectory(..., ignore.children = ignoreChildren)