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

Add a vectorized wrapper for xfun::relative_path #1091

Merged
merged 2 commits into from
Mar 4, 2021
Merged
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

- Added the same CSS as in default Pandoc's template for when a CSL is used (#1045).

- No more warnings are thrown when passing several input files to `render_book(preview = TRUE)` (#1091).

## MINOR CHANGES

- `anchor_sections = TRUE` becomes the default for `bookdown::gitbook()`.
Expand Down
2 changes: 1 addition & 1 deletion R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ render_book = function(
# store output directory and the initial input Rmd name
opts$set(
output_dir = output_dir,
input_rmd = xfun::relative_path(input, dir = "."),
input_rmd = relative_path(input),
preview = preview
)

Expand Down
6 changes: 6 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ dir_create = function(path) {
dir_exists(path) || dir.create(path, recursive = TRUE)
}

# vectorized version to get relative path of multiple input
relative_path = function(inputs, dir = ".") {
f = Vectorize(xfun::relative_path, "x", USE.NAMES = FALSE)
f(x = inputs, dir = dir, use.. = TRUE, error = TRUE)
}

# a wrapper of file.path to ignore `output_dir` if it is NULL
output_path = function(...) {
dir = opts$get('output_dir')
Expand Down
5 changes: 5 additions & 0 deletions tests/testit/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,8 @@ assert('fence_theorems() converts the knitr engine syntax to fenced Divs', {
res = fence_theorems(text = old)
(unclass(res) %==% old)
})

assert("relative_path is vectorized", {
relative_path(c('foo/bar.txt', 'foo/baz.txt'), 'foo/') %==% c("bar.txt", "baz.txt")
relative_path('foo/bar.txt', 'foo') %==% "bar.txt"
})