Skip to content

Commit c13c2ac

Browse files
committed
Merge branch 'r-0.0-6' into production
2 parents dcf91f1 + 93491be commit c13c2ac

File tree

5 files changed

+138
-13
lines changed

5 files changed

+138
-13
lines changed

DESCRIPTION

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
Package: here
22
Title: A Simpler Way to Find Your Files
3-
Version: 0.0-5
3+
Version: 0.0-6
44
Authors@R: person("Kirill", "Müller", role = c("aut", "cre"), email = "krlmlr+r@mailbox.org")
55
Description: Constructs paths to your project's files.
6-
Imports: rprojroot (>= 1.1)
6+
Imports: rprojroot (>= 1.2)
77
License: GPL-3
88
Encoding: UTF-8
99
LazyData: true
10-
Date: 2016-10-29
10+
Date: 2017-01-16
1111
URL: https://github.com/krlmlr/here, http://krlmlr.github.io/here
1212
BugReports: https://github.com/krlmlr/here/issues
1313
Roxygen: list(markdown = TRUE)
1414
RoxygenNote: 5.0.1.9000
15+
Remotes: krlmlr/rprojroot@r-1.2

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(dr_here)
34
export(here)
5+
export(set_here)
46
import(rprojroot)

NEWS.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## here 0.0-6 (2017-01-16)
2+
3+
- New `set_here()` function that creates a `.here` file and talks about it by default (#1).
4+
- New `dr_here()`, describes why `here()` has settled for a particular path.
5+
- Recognize projectile projects and VCS roots.
6+
- Using working directory as fallback produces wrong results, reverted.
7+
8+
19
## here 0.0-5 (2016-10-29)
210

311
- `remake` projects are also recognized by default.

R/here.R

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,98 @@
11
#' Find your files
22
#'
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.
59
#' 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()
714
#'
815
#' @param ... \code{[character]}\cr
916
#' Path components below the project root, can be empty.
1017
#' @export
1118
#' @examples
1219
#' here()
20+
#' \dontrun{here("some/path/below/your/project/root.txt")}
1321
here <- function(...) {
1422
.root_env$f(...)
1523
}
1624

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+
1776
.root_env <- new.env(parent = emptyenv())
1877

1978
#' @import rprojroot
2079
.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()
2482
}
2583

2684
.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+
)
2898
}

man/here.Rd

Lines changed: 47 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)