Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added helper function call_with_deduplication and use it to speed up path_file and path_dir for vectors with repeats. #425

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions R/path.R
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,16 @@ path_home_r <- function(...) {
#' @export
path_file <- function(path) {
is_missing <- is.na(path)
path[!is_missing] <- basename(path[!is_missing])
path[!is_missing] <- call_with_deduplication(basename, path[!is_missing])
as.character(path)
}


#' @rdname path_file
#' @export
path_dir <- function(path) {
is_missing <- is.na(path)
path[!is_missing] <- dirname(path[!is_missing])
path[!is_missing] <- call_with_deduplication(dirname, path[!is_missing])
as.character(path_tidy(path))
}

Expand Down
8 changes: 8 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,11 @@ is_installed <- function(pkg) {
mkdirp <- function(x) {
dir.create(x, showWarnings = FALSE, recursive = TRUE)
}


# Calls a function via deduplication of the input rather than each individual element.
call_with_deduplication <- function(func, x, ...) {
unique_x <- unique(x)

func(unique_x, ...)[match(x, unique_x)]
}