微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将报表函数的输出从报表包渲染到 knitr

如何解决将报表函数的输出从报表包渲染到 knitr

我想将 report 函数输出report R 包渲染到 .Rnw(使用 knitr)以输出 pdf format。但是,它没有按预期工作。

library(report)
model <- lm(Sepal.Length ~ Species,data = iris)
report(model)
We fitted a linear model (estimated using OLS) to predict Sepal.Length with Species (formula: Sepal.Length ~ Species). The model explains a statistically significant and substantial proportion of variance (R2 = 0.62,F(2,147) = 119.26,p < .001,adj. R2 = 0.61). The model's intercept,corresponding to Species = setosa,is at 5.01 (95% CI [4.86,5.15],t(147) = 68.76,p < .001). Within this model:

  - The effect of Species [versicolor] is statistically significant and positive (beta = 0.93,95% CI [0.73,1.13],t(147) = 9.03,p < .001; Std. beta = 1.12,95% CI [0.88,1.37])
  - The effect of Species [virginica] is statistically significant and positive (beta = 1.58,95% CI [1.38,1.79],t(147) = 15.37,p < .001; Std. beta = 1.91,95% CI [1.66,2.16])

Standardized parameters were obtained by fitting the model on a standardized version of the dataset.

report(model)results = 'asis' 一起使用无法正常工作,因为输出包含特殊字符 %。使用 results = 'markup' 不考虑页面宽度。

我尝试了 kable(report(model)),但抛出以下错误

Error in `[<-.data.frame`(`*tmP*`,which,value = "") : 
  unsupported matrix index in replacement

包裹信息

packageVersion("report")
[1] ‘0.3.0’

已编辑

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\begin{document}

<<include=FALSE>>=
knitr::opts_chunk$set(comment = NA,width = 80)
@

<<chunk1>>=
library(report)
model <- lm(Sepal.Length ~ Species,data = iris)
report(model)
@

\end{document}

已编辑 (2021-06-22)

它是一个 bug

解决方法

htmltools::tagList() 可能正是您要找的:

```{r}
library(report)
model <- lm(Sepal.Length ~ Species,data = iris)
htmltools::tagList(report(model))
```

enter image description here

然而,results='asis' 似乎更好地尊重换行符:

```{r,results='asis'}
library(report)
model <- lm(Sepal.Length ~ Species,data = iris)
report(model)
```

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。