Reputation: 489
I record individual pieces of my work in R Markdown documents. I have many projects on the go but they tend to overlap, so it is not obvious from reading a document where it originated and therefore where it will be filed on my system.
I can automatically include in a document the folder in which it is filed by inserting:
```{r, echo=FALSE}
print(getwd())
```
but what I really want to do is automatically include the file name as well as the path to the folder. There is such a feature in MS Word, but is this possible in R Markdown?
These are documents created solely for my own personal use, and therefore storing the full file path is not a problem. Please do not tell me that what I want is inconsistent with reproducibility.
Upvotes: 2
Views: 2515
Reputation: 15419
The function knitr::current_input()
will return the name of the current R Markdown file, as explained here. You could combine this with the working directory as follows:
knitr::current_input(dir = TRUE)
Note, this will only properly work when you knit the document. It will return NULL if you try and run it within RStudio.
If you wanted an easy way of including this within your analysis, you could add the filepath as a subtitle to your document by including it in your YAML:
---
title: Your Document
subtitle: "`r knitr::current_input(dir = TRUE)`"
output: word_document
---
Upvotes: 4