【问题标题】:Display a graphic in report with sweave : Just display the final report and no intermediate file使用 sweave 在报告中显示图形:只显示最终报告,没有中间文件
【发布时间】:2019-09-26 15:16:51
【问题描述】:

我很了解 R,但我从 Sweave 开始。

我在 Rstudio 下从几个脚本生成报告:

  • graphic.R,它定义了一个用 ggplot2 创建的名为 graph_1 的图形。我想在报告中显示它。
  • script.Rnw 包含 Latex 和 Sweave 代码。我调用 graph_1 来显示我的图形。
  • ma​​in.R 脚本,用于执行 .Rnw 代码并因此生成报告。

在graphic.R中

library(ggplot2)
graph_1 <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point(aes(color=Species, shape=Species))

在 script.Rnw 中

\documentclass[french,12pt]{article}
\begin{document}

 ... no important...

\begin{figure}[h]
  \begin{center}
   << fig=TRUE, echo=FALSE, height = 2.5>>=
    graph_1
    @
 \caption{caption figure 1}
 \label{graph_1}
  \end{center}
\end{figure}
\end{document}

在 main.R 中

Sweave("script.Rnw", encoding="UTF-8")
tools:::texi2dvi(file="script.tex"), pdf=TRUE)

一切正常:我的报告 (.pdf) 在文件夹中生成。但我也有一个文件:“script-001.pdf”,其中包含我添加到同一文件夹中的图形。

我只想要最终报告而不想要中间文件:所以不要文件'script-001.pdf'。 有人知道这是否可能吗?

谢谢,

【问题讨论】:

    标签: r latex sweave


    【解决方案1】:

    图形文件是必需的,以便 LaTeX 可以包含它。但是你可以在运行 Sweave 和 LaTeX 之后安全地删除它。

    我也不喜欢我的工作目录中到处都是中间 PDF。所以我把它们放在一个名为figures 的文件夹中。使用 Sweave,您可以通过设置合适的前缀来完成此操作:

    \SweaveOpts{prefix.string=figures/fig}
    

    您还需要自己创建figure 文件夹才能使其正常工作。

    还有一个个人建议:您可能想看看knitr——在我看来,它是更强大的现代版 Sweave。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-27
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多