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

Should dir_walk have a glob argument? #157

Closed
cboettig opened this issue Dec 19, 2018 · 3 comments
Closed

Should dir_walk have a glob argument? #157

cboettig opened this issue Dec 19, 2018 · 3 comments
Labels
feature a feature request or enhancement

Comments

@cboettig
Copy link

It's not obvious to me why dir_walk and dir_map lack the glob and pattern argument present in the other list functions (dir_ls() and friends).

For example, I'd like to have travis check if all Rmds can render in a given git repo.

Something like:

fs::dir_walk(glob = "*.Rmd", fun = rmarkdown::render,  recursive = TRUE)

seems like a nice alternative to:

lapply(list.files(pattern = ".*.Rmd", recursive = TRUE, full.names = TRUE), rmarkdown::render)

In general, I can imagine many other scenarios where I'd want to walk a directory for it's side effect based on a globbing pattern. Maybe I'm just failing to understand the logic of dir_walk and dir_map, but they seem like attractive alternatives to piping dir_ls() version into a purrr function (and thus introducing a purrr dependency).

@jimhester
Copy link
Member

The implementation doesn't really make this easy to do, the mapping is done in C++, so there is no (easy) way to match against a glob or regex pattern without adding a dependency to a regex library.

You can always do the filtering in the function, e.g.

render_rmd <- function(file) {
  if (endsWith(file, ".Rmd")) {
    rmarkdown::render(file)
  }
}

fs::dir_walk(fun = render_rmd, recursive = TRUE)

@cboettig
Copy link
Author

thanks, that makes sense; if it's not in the C++. The filtering approach is nice (though I was hoping for a one-liner for my .travis.yml).

@jimhester
Copy link
Member

fs::dir_walk(fun = function(f) if (endsWith(f, ".Rmd")) rmarkdown::render(file), recursive = TRUE)

😉

@jimhester jimhester added the feature a feature request or enhancement label Mar 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants