|
| 1 | +# listToDepth ------------------------------------------------------------------ |
| 2 | + |
| 3 | +#' List Elements Recursively up to Depth |
| 4 | +#' |
| 5 | +#' @param path path to the element at which to start listing |
| 6 | +#' @param max_depth maximal depth level of which to list elements. A value of |
| 7 | +#' \code{0} means non-recursive listing, a value of \code{NA} represents fully |
| 8 | +#' recursive listing. |
| 9 | +#' @param full_info return only \code{path} and \code{isdir} information or |
| 10 | +#' the full information provided by \code{FUN(full_info = TRUE)}? |
| 11 | +#' @param FUN function called to get the listing of the element given in |
| 12 | +#' \code{path}. The function must accept a path as its first argument and it |
| 13 | +#' must define the argument \code{full_info} second. It may accept further |
| 14 | +#' arguments. It must always return a data frame. For \code{full_info = FALSE} |
| 15 | +#' the data frame must have columns \code{file} and \code{isdir} (is the |
| 16 | +#' "file" a directory?). For \code{full_info = TRUE} the function may return |
| 17 | +#' further columns. The function must provide an empty data frame with the |
| 18 | +#' expected columns when being called with \code{character()} as the first |
| 19 | +#' argument. The function is expected to set the attribute "failed" to the |
| 20 | +#' given path in case that the path could not be accessed (e.g. because of a |
| 21 | +#' broken internet connection if the listing is done remotely). See |
| 22 | +#' \code{kwb.utils:::listFiles} for an example implementation that somehow |
| 23 | +#' simulates the behaviour of the \code{\link{dir}} function. See |
| 24 | +#' \code{kwb.dwd::list_url()} for a more advanced usage of this function in |
| 25 | +#' order to recursively list the files on an FTP server (FTP = file transfer |
| 26 | +#' protocol). |
| 27 | +#' @param \dots further arguments passed to \code{FUN} |
| 28 | +#' @param depth start depth of recursion if \code{max_depth > 0}. This argument |
| 29 | +#' is for internal use and not intended to be set by the user! |
| 30 | +#' @param prob_mutate probability to alter the path so that it becomes useless. |
| 31 | +#' This is zero by default. Set the value only if you want to test how the |
| 32 | +#' function behaves if the listing of a path fails. |
| 33 | +#' @return data frame containing at least the columns \code{file} and |
| 34 | +#' \code{isdir}. If \code{full_info = TRUE} the result data frame may contain |
| 35 | +#' further columns, as provided by the function given in \code{FUN} for |
| 36 | +#' \code{full_info = TRUE}. |
| 37 | +#' @export |
| 38 | +#' @examples |
| 39 | +#' # Example list function provided in this package (file listing) |
| 40 | +#' FUN <- kwb.utils:::listFiles |
| 41 | +#' |
| 42 | +#' # The list function must provide empty records when no path is given. The |
| 43 | +#' # returned data frame must provide the columns "file" and "isdir" ... |
| 44 | +#' FUN() |
| 45 | +#' FUN(full_info = TRUE) |
| 46 | +#' |
| 47 | +#' # ... even when being called with an empty character vector |
| 48 | +#' FUN(character()) |
| 49 | +#' FUN(character(), full_info = TRUE) |
| 50 | +#' |
| 51 | +#' # Call the function recursively up to the given depth level |
| 52 | +#' kwb.utils:::listToDepth(".", max_depth = 1, FUN = FUN) |
| 53 | +#' |
| 54 | +listToDepth <- function( |
| 55 | + path, |
| 56 | + max_depth = 0L, |
| 57 | + full_info = FALSE, |
| 58 | + FUN = listFiles, |
| 59 | + ..., |
| 60 | + depth = 0, |
| 61 | + prob_mutate = 0 |
| 62 | +) |
| 63 | +{ |
| 64 | + # Helper function to mutate the path with a probability of "prob" |
| 65 | + mutate_or_not <- function(x, prob = 0.1) { |
| 66 | + stopifnot(inRange(prob, 0, 1)) |
| 67 | + # Add some nonsense to the path if the TRUE/FALSE coin lands on TRUE |
| 68 | + if (prob > 0 && sample(c(TRUE, FALSE), 1L, prob = c(prob, 1 - prob))) { |
| 69 | + x <- paste0(x, "blabla") |
| 70 | + } |
| 71 | + x |
| 72 | + } |
| 73 | + |
| 74 | + # kwb.utils::assignPackageObjects("kwb.utils") |
| 75 | + # kwb.utils::assignArgumentDefaults(listToDepth) |
| 76 | + # max_depth = 1;full_info=TRUE;set.seed(1) |
| 77 | + |
| 78 | + # Call the user-defined function FUN to list the elements at the given path |
| 79 | + info <- FUN(mutate_or_not(path, prob_mutate), full_info, ...) |
| 80 | + #info <- FUN(mutate_or_not(path, prob_mutate), full_info) |
| 81 | + |
| 82 | + # Which files represent directories? |
| 83 | + is_directory <- selectColumns(info, "isdir") |
| 84 | + |
| 85 | + # Are we already at maximum depth? |
| 86 | + at_max_depth <- ! is.na(max_depth) && (depth == max_depth) |
| 87 | + |
| 88 | + # Return the file list if no recursive listing is requested or if we are |
| 89 | + # already at maximum depth or if there are no directories. The function is |
| 90 | + # also returned from if info is empty (! any(is_directory) is TRUE). |
| 91 | + if (at_max_depth || ! any(is_directory)) { |
| 92 | + return(info) |
| 93 | + } |
| 94 | + |
| 95 | + # URLs representing directories |
| 96 | + directories <- selectColumns(info, "file")[is_directory] |
| 97 | + |
| 98 | + # Number of directories |
| 99 | + n_directories <- length(directories) |
| 100 | + |
| 101 | + # There must be directories if we arrive here! |
| 102 | + stopifnot(n_directories > 0L) |
| 103 | + |
| 104 | + # Indices to loop through |
| 105 | + indices <- stats::setNames(seq_along(directories), directories) |
| 106 | + |
| 107 | + # List all directories by calling this function recursively |
| 108 | + subdir_infos <- lapply(indices, function(i) { |
| 109 | + |
| 110 | + #i <- 1L |
| 111 | + |
| 112 | + # Show the progress |
| 113 | + cat(sprintf("%s%d/%d: ", space(depth), i, n_directories)) |
| 114 | + |
| 115 | + # Recursive call of this function |
| 116 | + listToDepth( |
| 117 | + path = paste0(assertFinalSlash(path), directories[i]), |
| 118 | + max_depth = max_depth, |
| 119 | + full_info = full_info, |
| 120 | + FUN = FUN, |
| 121 | + ..., |
| 122 | + depth = depth + 1, |
| 123 | + prob_mutate = prob_mutate |
| 124 | + ) |
| 125 | + }) |
| 126 | + |
| 127 | + # We need a template just in case that no data could be retrieved |
| 128 | + template <- FUN(full_info = full_info) |
| 129 | + |
| 130 | + # Merge data frames with info on files in subdirectories |
| 131 | + subdir_info <- mergeFileInfos(subdir_infos, template) |
| 132 | + |
| 133 | + # Prepend info on files at this level |
| 134 | + result <- rbind(info[! is_directory, ], subdir_info) |
| 135 | + |
| 136 | + # Collect information on URLs that failed to be accessed |
| 137 | + failed <- c(attr(info, "failed"), attr(subdir_info, "failed")) |
| 138 | + |
| 139 | + # Return the sorted file information with newly composed attribute "failed" |
| 140 | + structure(orderBy(result, "file"), failed = failed) |
| 141 | +} |
| 142 | + |
| 143 | +# mergeFileInfos --------------------------------------------------------------- |
| 144 | +mergeFileInfos <- function(file_infos, template) |
| 145 | +{ |
| 146 | + stopifnot(is.list(file_infos)) |
| 147 | + |
| 148 | + # Keep only non-empty data frames |
| 149 | + dfs <- file_infos[sapply(file_infos, nrow) > 0L] |
| 150 | + |
| 151 | + # Function to prepend a parent name "p" to column "file" in data frame "df" |
| 152 | + prepend_parent <- function(df, p) { |
| 153 | + parent <- assertFinalSlash(p) |
| 154 | + child <- selectColumns(df, "file") |
| 155 | + setColumns(df, file = paste0(parent, child), dbg = FALSE) |
| 156 | + } |
| 157 | + |
| 158 | + # Prepend the parent names to the filenames for the remaining data frames |
| 159 | + result <- do.call(rbind, mapply( |
| 160 | + prepend_parent, dfs, names(dfs), SIMPLIFY = FALSE, USE.NAMES = FALSE |
| 161 | + )) |
| 162 | + |
| 163 | + # If the result is NULL (no data frames to loop through) set the result to the |
| 164 | + # empty file info record |
| 165 | + result <- defaultIfNULL(result, template) |
| 166 | + |
| 167 | + # Collect the information on URLs that could not be listed |
| 168 | + failed <- unlist(excludeNULL(lapply(file_infos, attr, "failed"), FALSE)) |
| 169 | + |
| 170 | + # Merge the file lists returned for each directory |
| 171 | + # Return the vector of files with an attribute "failed" holding the merged |
| 172 | + # URLs of directories that could not be read |
| 173 | + structure(result, failed = failed) |
| 174 | +} |
| 175 | + |
| 176 | +# listFiles -------------------------------------------------------------------- |
| 177 | +listFiles <- function(path = ".", full_info = FALSE, ...) |
| 178 | +{ |
| 179 | + message("listing ", path) |
| 180 | + |
| 181 | + # Return empty data frame if path is empty |
| 182 | + if (length(path) == 0L) { |
| 183 | + return(listFiles(full_info = full_info)[FALSE, ]) |
| 184 | + } |
| 185 | + |
| 186 | + files <- dir(path, include.dirs = TRUE) |
| 187 | + |
| 188 | + result <- resetRowNames(file.info(file.path(path, files))) |
| 189 | + |
| 190 | + result$file <- files |
| 191 | + |
| 192 | + FUN <- if (full_info) moveColumnsToFront else selectColumns |
| 193 | + |
| 194 | + FUN(result, c("file", "isdir")) |
| 195 | +} |
0 commit comments