将图形直接放入Knitr文档(不将其文件保存在文件夹中)

将图形直接放入Knitr文档(不将其文件保存在文件夹中),r,pdf,rstudio,knitr,R,Pdf,Rstudio,Knitr,我正在RStudio中创建一个名为test.Rnw的文档,其MWE如下所示: \documentclass[12pt,english,nohyper]{tufte-handout} \usepackage{tabularx} \usepackage{longtable} \begin{document} <<setup, echo = FALSE>>= library(knitr) library(xtable) library(ggplot2) @ <<

我正在RStudio中创建一个名为test.Rnw的文档,其MWE如下所示:

\documentclass[12pt,english,nohyper]{tufte-handout}
\usepackage{tabularx}
\usepackage{longtable}

\begin{document}

<<setup, echo = FALSE>>=
library(knitr)
library(xtable)
library(ggplot2)
@

<<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 normal 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}
\documentclass[12pt,英语,nohyper]{tufte讲义}
\usepackage{tablarx}
\usepackage{longtable}
\开始{document}
=
图书馆(knitr)
图书馆(xtable)
图书馆(GG2)
@
=
打印(qplot(深度、价格、数据=钻石))
@
=

myDF更改标题文本的颜色可以在LaTeX中完成,方法是将
\setcaptionfont
环境中的
\color
设置为黑色(代码第8行)

以你的例子:

\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}
\documentclass[nohyper]{tufte讲义}
\usepackage{tablarx}
\usepackage{longtable}
\setcaptionfont{%更改标题字体特征
\正常字体\脚注大小

\颜色{黑色}%你为什么不创建一个R标记文件,直接编织成pdf?你使用的是哪种平台:mac、pc、linux等?@GwenaëlGouérou:谢谢你的评论。我对这些差异非常不熟悉,也没有意识到如果我将脚本从.Rnw转换成.Rmd,图像可以直接编织成pdf文件。我已经阅读了at.Rmd不如.Rnw灵活,并且会担心其他一些功能可能会丢失?我正在接管其他人的项目,而那个人是在.Rnw中创建脚本的,所以我想知道这是否是有原因的(好像无法使用.Rmd完成)。我还想知道从.Rnw转换为.Rmd来测试它有多困难?@JimM。谢谢。我正在使用Mac。不过,我正在将其包装到一个R包中(该包生成带有摘要统计信息的输出.pdf文件)。因此,它可以被不同平台的人使用。无论你如何删除文件,这都必须在PDF文件编译后发生。这就是为什么Jim M.的答案不起作用。你是否考虑过在临时目录中编译,然后复制PDF?你也可以试试。谢谢你的输入,但我担心这可能是一个错误n不安全的解决方案:(.我不小心将“fig_目录”设置到我的桌面路径,它删除了我桌面上的所有内容(它们也不在“垃圾桶”中,它们似乎完全消失了?)。我正试图将其放入一个R软件包中,因此我不认为我能够拥有rm-R功能,因为从用户的计算机上删除文件几乎就像恶意软件一样。这对我来说似乎也是一个很好的解决方案,但不幸的是,我可能必须找到另一个解决方案…我想知道为什么这个答案会受到赞誉-espe特别是如果这意味着我做错了什么?因为即使我在删除桌面上的所有内容后尝试运行此操作(这样它不会删除我想要保留的文件,并且只删除figure文件夹中的文件),我仍然会遇到一个错误“/Desktop/figure/diamondData-1.png”找不到。”我认为它删除图形太快了。如果没有这些图形文件,当我在.tex文件上运行pdflatex时,.pdf文件中没有图形,因为它们在编织后立即被删除。或者我做错了什么吗?再次感谢。这不会起作用,因为在编译pdf之前,图形目录已被清除。您应该可以ll
system()
Rnw
文件之外,该文件存在将
fig_目录
传递到
knitr
的问题。此外,我认为
fig.path
的全局块选项将是合适的。@Josephudson:好的观点。我已经根据您和user2706569的建议对代码进行了更改。你们好欧,谢谢你的帮助。看起来这是一个比以前更好的解决方案。所以,它基本上创建了一个临时文件夹,并将数字存储在其中?这个临时文件夹多长时间被删除一次?