diff --git a/DESCRIPTION b/DESCRIPTION index 999c824..24c51e0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,11 +21,11 @@ License: MIT + file LICENSE URL: https://here.r-lib.org/, https://github.com/r-lib/here BugReports: https://github.com/r-lib/here/issues Imports: - rprojroot (>= 2.0.2) + rprojroot (>= 2.0.2), + fs Suggests: conflicted, covr, - fs, knitr, palmerpenguins, plyr, diff --git a/NAMESPACE b/NAMESPACE index f31f993..5f9d980 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,7 +1,9 @@ # Generated by roxygen2: do not edit by hand export(dr_here) +export(from_here) export(here) export(i_am) export(set_here) import(rprojroot) +importFrom(fs,path_rel) diff --git a/R/from_here.R b/R/from_here.R new file mode 100644 index 0000000..17f67cc --- /dev/null +++ b/R/from_here.R @@ -0,0 +1,41 @@ +#' +#' +#' @title +#' Get the relative path to a file +#' +#' @description +#' `from_here()` returns the path to a file or directory using `here()`, relative +#' to a parent directory (by default, the project root). +# +#' @param .path +#' The path to the target file or directory. +#' +#' @param .root +#' The path to the parent directory. Defaults to project root (`here()`). +#' +#' @importFrom fs path_rel +#' +#' @export +#' +#' @examples +#' from_here("an_example.csv") +#' \dontrun{ +#' myfilepath <- here("some", "path", "below", "your", "project", "root.txt") +#' >> "parent/path/some/path/below/your/project/root.txt" +#' myrelfilepath <- from_here(myfilepath) +#' >> "some/path/below/your/project/root.txt" +#' myrelfilepath <- from_here(myfilepath, here("some", "path") +#' >> "below/your/project/root.txt" +#' } + + +from_here <- function(.path, .root = here::here()) { + + # Using FS + fs::path_rel(.path, start = .root) + + # Using FS logic to retain leading `/` + #substr(.path, nchar(.root) + 1, nchar(.path)) +} + + diff --git a/man/from_here.Rd b/man/from_here.Rd new file mode 100644 index 0000000..421f404 --- /dev/null +++ b/man/from_here.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/from_here.R +\name{from_here} +\alias{from_here} +\title{Get the relative path to a file} +\usage{ +from_here(.path, .root = here::here()) +} +\arguments{ +\item{.path}{The path to the target file or directory.} + +\item{.root}{The path to the parent directory. Defaults to project root (\code{here()}).} +} +\description{ +\code{from_here()} returns the path to a file or directory using \code{here()}, relative +to a parent directory (by default, the project root). +} +\examples{ + from_here("an_example.csv") + \dontrun{ + myfilepath <- here("some", "path", "below", "your", "project", "root.txt") + >> "parent/path/some/path/below/your/project/root.txt" + myrelfilepath <- from_here(myfilepath) + >> "some/path/below/your/project/root.txt" + myrelfilepath <- from_here(myfilepath, here("some", "path") + >> "below/your/project/root.txt" + } +}