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

Function to detect if path A is "in" path B #116

Closed
jennybc opened this Issue Jun 18, 2018 · 2 comments

Comments

Projects
None yet
1 participant
@jennybc
Copy link
Member

jennybc commented Jun 18, 2018

Came up in the fs-ification of usethis.

Something along these lines, but more general (my path B is hard-wired to proj_get(), which is guaranteed to be realized):

is_in_proj <- function(path) {
  ## realize path, if possible; use "as is", otherwise
  path <- tryCatch(path_real(path), error = function(e) NULL) %||% path
  identical(
    proj_get(),
    path_common(c(proj_get(), path))
  )
}
@jennybc

This comment has been minimized.

Copy link
Member Author

jennybc commented Jun 26, 2018

Related, determining if path A is same as path B. For example, resolving different ways to express "session temp dir". I know I can explicitly use path_real() myself, but should fs have helpers for detecting same-ness or inclusion?

library(fs)

setwd(path_temp())

getwd()
#> [1] "/private/var/folders/vr/gzrbtprx6ybg85y5pvwm1ct40000gn/T/Rtmp2gk7aQ"

path_temp()
#> /var/folders/vr/gzrbtprx6ybg85y5pvwm1ct40000gn/T/Rtmp2gk7aQ

path_common(c(getwd(), path_temp()))
#> /

path_common(path_real(c(getwd(), path_temp())))
#> /private/var/folders/vr/gzrbtprx6ybg85y5pvwm1ct40000gn/T/Rtmp2gk7aQ

Created on 2018-06-25 by the reprex package (v0.2.0.9000).

@jennybc

This comment has been minimized.

Copy link
Member Author

jennybc commented Jun 30, 2018

Maybe something like this, because it is handy if it works on paths that don't exist yet. Sorry about the crappy name.

is_in <- function(path, target) {
  path <-   tryCatch(path_real(path), error = function(e) path_tidy(path))
  target <- tryCatch(path_real(target), error = function(e) path_tidy(target))
  identical(target, path_common(c(path, target)))
}

@jimhester jimhester closed this in 1528958 Jul 20, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment