Overwrite chunk cache settings - r

I have a .Rnw document that relies on sourcing an R file which I occasionally change.
I have a lot of chunks that I want to update when I change this source .R file, but I don't want
them constantly recalculating as I'm doing regular edits/builds of the .Rnw file.
Is there any way to overwrite the cache=TRUE chunk option and force the document to recalculate when I change this .R file?

Related

Change same line of code in multiple RMD or R files

Is there a way to change the same line of code in multiple RMD or R script files instead of manually going in to each file and changing it?
also is there a way to change the first chunk of multiple RMD files to something else instead of manually doing it? The code in the first chunk of each RMD file isn't exactly the same but I want to change the entire first chunk of each RMD file to some other code.
One straightforward option here is to create a separate R script file that performs the process included in the R line or the RMD chunk and simply source that script file in all of the separate R script files or RMD files that use it. Then when you need to change that process you simply edit the external sourced script file.

Keep auxiliary TeX files when rendering a rmarkdown document

I am rendering two documents, that cross-reference items in each other. For that I am using the LaTeX package zref.
To make zref work, it needs the *.aux file of the documents which are created when calling pdflatex.
Unfortunately, using RStudio and its basic approach to render the document (the knit button, Cmd+Shift+K or rmarkdown::render()) these files will be deleted after the compilation was successful.
Unchecking the global option Tools -> Global Options -> Sweave -> Clean auxiliary output after compile does not help.
I know of two options to go around this:
Manually compile the tex file after the pdf was rendered.
Write a makefile that does that.
But is there another option I am not aware of?
rmarkdown::render() eventually calls tinytex::latexmk() to compile the intermediate .tex to .pdf. To preserve auxiliary files, you need tinytex::latexmk(..., clean = FALSE). One way to set clean = FALSE is through the global option options(tinytex.clean = FALSE). You can set this in either your .Rprofile or a code chunk of your Rmd document.
The RStudio option you mentioned is only for Sweave documents (.Rnw).

Put figure directly into Knitr document (without saving file of it in folder) Part 2

I am extending a question I recently posted here (Put figure directly into Knitr document (without saving file of it in folder)).
I am writing an R package that generates a .pdf file for users that outputs summarizations of data. I have a .Rnw script in the package (here, my MWE of it is called test.Rnw). The user can do:
1) knit("test.Rnw") to create a test.tex file
2) "pdflatex test.tex" to create the test.pdf summary output file.
The .Rnw file generates many images. Originally, these all got saved in the current directory. These images being saved to the directory (or maybe the .aux or .log files that get created upon calling pdflatex on the .tex file) just does not seem as tidy as it could be (since users must remember to delete these image files). Secondarily, I also worry that this untidiness may cause issues when scripts are run multiple time.
So, in my previous post, we improved the .Rnw file by saving the images to a temporary folder. I have been told the files in the temporary folder get deleted each time a new R session is opened. However, I still worry about certain things:
1) I feel I may need to insert a line, like the one on line 19:
system(sprintf("%s", paste0("rm -r ", temppath, "/*")))
to automatically delete the files in the temporary folder each time the .Rnw file is run (so that the images do not only get deleted each time R gets restarted). This will keep the current directory clean of the images, and the user will not have to remember to manually delete the images. However, I do not know if this "solution" will pass CRAN standards to have a line to delete files in the temporary folder. The reason is that it deletes files in the user's system, which could cause problems if other programs are writing files to the temporary folder. I feel I have read about CRAN not allowing files to be written/deleted from the user's computer for obvious reasons. How strict would CRAN be about such a practice? Is there a safe way to go about it?
2) If writing and deleting the image files in a temporary file will not work, what is another way to accomplish the same effect (run the script without having cumbersome image files created in the folder)? Is it possible to instead have the images directly embedded in the output file (not needing to be saved to any directory)? I am pretty sure this is not possible. However, I have been told it is possible to do so with .Rmd, and that I could convert my .Rnw to .Rmd. This may be difficult because the .Rnw file must follow certain formats (text and margins) for the correct output, and it is very long. Is it possible to make use of the .Rmd capability (of inserting images directly into the output) only for the chunks that generate images, without rewriting the entire .Rnw file?
Below is my MWE:
\documentclass[nohyper]{tufte-handout}
\usepackage{tabularx}
\usepackage{longtable}
\setcaptionfont{% changes caption font characteristics
\normalfont\footnotesize
\color{black}% <-- set color here
}
\begin{document}
<<setup, echo=FALSE>>=
library(knitr)
library(xtable)
library(ggplot2)
# Specify directory for figure output in a temporary directory
temppath <- tempdir()
# Erase all files in this temp directory first?
#system(sprintf("%s", paste0("rm -r ", temppath, "/*")))
opts_chunk$set(fig.path = temppath)
#
<<diamondData, echo=FALSE, fig.env = "marginfigure", out.width="0.95\\linewidth", fig.cap = "The diamond dataset has varibles depth and price.",fig.lp="mar:">>=
print(qplot(depth,price,data=diamonds))
#
<<echo=FALSE,results='asis'>>=
myDF <- data.frame(a = rnorm(1:10), b = letters[1:10])
print(xtable(myDF, caption= 'This data frame shows ten random variables from the distribution and a corresponding letter', label='tab:dataFrame'), floating = FALSE, tabular.environment = "longtable", include.rownames=FALSE)
#
Figure \ref{mar:diamondData} shows the diamonds data set, with the
variables price and depth.Table \ref{tab:dataFrame} shows letters a through j
corresponding to a random variable from a normal distribution.
\end{document}

Adding XeTeX to ess-swv-PDF

I am working with an .Rnw file with Hebrew in it, and I would like to compile the resulting .tex file using XeTeX. I work in Emacs, so I am using the Emacs Speaks Statistics (ESS) package, which allows a few options for compiling the .tex file. However, using XeTeX is not currently among the possible selections of ess-swv-PDF, accessed via M-n P.
I would like to add XeTeX to this list. Ideally, this added option would allow me to not only compile the .tex file using XeTeX but also run, as needed, BibTeX on the same fileā€”all from a buffer displaying the .Rnw file. This would be similar to texi2pdf, one of the current options of ess-swv-PDF.
This can be customized if you have ESS already installed: M-x customize-group select ess-sweave and edit ess-swv-pdflatex-commands to add "xelatex" to them. If you want to use xelatex or xetex as the default TeX engine, like I do, you can put that in front (see screenshot below).
Then press Apply and save this should save code into your .init.el or .emacs.el, depending on your setting.
Now when you go to your .Rnw file and hit M-n P you will see xelatex as default since you put it ahead of other commands. That is all to it.

How to remove .tex after running knit and texi2pdf

I have an R data frame that I run through knitr using the following code:
knit('reportTemplate.Rnw', 'file.tex') # creates a .tex file from the .Rnw one
texi2pdf('file.tex') # creates a .pdf file from the .tex one
Inside my R script, I want to remove 'file.tex' from my computer folder afterwards. How do I achieve this? It is important that I do this within my .R file, since those lines are actually inside a loop that generates 1000 different reports from that template.
There are a family of functions in R that allow the user to interact with the computer's file system. Run ?files to see functions that make it possible to, for instance, create, rename and remove files.
As noted by Josh O'Brien, in a comment to the OP, in this specific case, the command to be used is file.remove('file.tex').

Resources