Hmisc乳胶功能需要去掉第一行

时间:2015-07-16 00:20:51

标签: r latex hmisc

我在rmarkdown文件中使用Hmisc。当我创建一个表时,这就是我的工作

---
output: pdf_document
---

```{r Arrests Stats, results ='asis', message = FALSE, warning = FALSE, echo = FALSE}

# render the table

options(digits=1)
library(Hmisc)
latex(head(mtcars), file="")

```

乳胶输出的第一行显示如下

%latex.default(cstats, title= title....
\begin{table}...
.
.
.
\end{tabular}

注意'%'我需要弄清楚它在编织时显示在PDF文档上的第一行

1 个答案:

答案 0 :(得分:3)

看起来它被硬编码到latex.defaultcat("%", deparse(sys.call()), "%\n", file = file, append = file != "", sep = "")在身体中,没有条件限制它。)

我认为您最好的猜测是capture.output cat - d输出并自行删除评论。

cat(capture.output(latex(head(mtcars), file=''))[-1], sep='\n')

capture.output抓住latex(...) cat的所有内容,[-1]删除第一行(即'%latex.default'),{cat 1}}使用换行符分隔符打印出其他所有内容。

您可以定义自己的mylatex来做到这一点,并且更聪明一些(例如,不是盲目地剥离输出的第一行,如果以'%'开头,则只能剥离它)。

mylatex <- function (...) {
    o <- capture.output(latex(...))
    # this will strip /all/ line-only comments; or if you're only
    #  interested in stripping the first such comment you could
    #  adjust accordingly
    o <- grep('^%', o, inv=T, value=T)
    cat(o, sep='\n')
}
mylatex(head(mtcars), file='')