[R] Works on Mac, but not Windows: Using tempdir() to determine image location works with .tex file

Mark Sharp msharp at txbiomed.org
Mon Nov 2 01:26:34 CET 2015


Did you look at file.path()?


R. Mark Sharp, Ph.D.
Director of Primate Records Database
Southwest National Primate Research Center
Texas Biomedical Research Institute
P.O. Box 760549
San Antonio, TX 78245-0549
Telephone: (210)258-9476
e-mail: msharp at TxBiomed.org







> On Oct 31, 2015, at 3:28 PM, Green Stone <greenstone1114 at gmail.com> wrote:
> 
> 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:
> 
> knit2pdf("test.Rnw", clean=T)
> 
> This makes the process easy for them, because it automatically creates the
> .pdf file from the .tex file, and it erases unnecessary files for them
> (.aux and .log, for example). It also stores any images into a temporary
> directory (using tempdir()), which will then be erased routinely by the
> system after they have been incorporated into the .tex and .pdf file. This
> means they do not have to erase image files either.
> 
> Below is my test.Rnw 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()
> 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}
> 
> I should note that, in reality, there is another .Rnw file in my package
> that calls the test.Rnw file via:
> 
> knit2pdf("/inst/Rnw/test.Rnw","/path/test.tex",clean=T)
> 
> In any case, I am trying to get this package ready to be submitted to CRAN
> and have run across two problems:
> 
> 1) The more perplexing question first: The MWE code above seems to work on
> Mac Systems, but does not seem to work on Windows Systems! On Windows, the
> .pdf file that is generated does not contain the images. After
> troubleshooting, I think I have figured out the problem, but still cannot
> find a solution.
> 
> Basically, on Windows, it seems that the tempdir() command will create a
> pathway with double back-slashes, such as \this\is\myPath. Then, in the
> .tex file, the pathway to the temporary directory (that contains the
> images) are single back-slashes, such as \this\is\myPath. However, these
> should be single forward-slashes, such as /this/is/myPath.
> 
> Indeed, if I manually the change the backslashes to forward slashes in the
> .tex file in Windows, then I can successfully convert it to .pdf file that
> successfully contains the images.
> 
> I am unsure how to solve this in my syntax, however. If I simply do
> something like:
> 
> # Specify directory for figure output in a temporary directory
> temppath <- tempdir()
> gsub("\\\\", "/", temppath)
> 
> Then the images cannot be stored into the pathway on the Windows in the
> first place, even if the .tex file will contain the correct single forward
> slashes needed.
> 
> 2) I am wondering if it would acceptable for me to, in my other .Rnw file,
> add a second line to call:
> 
> knit2pdf("/inst/Rnw/test.Rnw","/path/test.tex",clean=T)
> system(sprintf("%s", paste0("rm -r ", "/path/myFile.tex")))
> 
> So that the .tex file can also be automatically erased. I am trying to
> confirm that such syntax would be acceptable by CRAN standards, as it does
> involve erasing a file from the user's computer (which could seem like
> dangerous/malware), although it points specifically at the .tex file it
> just generated, and so it should not be deleting anything important for
> them.
> 
> *Note: I am by default erasing all intermediary files so the user only
> deals with the .pdf file. However, I am still allowing users the option to
> go against this default, and keep these intermediary files, if needed.
> 
> I am grateful to hear any suggestions...
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list