Reputation: 37913
I'm interested in attaching metadata to saved objects, including the script that saved the data. Towards that end, I would like to catch the name of a script in the script itself.
When I have an Rmarkdown document as below and I knit it, the code chunk results in the name of the script that generated it. This is exactly what I want, however, this only works when the document is being knit. I would like to do the same if I'm executing the chunk in Rstudio in an interactive way.
---
title: "test"
author: "me"
date: "21/09/2020"
output: html_document
---
```{r}
if (interactive()) {
# Get the same as non-interactive version?
} else {
as.character(sys.call(1))[2]
}
```
Does anybody know how to catch the name of the current Rmarkdown script in an interactive session?
Upvotes: 1
Views: 214
Reputation: 30104
rstudioapi::getSourceEditorContext()
gives you information (a list) about the current file opened in the RStudio source editor. The path of the file is stored in the path
element of the list.
BTW, for non-interactive R sessions, knitr::current_input()
gives you the path to the source document being knitted (your sys.call(1)
approach sounds clever, though).
Upvotes: 2