user4148072
user4148072

Reputation:

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

I am extending two questions I asked earlier:

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 in order to the .tex file to find the images stored in the temporary directory.

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. I thought this would be an easy solution, but 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 temporary directory on the Windows in the first place, even if the .tex file will contain the correct single forward slashes needed. (I believe) the same variable is used to denote the temporary directory location, as well as the image location in the .tex file, and they somehow have to have different slash directions.

I am a bit surprised this has not come up earlier (at least in my research). Maybe people do not have automatic image erasure when running knitr?

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.

Upvotes: 6

Views: 183

Answers (1)

Jack Wasey
Jack Wasey

Reputation: 3440

The code you present gives me a knitr warning about Sweave-specific expression: \SweaveOpts{concordance=TRUE} If I remove this, the page compiles for me (with numerous box errors) and produces a PDF with the graphics in Windows (and Ubuntu).

I'm also not clear why you want to specify your own temporary directory in the first place: doesn't knitr take care of this for you?

I freshly installed MikTex and updated it (numerous packages were 4 or 5 years old, immediately after installation).

I tried with R-devel and R-3.2.2:

> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_1.0.1 xtable_1.7-4  knitr_1.11   

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.0      digest_0.6.8     MASS_7.3-44      grid_3.2.2       plyr_1.8.3       gtable_0.1.2     formatR_1.2     
 [8] magrittr_1.5     evaluate_0.7.2   scales_0.3.0     stringi_0.5-5    reshape2_1.4.1   labeling_0.3     proto_0.3-10    
[15] tools_3.2.2      stringr_1.0.0    munsell_0.4.2    colorspace_1.2-6

sessionInfo()
R Under development (unstable) (2015-09-27 r69432)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_1.0.1 xtable_1.7-4  knitr_1.11   

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.0      digest_0.6.8     MASS_7.3-44      grid_3.3.0       plyr_1.8.3       gtable_0.1.2     formatR_1.2     
 [8] magrittr_1.5     evaluate_0.7.2   scales_0.3.0     stringi_0.5-5    reshape2_1.4.1   labeling_0.3     proto_0.3-10    
[15] tools_3.3.0      stringr_1.0.0    munsell_0.4.2    colorspace_1.2-6

Upvotes: 1

Related Questions