Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need the filename of the Rnw when knitr runs in rStudio

When working on a reproducible research project, I would like to know the name of the Rnw file that is being run to use as an R variable.

This would be analogous to inserting an MSExcel filename in a footer

I am using RStudio Server on Ubuntu.

Thank you.

like image 995
Kate Avatar asked Jan 06 '14 18:01

Kate


People also ask

How do you use the knitr in RStudio?

If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it.

What is the function of knitr in creating Markdown document?

knitr is an engine for dynamic report generation with R. It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming.

What knitr function transforms R Markdown into pure R code?

What if you want to extract only the R code from your R Markdown report? For this, use the function knitr::purl() . The output from purl() can show no text, all text, or just the chunk options from your . Rmd file depending on the documentation argument.


2 Answers

knitr now has a method for this: current_input()

https://github.com/yihui/knitr/issues/701

like image 124
Kate Avatar answered Sep 20 '22 22:09

Kate


You can use the following two idioms to fetch the directory and name of the current file in knitr:

knitr:::.knitEnv$input.dir
knitr:::knit_concord$get("infile")

This is independent of RStudio, the knitr package is entirely responsible for this. These are private functions and variables that can change at any time without notice, so if you need something reliable, you might want to file an issue on GitHub.

like image 27
krlmlr Avatar answered Sep 20 '22 22:09

krlmlr