|
1 | 1 | #' Find your files |
2 | 2 | #' |
3 | | -#' Uses a reasonable heuristics to find your project's files, based on the |
4 | | -#' current working directory when the package is loaded. |
| 3 | +#' `here()` uses a reasonable heuristics to find your project's files, based on |
| 4 | +#' the current working directory at the time when the package is loaded. |
| 5 | +#' Use it as a drop-in replacement for [file.path()], it will always locate the |
| 6 | +#' files relative to your project root. |
| 7 | +#' |
| 8 | +#' This package is intended for interactive use only. |
5 | 9 | #' Use [rprojroot::has_file()] or the other functions in |
6 | | -#' the \pkg{rprojroot} package for more control. |
| 10 | +#' the \pkg{rprojroot} package for more control, |
| 11 | +#' or for package development. |
| 12 | +#' |
| 13 | +#' @evalRd format_root_section() |
7 | 14 | #' |
8 | 15 | #' @param ... \code{[character]}\cr |
9 | 16 | #' Path components below the project root, can be empty. |
10 | 17 | #' @export |
11 | 18 | #' @examples |
12 | 19 | #' here() |
| 20 | +#' \dontrun{here("some/path/below/your/project/root.txt")} |
13 | 21 | here <- function(...) { |
14 | 22 | .root_env$f(...) |
15 | 23 | } |
16 | 24 |
|
| 25 | +#' @rdname here |
| 26 | +#' @description `dr_here()` shows a message that by default also includes the |
| 27 | +#' reason why `here()` is set to a particular directory. Use this function |
| 28 | +#' if `here()` gives unexpected results. |
| 29 | +#' @param show_reason \code{[logical(1)]}\cr |
| 30 | +#' Include reason in output of `dr_here()`, defaults to `TRUE`. |
| 31 | +#' @export |
| 32 | +dr_here <- function(show_reason = TRUE) { |
| 33 | + message(format_dr_here(show_reason = show_reason)) |
| 34 | +} |
| 35 | + |
| 36 | +format_dr_here <- function(show_reason) { |
| 37 | + root <- .root_env$f() |
| 38 | + paste0( |
| 39 | + "here() starts at ", root, |
| 40 | + if (show_reason) { |
| 41 | + paste0(", because it ", get_root_desc(.root_env$crit, root)) |
| 42 | + } |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 46 | +#' @rdname here |
| 47 | +#' @description `set_here()` creates an empty file named `.here`, by default |
| 48 | +#' in the current directory. When `here` encounters such a file, it uses the |
| 49 | +#' directory that contains this file as root. This is useful if none of the |
| 50 | +#' default criteria apply. |
| 51 | +#' @param path \code{[character(1)]}\cr |
| 52 | +#' Directory where to create `.here` file, defaults to the current directory. |
| 53 | +#' @param verbose \code{[logical(1)]}\cr |
| 54 | +#' Verbose output, defaults to `TRUE`. |
| 55 | +#' @export |
| 56 | +set_here <- function(path = ".", verbose = TRUE) { |
| 57 | + path <- normalizePath(path) |
| 58 | + file_path <- file.path(path, ".here") |
| 59 | + |
| 60 | + if (file.exists(file_path)) { |
| 61 | + if (verbose) { |
| 62 | + message("File .here already exists in ", path) |
| 63 | + } |
| 64 | + } else { |
| 65 | + writeLines(character(), file_path) |
| 66 | + if (verbose) { |
| 67 | + message("Created file .here in ", path) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + invisible(file_path) |
| 72 | +} |
| 73 | + |
| 74 | +is_here <- has_file(".here") |
| 75 | + |
17 | 76 | .root_env <- new.env(parent = emptyenv()) |
18 | 77 |
|
19 | 78 | #' @import rprojroot |
20 | 79 | .onLoad <- function(libname, pkgname) { |
21 | | - crit <- is_rstudio_project | is_r_package | is_remake_project | from_wd |
22 | | - |
23 | | - .root_env$f <- crit$make_fix_file() |
| 80 | + .root_env$crit <- is_here | is_rstudio_project | is_r_package | is_remake_project | is_projectile_project | is_vcs_root |
| 81 | + .root_env$f <- .root_env$crit$make_fix_file() |
24 | 82 | } |
25 | 83 |
|
26 | 84 | .onAttach <- function(libname, pkgname) { |
27 | | - packageStartupMessage("here() starts at ", .root_env$f()) |
| 85 | + packageStartupMessage(format_dr_here(show_reason = FALSE)) |
| 86 | +} |
| 87 | + |
| 88 | +format_root_section <- function() { |
| 89 | + paste( |
| 90 | + "\\section{Project root}{", |
| 91 | + "Starting with the current working directory during package load time, `here` will walk the directory hierarchy upwards until it finds a directory that satisfies at least one of the following conditions:", |
| 92 | + paste(format(.root_env$crit)[-1], collapse = "\n"), |
| 93 | + "", |
| 94 | + "Once established, the root directory doesn't change during the active R session. `here()` then appends the arguments to the root directory.", |
| 95 | + "}", |
| 96 | + sep = "\n" |
| 97 | + ) |
28 | 98 | } |
0 commit comments