我正在提出我之前提出的两个问题:
-
Put figure directly into Knitr document (without saving file of it in folder)
-
Put figure directly into Knitr document (without saving file of it in folder) Part 2
我正在编写一个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文件 . 但是,我仍然允许用户选择违反此默认值,并在需要时保留这些中间文件 .
1 回答
你提供的代码给我一个关于特定于Sweave的表达式的编织警告:
\SweaveOpts{concordance=TRUE}
如果我删除它,页面为我编译(有很多盒子错误)并在Windows(和Ubuntu)中生成带有图形的PDF .我'm also not clear why you want to specify your own temporary directory in the first place: doesn' t
knitr
为你照顾这个?我刚刚安装了MikTex并对其进行了更新(许多软件包在安装后立即使用4或5年) .
我试过R-devel和R-3.2.2: