0

I am creating a bookdown document in which I provide a link for people to download the PDF, DOCX and TEX outputs of the current section they are looking at. All output documents are in a folder named "Compilation" and have the same name of the original Rmd documents they were retrieved.

For exemple, I have the file "1.2-Endowment-effect.Rmd" in which I coded:

Download Links: [[PDF]](./Compilation/1.2-Endowment-effect.pdf)
[[DOCX]](./Compilation/1.2-Endowment-effect.docx)
[[TEX]](./Compilation/1.2-Endowment-effect.tex)

I wanted to know if, instead of writting "1.2-Endowment-effect" in the code, there would be a way to take the name of the current Rmd file and add the extension. Something like:

Download Links: [[PDF]](./Compilation/NameCurrentFile.pdf)
[[DOCX]](./Compilation/NameCurrentFile.docx)
[[TEX]](./Compilation/NameCurrentFile.tex)

I have to repeat that process for lots of Rmd files, and I want to avoid always updating the links.

Thank you for your help.

abelb
  • 13
  • 2

1 Answers1

0

The output filenames can be generated with inline R expressions, e.g.,

Download Links: [PDF](./Compilation/`r xfun::with_ext(knitr::current_input(), 'pdf'`)

Please read the help pages of the functions xfun::with_ext() and knitr::current_input() if you are not familiar with them.

To include this part in all other Rmd documents, you can put the content in a child document named, say, _download.Rmd, and in your main Rmd documents, use

```{r, child='_download.Rmd'}
```
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • Thank you very much for you reply. I was not familiar with `xfun::with_ext()`, it is really convenient. However, I am still missing something with `knitr::current_input()` mechanism. When I run that code, the name that it takes is the name of the main Rmd file ("merged from all chapters" according to your book) which is in my case "Book-test" and doesn't take the name of the actual file that I'm working on ("1.2-Endowment-effect.Rmd"). The `current_input` fonction is not supposed to take the current Rmd file instead of the main one ? Thank you. – abelb Jun 10 '20 at 12:36
  • Oh, you were correct. My solution won't really work, unless you use `new_session: true` (i.e., use the "Knit and Merge" approach instead of the default "Merge and Knit"). Sorry. – Yihui Xie Aug 19 '20 at 04:19