From 412f821b6abfe20a9ab9d020cdf6636451c92159 Mon Sep 17 00:00:00 2001 From: Brennan Chapman Date: Fri, 28 Mar 2025 13:24:09 -0400 Subject: [PATCH 1/4] feat: add from_here() --- NAMESPACE | 1 + R/from_here.R | 39 +++++++++++++++++++++++++++++++++++++++ man/from_here.Rd | 28 ++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 R/from_here.R create mode 100644 man/from_here.Rd diff --git a/NAMESPACE b/NAMESPACE index f31f993..2952b6e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand export(dr_here) +export(from_here) export(here) export(i_am) export(set_here) diff --git a/R/from_here.R b/R/from_here.R new file mode 100644 index 0000000..c30b23a --- /dev/null +++ b/R/from_here.R @@ -0,0 +1,39 @@ +#' +#' +#' @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()`). +#' +#' @export +#' +#' @examples +#' from_here() +#' \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..5a76ebf --- /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() + \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 eea7edde5f7374fa8e2419542290fc9c031790fc Mon Sep 17 00:00:00 2001 From: Brennan Chapman Date: Fri, 28 Mar 2025 13:35:13 -0400 Subject: [PATCH 2/4] fix: add import of fs --- NAMESPACE | 1 + R/from_here.R | 2 ++ 2 files changed, 3 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index 2952b6e..5f9d980 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,3 +6,4 @@ 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 index c30b23a..8948a18 100644 --- a/R/from_here.R +++ b/R/from_here.R @@ -13,6 +13,8 @@ #' @param .root #' The path to the parent directory. Defaults to project root (`here()`). #' +#' @importFrom fs path_rel +#' #' @export #' #' @examples From 21783312d9b8ca404cb5b3efa954efaa4473f313 Mon Sep 17 00:00:00 2001 From: Brennan Chapman Date: Fri, 28 Mar 2025 13:52:26 -0400 Subject: [PATCH 3/4] fix: make fs an import Move fs from suggests to imports to support fs implementation of from_here() --- DESCRIPTION | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 8ffd97b..e65836b 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, From 0066ba86e952ef10b92fb734d2718b11d9da3127 Mon Sep 17 00:00:00 2001 From: Brennan Chapman Date: Fri, 1 Aug 2025 15:12:56 -0400 Subject: [PATCH 4/4] fix: error in example --- R/from_here.R | 2 +- man/from_here.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/from_here.R b/R/from_here.R index 8948a18..17f67cc 100644 --- a/R/from_here.R +++ b/R/from_here.R @@ -18,7 +18,7 @@ #' @export #' #' @examples -#' from_here() +#' from_here("an_example.csv") #' \dontrun{ #' myfilepath <- here("some", "path", "below", "your", "project", "root.txt") #' >> "parent/path/some/path/below/your/project/root.txt" diff --git a/man/from_here.Rd b/man/from_here.Rd index 5a76ebf..421f404 100644 --- a/man/from_here.Rd +++ b/man/from_here.Rd @@ -16,7 +16,7 @@ from_here(.path, .root = here::here()) to a parent directory (by default, the project root). } \examples{ - from_here() + from_here("an_example.csv") \dontrun{ myfilepath <- here("some", "path", "below", "your", "project", "root.txt") >> "parent/path/some/path/below/your/project/root.txt"