将图形直接放入Knitr文档(不将其文件保存在文件夹中)第3部分
我将延续我先前提出的两个问题:将图形直接放入Knitr文档(不将其文件保存在文件夹中)第3部分,r,windows,image,macos,knitr,R,Windows,Image,Macos,Knitr,我将延续我先前提出的两个问题: 我正在编写一个R包,它为输出数据摘要的用户生成一个.pdf文件。我在包中有一个.Rnw脚本(这里,我的MWE称为test.Rnw)。用户可以执行以下操作: knit2pdf("test.Rnw", clean=T) 这使得这个过程对他们来说很容易,因为它会自动从.tex文件创建.pdf文件,并为他们删除不必要的文件(.aux和.log,例如)。它还将所有图像存储到一个临时目录中(使用tempdir()),在将它们合并到.tex和.pdf文件中后,系统将定
knit2pdf("test.Rnw", clean=T)
这使得这个过程对他们来说很容易,因为它会自动从.tex文件创建.pdf文件,并为他们删除不必要的文件(.aux和.log,例如)。它还将所有图像存储到一个临时目录中(使用tempdir()),在将它们合并到.tex和.pdf文件中后,系统将定期擦除该临时目录。这意味着他们也不必擦除图像文件
以下是我的测试。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}
在任何情况下,我都试图将此包提交给CRAN,但遇到了两个问题:
1) 首先,更令人困惑的问题是:上面的MWE代码似乎在Mac系统上工作,但在Windows系统上似乎不工作!在Windows上,生成的.pdf文件不包含图像。排除故障后,我想我已经解决了问题,但仍然找不到解决方案
基本上,在Windows上,tempdir()命令似乎会创建带有双反斜杠的路径,例如\\this\\is\\myPath。然后,在.tex文件中,临时目录(包含图像)的路径是单反斜杠,例如\this\is\myPath。但是,这些应该是单正斜杠,例如/this/is/myPath,以便.tex文件查找存储在临时目录中的图像
事实上,如果我在Windows中手动将.tex文件中的反斜杠更改为正斜杠,那么我可以成功地将其转换为成功包含图像的.pdf文件
然而,我不确定如何用我的语法解决这个问题。我认为这将是一个简单的解决方案,但如果我简单地执行以下操作:
# Specify directory for figure output in a temporary directory
temppath <- tempdir()
gsub("\\\\", "/", temppath)
这样.tex文件也可以自动擦除。我试图确认这样的语法在CRAN标准中是可以接受的,因为它确实涉及到从用户的计算机上删除一个文件(这可能看起来像危险/恶意软件),尽管它特别指向它刚刚生成的.tex文件,因此它不应该删除对他们来说重要的任何内容
*注意:默认情况下,我会删除所有中间文件,因此用户只处理.pdf文件。但是,我仍然允许用户选择违反此默认设置,并保留这些中间文件(如果需要)。您提供的代码对特定于Swave的表达式给出了一个尖锐的警告:
\SwaveOpts{concordance=TRUE}
如果我删除此选项,页面将为我编译(有许多框错误)并在Windows(和Ubuntu)中生成带有图形的PDF
我也不清楚为什么您首先要指定自己的临时目录: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
normalizePath(tempdir(),winslash='/')是否有帮助?在r会话结束后,tempdir
中存储的任何内容都将被删除,因此不需要明确删除任何内容。但是您可以?取消链接上的文件。退出函数的
,cran可能更喜欢运行系统命令。我只是想说,这个版本的问题在问题质量和清晰度方面比第2部分有了很大改进!
knit2pdf("/inst/Rnw/test.Rnw","/path/test.tex",clean=T)
system(sprintf("%s", paste0("rm -r ", "/path/myFile.tex")))
> 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