Skip to content

Commit

Permalink
Merge branch 'r-0.0-6' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Jan 16, 2017
2 parents dcf91f1 + 93491be commit c13c2ac
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 13 deletions.
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
Package: here
Title: A Simpler Way to Find Your Files
Version: 0.0-5
Version: 0.0-6
Authors@R: person("Kirill", "Müller", role = c("aut", "cre"), email = "[email protected]")
Description: Constructs paths to your project's files.
Imports: rprojroot (>= 1.1)
Imports: rprojroot (>= 1.2)
License: GPL-3
Encoding: UTF-8
LazyData: true
Date: 2016-10-29
Date: 2017-01-16
URL: https://github.com/krlmlr/here, http://krlmlr.github.io/here
BugReports: https://github.com/krlmlr/here/issues
Roxygen: list(markdown = TRUE)
RoxygenNote: 5.0.1.9000
Remotes: krlmlr/[email protected]
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(dr_here)
export(here)
export(set_here)
import(rprojroot)
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## here 0.0-6 (2017-01-16)

- New `set_here()` function that creates a `.here` file and talks about it by default (#1).
- New `dr_here()`, describes why `here()` has settled for a particular path.
- Recognize projectile projects and VCS roots.
- Using working directory as fallback produces wrong results, reverted.


## here 0.0-5 (2016-10-29)

- `remake` projects are also recognized by default.
Expand Down
84 changes: 77 additions & 7 deletions R/here.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,98 @@
#' Find your files
#'
#' Uses a reasonable heuristics to find your project's files, based on the
#' current working directory when the package is loaded.
#' `here()` uses a reasonable heuristics to find your project's files, based on
#' the current working directory at the time when the package is loaded.
#' Use it as a drop-in replacement for [file.path()], it will always locate the
#' files relative to your project root.
#'
#' This package is intended for interactive use only.
#' Use [rprojroot::has_file()] or the other functions in
#' the \pkg{rprojroot} package for more control.
#' the \pkg{rprojroot} package for more control,
#' or for package development.
#'
#' @evalRd format_root_section()
#'
#' @param ... \code{[character]}\cr
#' Path components below the project root, can be empty.
#' @export
#' @examples
#' here()
#' \dontrun{here("some/path/below/your/project/root.txt")}
here <- function(...) {
.root_env$f(...)
}

#' @rdname here
#' @description `dr_here()` shows a message that by default also includes the
#' reason why `here()` is set to a particular directory. Use this function
#' if `here()` gives unexpected results.
#' @param show_reason \code{[logical(1)]}\cr
#' Include reason in output of `dr_here()`, defaults to `TRUE`.
#' @export
dr_here <- function(show_reason = TRUE) {
message(format_dr_here(show_reason = show_reason))
}

format_dr_here <- function(show_reason) {
root <- .root_env$f()
paste0(
"here() starts at ", root,
if (show_reason) {
paste0(", because it ", get_root_desc(.root_env$crit, root))
}
)
}

#' @rdname here
#' @description `set_here()` creates an empty file named `.here`, by default
#' in the current directory. When `here` encounters such a file, it uses the
#' directory that contains this file as root. This is useful if none of the
#' default criteria apply.
#' @param path \code{[character(1)]}\cr
#' Directory where to create `.here` file, defaults to the current directory.
#' @param verbose \code{[logical(1)]}\cr
#' Verbose output, defaults to `TRUE`.
#' @export
set_here <- function(path = ".", verbose = TRUE) {
path <- normalizePath(path)
file_path <- file.path(path, ".here")

if (file.exists(file_path)) {
if (verbose) {
message("File .here already exists in ", path)
}
} else {
writeLines(character(), file_path)
if (verbose) {
message("Created file .here in ", path)
}
}

invisible(file_path)
}

is_here <- has_file(".here")

.root_env <- new.env(parent = emptyenv())

#' @import rprojroot
.onLoad <- function(libname, pkgname) {
crit <- is_rstudio_project | is_r_package | is_remake_project | from_wd

.root_env$f <- crit$make_fix_file()
.root_env$crit <- is_here | is_rstudio_project | is_r_package | is_remake_project | is_projectile_project | is_vcs_root
.root_env$f <- .root_env$crit$make_fix_file()
}

.onAttach <- function(libname, pkgname) {
packageStartupMessage("here() starts at ", .root_env$f())
packageStartupMessage(format_dr_here(show_reason = FALSE))
}

format_root_section <- function() {
paste(
"\\section{Project root}{",
"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:",
paste(format(.root_env$crit)[-1], collapse = "\n"),
"",
"Once established, the root directory doesn't change during the active R session. `here()` then appends the arguments to the root directory.",
"}",
sep = "\n"
)
}
50 changes: 47 additions & 3 deletions man/here.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c13c2ac

Please sign in to comment.