我正在提出我之前提出的两个问题:
我正在编写一个R包,为输出数据摘要的用户生成.pdf文件。我在包中有一个.Rnw脚本(这里,我的MWE称为test.Rnw)。用户可以这样做:
knit2pdf("test.Rnw", clean=T)
这使得这个过程变得简单,因为它会自动从.tex文件创建.pdf文件,并为它们删除不必要的文件(例如.aux和.log)。它还将任何图像存储到临时目录中(使用tempdir()),然后系统将这些图像合并到.tex和.pdf文件中后,将由系统定期擦除。这意味着他们也不必擦除图像文件。
以下是我的测试.RWW 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}
我应该注意到,实际上,我的包中还有另一个.Rnw文件,它通过以下方式调用test.Rnw文件:
knit2pdf("/inst/Rnw/test.Rnw","/path/test.tex",clean=T)
无论如何,我试图让这个软件包准备好提交给CRAN并遇到两个问题:
1)首先是一个更令人困惑的问题:上面的MWE代码似乎适用于Mac系统,但似乎不适用于Windows系统!在Windows上,生成的.pdf文件不包含图像。经过故障排除后,我想我已经找到了问题,但仍然无法找到解决方案。
基本上,在Windows上,似乎tempdir()命令将创建一个带有双反斜杠的路径,例如\\ this \\是\\ myPath。然后,在.tex文件中,到临时目录(包含图像)的路径是单反斜杠,例如\ this \是\ myPath。但是,这些应该是单正斜杠,例如/ this / is / myPath,以便.tex文件找到存储在临时目录中的图像。
实际上,如果我手动在Windows中的.tex文件中将反斜杠更改为正斜杠,那么我可以将其成功转换为成功包含图像的.pdf文件。
我不确定如何在我的语法中解决这个问题。我认为这将是一个简单的解决方案,但如果我只是做一些像:
# Specify directory for figure output in a temporary directory
temppath <- tempdir()
gsub("\\\\", "/", temppath)
然后,即使.tex文件包含所需的正确单个正斜杠,也无法将图像存储在Windows上的临时目录中。 (我相信)同一个变量用于表示临时目录位置,以及.tex文件中的图像位置,它们必须有不同的斜杠方向。
我有点意外,早些时候没有出现过(至少在我的研究中)。也许人们在运行knitr时没有自动图像擦除?
2)我想知道我是否可以在我的另一个.Rnw文件中添加第二行来调用:
knit2pdf("/inst/Rnw/test.Rnw","/path/test.tex",clean=T)
system(sprintf("%s", paste0("rm -r ", "/path/myFile.tex")))
这样也可以自动删除.tex文件。我试图确认CRAN标准可以接受这样的语法,因为它确实涉及从用户的计算机中删除文件(看起来像危险/恶意软件),尽管它专门指向它刚刚生成的.tex文件,并且所以它不应该删除任何对他们重要的东西。
*注意:我默认删除所有中间文件,因此用户只处理.pdf文件。但是,我仍然允许用户选择违反此默认值,并在需要时保留这些中间文件。
答案 0 :(得分:1)
你提供的代码给我一个关于特定于Sweave的表达式的编织警告:\SweaveOpts{concordance=TRUE}
如果我删除它,页面为我编译(有很多盒子错误)并在Windows中生成带有图形的PDF(和Ubuntu的)。
我也不清楚为什么要首先指定自己的临时目录:knitr
不会为你处理这个问题?
我刚安装了MikTex并对其进行了更新(许多软件包在安装后立即使用4或5年)。
我尝试使用R-devel和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