image filename in Rmarkdown generated files

Hey y'all, I'm looking at the source files for a bookdown book I'm working on. Is there a way to control the name of the pdf images created when I render to tex? The naming convention seems to be {codeblock name}-1.pdf I'd really like (publisher has requested) the naming convention to not include the -1 in the name. I could, of course, script a process that changes the file names and the tex file and whatnot, but I'd really like to just throw a switch and have the names be different.

1 Like

Looking at the chunk options here, https://yihui.name/knitr/options/#plots, more specifically fig.process:

fig.process : ( NULL ) a function to post-process a figure file; it should take a filename, and return a character string as the new source of the figure to be inserted in the output

You could probably write a function that removes the numbering and then copies/moves the new file to the figure folder

If all the figures only have -1, then something like this should work (tested on PDF template).

knitr::opts_chunk$set(
  fig.process = function(filename) {
    new_filename <- stringr::str_remove(string = filename,
                                        pattern = "-1")
    fs::file_move(path = filename, new_path = new_filename)
    ifelse(fs::file_exists(new_filename), new_filename, filename)
  }
)
4 Likes

This topic was automatically closed 21 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.