【问题标题】:xtable in .Rmd then knit as pdf in rstudio shows % comments.Rmd 中的 xtable 然后在 rstudio 中编织为 pdf 显示 % 评论
【发布时间】:2014-09-29 18:21:14
【问题描述】:

在编写 R Markdown (.Rmd) 文件时,我正在使用 Rstudio 的“knit PDF”选项制作 PDF。

当使用 xtable 函数创建表格时,使用 % 在 Latex 中注释的文本会显示在 pdf 中。使用 Latex 和 R 编织 .Rnw 文件时,这个问题就消失了。

下面是一个将 .Rmd 文件编织为 PDF 的示例,以及将等效的 .Rnw 文件编织为(自然为 pdf)的示例。

他们的 PDF 结果是相同的,除了一行。就在表格上方,显示以下内容:

xtable 1.7-3 包在 R 3.1.0 中生成的乳胶表百分比 % Wed Aug 06 19:06:37 2014

MarkdownFile.Rmd

---
output: pdf_document
---

```{r, results='asis'}
library(xtable)
xtable(summary(cars)) 
```

SweaveFile.Rnw

\documentclass{article}

\begin{document}

<<r, results='asis'>>=
library(xtable)
xtable(summary(cars))
@

\end{document}

r 中xtable(summary(cars)) 表达式的实际输出如下。可以看到前两行,以%开头,不同的是.Rnw文件隐藏了它们,.Rmd文件没有。

% latex table generated in R 3.1.0 by xtable 1.7-3 package
% Wed Aug 06 19:33:18 2014
\begin{table}[ht]
\centering
\begin{tabular}{rll}
\hline
 &     speed &      dist \\ 
  \hline
1 & Min.   : 4.0   & Min.   :  2.00   \\ 
  2 & 1st Qu.:12.0   & 1st Qu.: 26.00   \\ 
  3 & Median :15.0   & Median : 36.00   \\ 
  4 & Mean   :15.4   & Mean   : 42.98   \\ 
  5 & 3rd Qu.:19.0   & 3rd Qu.: 56.00   \\ 
  6 & Max.   :25.0   & Max.   :120.00   \\ 
   \hline
\end{tabular}
\end{table}

我假设问题在于编织的 .Rmd 文件无法将 % 识别为乳胶注释,因此将其打印出来。 我怎样才能摆脱桌子上方的这些线条? .Rmd 文件有没有办法将% 识别为评论?

【问题讨论】:

  • 使用print(xtable(summary(cars)), comment=F)
  • 效果很好。谢谢!

标签: r pdf knitr rstudio rnw


【解决方案1】:

正如@celiomsj 所说,将comment 参数用于print.xtable 并将其设置为FALSE 以省略参数:

print(xtable(summary(cars)), comment=FALSE)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-04
    • 2014-08-10
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    相关资源
    最近更新 更多