【问题标题】:Rnw file doesn't generate plot in pdfRnw 文件不会在 pdf 中生成绘图
【发布时间】:2016-06-03 16:58:26
【问题描述】:

我正在处理一个.Rnw 文件,试图生成一个图。但是,当我单击编译 PDF 时,我得到一个只有图形标题而没有绘图的空白 pdf。它看起来像这样:

代码如下:

\documentclass{article}

\begin{document}

<<fasfd, fig.height=10, fig.cap="first", fig.pos="t", echo=FALSE, fig.width=10>>=
barplot(table(mtcars$gear))
@

\end{document}

我做错了什么?

【问题讨论】:

  • 您使用knitrsweave 标记您的问题。你用的是哪一个?
  • FWIW,将您的示例保存到名为 "eg.Rnw" 的文件中,然后执行 knitr::knit2pdf("eg.Rnw") 会得到一个显示该图的文档。
  • 我正在使用knitr,很抱歉造成混淆。
  • @JoshO'Brien 好吧,这行得通。谢谢!知道为什么当我单击编译 PDF 时它不起作用吗?
  • 不,不能帮你,因为这是 RStudio 功能的问题,我不倾向于使用 RStudio。

标签: r pdf knitr


【解决方案1】:

Sweave 没有提供像knitr 那么多的选项。要包含带有sweave 的图,您需要指定fig= TRUE

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}
Without $fig=TRUE$
<<fasfd, fig.height=5, fig.cap="first", fig.pos="t", echo=TRUE, fig.width=10>>=
barplot(table(mtcars$gear))
@
With $fig=TRUE$
<<fig=TRUE>>=
barplot(table(mtcars$gear))
@

A boxplot of the \emph{airquality} data:
\centering
<<fig=TRUE, echo=FALSE>>=
data(airquality)
boxplot(Ozone ~ Month, data = airquality)
@

\end{document}

【讨论】:

    【解决方案2】:

    我从来没有用过knitr,但是语法中继在sweave上提醒了我,这种情况下你需要更正标签,例如:

    <<label=fig1,fig=TRUE,echo=FALSE>>=
    

    总共:

    \documentclass{article}
    
    \begin{document}
    \SweaveOpts{concordance=TRUE}
    
    <<label=fig1,fig=TRUE,echo=FALSE>>=
    
    barplot(table(mtcars$gear))
    @
    \end{document}
    

    【讨论】:

    • OP 的标签语法适用于knitr
    猜你喜欢
    • 1970-01-01
    • 2014-06-18
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 2016-10-26
    • 2012-12-01
    • 2016-07-01
    相关资源
    最近更新 更多