Automatically update PDF as I write .Rnw file with knitr - r

I'm writing a .Rnw file in TextMate when using knitr to convert this file to a .tex file, then converting the .tex file a PDF. I'm wondering if there are any tools available that will automate this process, ie everytime anything updates in the .Rnw file, the PDF also updates? Using knitr to combine R and LaTeX code is great, but I think the conversion process to PDF takes up a lot of time, especially if changes to the .Rnw file are small but need to be cross-checked in the PDF file everytime they are made.

If you are on Linux use inotifywait for that. Most distributions have the package inotify-tools
Then do something like
while inotifywait -e close_write myfile.Rnw; do compile_myfile.sh; done
or
while inotifywait -e delete_self myfile.Rnw; do compile_myfile.sh; done
It depends on how your editor actually saves files. Just try and read the man page.
You have to write compile_myfile.sh of course to compile the .Rnw to .pdf - but I guess you know the commands.

Related

How to keep temporary _main.Rmd file in bookdown (when there is no error)

I am building a PDF from several RMarkdown files using Bookdown in Rstudio. In the process, Bookdown first compiles the different Rmd files into one big Rmd file (_main.Rmd), and then knits it into a PDF. If no errors are thrown, this temporary _main.Rmd file is usually deleted. I would like to keep the _main.Rmd file even when the file has successfully knitted into a PDF.
Is there any option I can add to _bookdown.yml to achieve this?
I just added this feature in the development version of bookdown, you may try:
remotes::install_github('rstudio/bookdown')
and set delete_merged_file: false in the config file _bookdown.yml.

Access R file functions from .Rmd file

I'm new in R and Rstudio and I'm making a little project. The fact is that i have on one hand an .R file with the code I want to execute. And on the other hand I've an .Rmd file that I should use to report my work, including the results of the execution of my code in the other file.
How can I access the results and/or functions from de .Rmd file to the .R file?
Thank you,
By default, your .Rmd file will have its working directory as wherever the .Rmd file is saved. You can use all of R's standard functions inside the .Rmd file, including source() to run a .R file. So if your files are in the same directory, you can include source("your_r_file.R") to run the .R file. If they are in different directories, you can use relative or absolute file paths (though you should try to avoid absolute file paths in case the .Rmd file is ever run on a different computer).
If you are using RStudio, I would strongly recommend using the "Projects" feature and the here package. The readme for the here package is quite good for explaining its benefits.
Source the R file in at the top of your .Rmd file like
```{r}
source("file-name.R")
```
and the functions/objects in that R file will be avilable

Running external .rmd files from a .rmd file - R

I want to run an external .rmd file(AKA rstudio notebook file) when I run a specific .rmd file. For example, the project X has a.Rmd, b.Rmd, and c.Rmd files. However, to run c.Rmd I need to run a.Rmd and b.Rmd first. Currently, each time I run c.Rmd I have to run a.Rmd and b.Rmd manually beforehand. Is there a codeline like when I run c.Rmd, it will automatically run a.Rmd and b.Rmd?, for example:
Inside c.Rmd:
execute(a.Rmd)
execute(b.Rmd)
I don't know the difference between an RStudio notebook file and any other R Markdown file, but this would work in the latter:
rmarkdown::render("a.Rmd")
rmarkdown::render("b.Rmd")

Using packrat libraries with knitr and the rstudio compile PDF button

As explained by Yihui Xie in this post, when one uses the Compile PDF button of the RStudio IDE to produce a PDF from a .Rnw file, knit() uses the globalenv() of a new R session. Is there a way that this new R session would use the packrat libraries of my project (even the version of knitr included in my packrat libraries) instead of my personal user libraries to ensure a maximum level of reproducibility? I guess that the new R session would have to be linked to the project itself, but I don't know how to do this efficiently.
I know I could directly use the knit() function instead of the Compile PDF button and, that way, knit() would use my current globalenv(), but I don't like this solution since it's less reproducible.
I think I got the problem myself, but I want to share with others who could confirm I'm right, and possibly help improve my solution.
My specific problem is that my .Rnw file is in a sub-directory of my whole project. When the Compile PDF button creates a new R session, it is created in this sub-directory, thus not finding the .Rprofile file that would initialize packrat. I think the easiest solution would be to create a .Rprofile file in my subdirectory which contains
temp <- getwd()
setwd("..")
source("packrat/init.R")
setwd(temp)
rm(temp)
I have to change the working directory at the project level before source("packrat/init.R") because the file itself refers to the directory...
Anybody can see a better solution?
P.,
I don't know if this solution works for even the knitr package, but I am 99% sure it works for all other packages as it seems to for me.
(I believe) I have a very similar problem. I have my project folder, but my working directory has always been the sub folder where my .rnw file is located, in a subdirectory of my project folder.
The link to Yihiu Xie's answer was very helpful.
Originally I wanted a project folder such as:
project-a/
working/
data/
datas.csv
analysis/
library.R
rscripts.R
rnw/
report.rnw
child/
preamble.rnw
packrat/
But I'm not sure if that is possible with packrat when my R library() calls are not in the working directory and packrat cannot parse the .rnw file (I call the library.R file from a chunck using source() in my .rnw file). A few notes:
I wanted to use a .Rproj file to open the project and have project-a/working as the working directory
If this was true then packrat can find the library.R script
But the .rnw file still defaults to its own working directory when compiling
I thought an .Rprofile with knitr::opts_knit$set(root.dir = "..") would work but I don't think it works for latex commands like input\, it defaults back to the directory containing the .rnw file
I thought this was insufficient because then you have two working directories, one for your r chunks and one for your latex!
Since .rnw always sets the working directory, I put my library.R script in the same directory as my .rnw file which creates the packrat folder in project-a/working/rnw. I am 99% sure this works because when I created the packrat folder in the project-a/working/rnw folder WITHOUT relocating the library.R file it received an error that no packages could be found and I could not compile the .rnw file.
project-a/
working/
data/
datas.csv
analysis/
rscripts.R
rnw/
report.rnw
library.R
packrat/
child/
preamble.rnw
Again, unless I am overlooking something or misunderstanding which packages are being used, this seems to have worked for me. Disclaimer here that I am relatively new to packrat.

Knit Rnw File into PDF and view in Emacs using polymode

I am trying using emacs recently. How can I compile my ".Rnw" file into PDF and view it after the compilation is completed. If I can pass the current file in an express knit2pdf(), it will compile. But I have no idea how to do it with subsequent action of viewing PDF.
It would be a lot better if I can compile the main document if I compile from a child rnw file.
Install rnw2pdf,
(require 'rnw2pdf)
in your init, then just M-x rnw2pdf in your Rnw buffer.
Note. Since the command opens the PDF for you, you may want to set in your init:
(setq rnw2pdf-viewer "/path/to/pdf-viewer")

Resources