我正在寻找格式良好的测试结果的降价输出,该输出在for循环内生成并由标题构成。例如
df <- data.frame(x = rnorm(1000),
y = rnorm(1000),
z = rnorm(1000))
for (v in c("y","z")) {
cat("##", v, " (model 0)\n")
summary(lm(x~1, df))
cat("##", v, " (model 1)\n")
summary(lm(as.formula(paste0("x~1+",v)), df))
}
Run Code Online (Sandbox Code Playgroud)
而输出应该是
Call:
lm(formula = x ~ 1, data = df)
Residuals:
Min 1Q Median 3Q Max
-3.8663 -0.6969 -0.0465 0.6998 3.1648
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.05267 0.03293 -1.6 0.11
Residual standard error: 1.041 on 999 degrees of freedom
Run Code Online (Sandbox Code Playgroud)
Call:
lm(formula = as.formula(paste0("x~1+", v)), data = df)
Residuals:
Min 1Q Median 3Q Max
-3.8686 -0.6915 -0.0447 0.6921 3.1504
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.05374 0.03297 -1.630 0.103
y -0.02399 0.03189 -0.752 0.452
Residual standard error: 1.042 on 998 degrees of freedom
Multiple R-squared: 0.0005668, Adjusted R-squared: -0.0004346
F-statistic: 0.566 on 1 and 998 DF, p-value: 0.452
Run Code Online (Sandbox Code Playgroud)
等等...
有几个结果讨论了部分问题,例如此处或此处,建议将asis
-tag与cat
-statement 结合使用。这包括标题。
离我最近的要求似乎是两年前的这个问题。但是,尽管受到高度赞赏,但还是不赞成使用某些建议,asis_output
否则我无法在formattable
建议的一般条件下使用它们(例如,使用lm
-output)。我只是想知道-从那时起已经过去两年了-是否有一种现代化的方法可以促进我寻找的东西。
您可以capture.output(cat(.))
使用一些lapply
循环方法。将输出发送到文件并使用rmarkdown::render(.)
。
这是产生一个的R代码*.pdf
。
capture.output(cat("---
title: 'Test Results'
author: 'Tom & co.'
date: '11 10 2019'
output: pdf_document
---\n\n```{r setup, include=FALSE}\n
knitr::opts_chunk$set(echo = TRUE)\n
mtcars <- data.frame(mtcars)\n```\n"), file="_RMD/Tom.Rmd") # here of course your own data
lapply(seq(mtcars), function(i)
capture.output(cat("# Model", i, "\n\n```{r chunk", i, ", comment='', echo=FALSE}\n\
print(summary(lm(mpg ~ ", names(mtcars)[i] ,", mtcars)))\n```\n"),
file="_RMD/Tom.Rmd", append=TRUE))
rmarkdown::render("_RMD/Tom.Rmd")
Run Code Online (Sandbox Code Playgroud)
当我们想在rmarkdown本身中自动化多个模型摘要的输出时,我们可以选择1.选择results='asis'
将产生代码输出但例如# Model 1
标题的块选项,或2.选择不选择它将产生模型1但选择它。破坏代码格式。解决方案是使用该选项,并将其与内联代码组合在一起,我们可以将其与模型中的paste()
另一个sapply()
-loop 一起使用sapply()
。
在主sapply
,我们申请@ G.Grothendieck的古老的解决方案很好地替代Call:
使用的输出线do.call("lm", list(.))
。我们需要将其包装invisible(.)
起来,以避免产生不必要的空列表sapply()
输出[[1]] [[2]]...
。
我在中". "
加入了cat()
,因为前导空格` this`
将this
在摘要输出的第6行和第10行中呈现。
这是产生一个的rmarkdown脚本*pdf
,该脚本也可以逐行执行:
---
title: "Test results"
author: "Tom & co."
date: "15 10 2019"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Overview
This is an example of an ordinary code block with output that had to be included.
```{r mtcars, fig.width=3, fig.height=3}
head(mtcars)
```
# Test results in detail
The test results follow fully automated in detail.
```{r mtcars2, echo=FALSE, message=FALSE, results="asis"}
invisible(sapply(tail(seq(mtcars), -2), function(i) {
fo <- reformulate(names(mtcars)[i], response="mpg")
s <- summary(do.call("lm", list(fo, quote(mtcars))))
cat("\n## Model", i - 2, "\n")
sapply(1:19, function(j)
cat(paste0("`", ". ", capture.output(s)[j]), "` \n"))
cat(" \n")
}))
```
***Note:*** This is a concluding remark to show that we still can do other stuff afterwards.
Run Code Online (Sandbox Code Playgroud)
(注意:网站3省略了)