Can't use underscore in render() input filename

Fairly new-ish to R, so apologies if I've completely overlooked something.

I have an R script that uses purrr::walk to create pdf reports using rmarkdown::render. In render() the input Rmd script file works fine when it has spaces in the name, but when I renamed it with underscores it states the directory it is contained in does not exist.

This code runs fine and produces the required output.

walk(
  .x = boards[[1]],
  ~ rmarkdown::render(
    input = "./rooms/report per board template.Rmd",
    output_file = glue("./rooms/Issues_{str_replace_all(.x, ' ', '_')}.pdf"),
    params = list(board = {.x})
  )
)

But this code does not run

walk(
  .x = boards[[1]],
  ~ rmarkdown::render(
    input = "./rooms/report_per_board_template.Rmd",
    output_file = glue("./rooms/Issues_{str_replace_all(.x, ' ', '_')}.pdf"),
    params = list(board = {.x})
  )
)

This error is produced

Error in map():
:information_source: In index: 1.
Caused by error:
! The directory './rooms' does not exist.

Any ideas?

what is the situation with the Rmd file currently , is it that you have it twice, once with, and once without underscores in the name ?

I only have it once without the underscores and that works fine. But if I rename it with underscores and change the input parameter to reflect that then I get the directory does not exist error

This is probably an incredibly dumb question, but are you sure a directory exists with exactly that name? What does dir.exists(input) return?

dir.exists("./rooms/")
[1] TRUE

As I said, once I remove all the underscores it runs fine. I even kept all the underscores in and tried with input = "./rooms/report_per_board template.Rmd" and that runs fine. It just seems to not like the input file having all underscores between words.

This is confusing; you kept all the underscores apart from the last one ? or you are saying that you devised a mismatch, and had success regardless ?

I've tried:

input = "./rooms/report_per_board_template.Rmd" - Doesn't work
input = "./rooms/report per board template.Rmd" - Does work
input = "./rooms/report_per_board template.Rmd" - Does work

As your input and output leaves in the same directory, does it work if you change the working dir before rendering (either using xfun::in_dir() or withr::with_dir()) ?

Example

walk(
  .x = boards[[1]],
  ~ xfun::in_dir("rooms", rmarkdown::render(
    input = "report_per_board_template.Rmd",
    output_file = glue("Issues_{str_replace_all(.x, ' ', '_')}.pdf"),
    params = list(board = {.x})
  ))
)

If so, there could be an issue somewhere in rmarkdown and path handling. If you could create a reproducible example (e.g. github repo or a zip folder), then open an issue in rmarkdown repo.

Thank you !

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.