0

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}
Community
  • 1
  • 1
  • I think you should isolate the problem with running the script multiple times rather than assume it's the images folder. (The images seem an unlikely culprit.) Does the same script cause problems if you set your chunk options to `fig.keep = "none"`? Have you tried deleting the `aux` and `log` files automatically by calling `knit2pdf(..., clean = T)`? – Gregor Thomas Sep 28 '15 at 02:18
  • @Gregor: I greatly appreciate your input! I had not heard of the knit2pdf() before. Would it (and the clean=T) option work on all platforms? Also, I am so pleased to see there is an option like fig.keep. It does seem to erase the figure folder. When I add in the option fig.keep="none", and then run knit2pdf(...,clean=T), it seems good in that .aux, .log, and figure file are all gone, and what remains is the .tex and .pdf file. However, the .pdf file does not have the figure in it! –  Sep 29 '15 at 04:00
  • @Gregor Yes, I still trying to isolate the problem. However, there are actually multiple .Rnw files that produce slightly different output summary .pdf files. And, if I run on type of .Rnw file, and then another .Rnw after it, I think I see images that should be in the first .pdf file still showing in the second type of .pdf file. –  Sep 29 '15 at 04:09
  • You should read up a little on these options. `knit2pdf` knits (runs the R code and replaces it with formatted code / output) to a TeX file, and then calls `tools::texi2pdf` to run LaTeX and produce a PDF. The `clean = T` is passed on by `knit2pdf` to `texi2pdf`, so all it does is clean up the latex byproducts (like the aux and log files). It should work on all systems. – Gregor Thomas Sep 29 '15 at 05:37
  • `fig.keep` is a knitr chunk option, which if set to `"none"` tells knitr to ignore any plots produced in the chunk. It won't clean up a `figures` directory, it will prevent any figures from being created in the first place (and thus prevent the creation of/overwriting of a figures directory). – Gregor Thomas Sep 29 '15 at 05:39
  • 2
    Your question is that if you create your output several times successively, it "seems to cause problems" (**super vague** description!). You don't know what's causing these problems, but it might be (a) figures folder or (b) `aux` and `log` files or (c) something else. The settings I've recommended should let you figure out which one of those three is the problem (and if it's (b) it gives you the tool to solve it). I'd suggest you narrow down your problem to one of those three, then update your question with that info and with a less vague description of the problem. – Gregor Thomas Sep 29 '15 at 05:46
  • @Gregor: Sorry if my wording was poor. Yes, that is part of the question. But I also state "The generation of these images in the current directory also just does not seem as tidy as it could be (since users must remember to delete these image files)". I mainly would like to keep the output tidy, for tidy purposes and secondarily for prevention of this problem (which I agree is a vague description, and I should have emphasized that was kind of secondary). –  Sep 29 '15 at 16:55
  • @Gregor So, just to verify: fig.keep="none" cannot really be used in this situation, because it will delete files before they are inserted into the .pdf. I did do some reading on the options, but I think what confused me is that I thought it was a solution to remove the folder, .aux, and .log to keep it all tidy (and possibly secondarily prevent problems). I now believe your intentions were more about testing for my vague problem, rather than a solution to keep it tidy (justifiably so because looking back, my wording was unclear!) –  Sep 29 '15 at 16:59
  • 1
    @Gregor I adore the knit2pdf(..., clean=T) option and will use it (Thank you!). But it probably is not too easy/possible to clean the images automatically at the same time, right (for tidiness, not necessarily my vague problem :o))? Because the only suggestions I have for automatic image folder removal is 1) convert .Rnw to .Rmd, 2) fig.keep="none", and 3) saving image folder to a temporary directory (which I worry may not pass CRAN due to my reasoning above). But, as you can see, I am not very skilled to really be sure of that all just yet! Thanks again. –  Sep 29 '15 at 17:01
  • Also, I hope it is appropriate that I updated my poor wording in the question to prevent this miscommunication. Sorry again. –  Sep 29 '15 at 17:06
  • Let's be clear that `fig.keep = "none"` was a suggestion for a debugging technique to isolate whether the figures directory is related to your vague problem. Obviously you want figures in your final document so you will need to keep them around longer than the code chunk. – Gregor Thomas Sep 29 '15 at 17:12
  • 2
    Editing your question for clarity is entirely appropriate. Let's also be very clear that when you say "I also worry that this untidiness may cause issues when scripts are run multiple times," this worry is **entirely unfounded**---it's very common to knit a doc multiple times in the same working directory and I've never had or heard of problems with that. You can see [open bugs in knitr](https://github.com/yihui/knitr/issues) and this is not one. If you can reproduce the vague problem, I bet you can reproduce it with `fig.keep = 'none'` and thus rule out the `figures` file as the cause. – Gregor Thomas Sep 29 '15 at 17:13
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90921/discussion-between-gregor-and-joseph-hudson). – Gregor Thomas Sep 29 '15 at 17:32

0 Answers0