R Markdown,循环输出测试结果 [英] R Markdown, output test results in loop
问题描述
我正在寻找在 for 循环中生成并带有标题的测试结果的格式良好的降价输出.例如
df <- data.frame(x = rnorm(1000),y = 范数(1000),z = 范数(1000))for (v in c("y","z")) {cat("##", v, " (模型 0)\n")总结(lm(x~1,df))cat("##", v, " (模型 1)\n")总结(lm(as.formula(paste0(x~1+",v)),df))}
而输出应该是
y(模型 0)
调用:lm(公式 = x ~ 1, 数据 = df)残差:最小 1Q 中值 3Q 最大值-3.8663 -0.6969 -0.0465 0.6998 3.1648系数:估计标准误差 t 值 Pr(>|t|)(拦截) -0.05267 0.03293 -1.6 0.11剩余标准误差:999 自由度上的 1.041
y(模型 1)
调用:lm(formula = as.formula(paste0("x~1+", v)), data = df)残差:最小 1Q 中值 3Q 最大值-3.8686 -0.6915 -0.0447 0.6921 3.1504系数:估计标准误差 t 值 Pr(>|t|)(拦截) -0.05374 0.03297 -1.630 0.103y -0.02399 0.03189 -0.752 0.452剩余标准误差:998 自由度上的 1.042多个 R 平方:0.0005668,调整后的 R 平方:-0.0004346F 统计量:1 和 998 DF 上的 0.566,p 值:0.452
z(模型 0)
等等...
有几个结果讨论了部分问题,例如
解决方案类型 2
当我们想要在 rmarkdown 本身中自动输出多个模型摘要时,我们可以在 1. 选择块选项 results='asis'
之间进行选择,这将产生代码输出,但例如# Model 1
标题,或 2. 选择不选择它,这会产生 Model 1 但破坏代码格式.解决方案是使用该选项并将其与我们可以paste()
与另一个sapply()
循环内的内联代码结合起来sapply()
用于模型.
在主要的sapply
中,我们应用了@G.Grothendieck's 古老的
I'm looking for a nicely formated markdown output of test results that are produced within a for loop and structured with headings. For example
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))
}
whereas the output should be
y (model 0)
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
y (model 1)
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
z (model 0)
and so on...
There are several results discussing parts of the question like here or here suggesting the asis
-tag in combination with the cat
-statement. This one includes headers.
Closest to me request seems to be this question from two years ago. However, even though highly appreciated, some of suggestions are deprecated like the asis_output
or I can't get them to work in general conditions like the formattable
suggestion (e.g. withlm
-output). I just wonder -- as two years have past since then -- if there is a modern approach that facilitates what I'm looking for.
Solution Type 1
You could do a capture.output(cat(.))
approach with some lapply
-looping. Send the output to a file and use rmarkdown::render(.)
.
This is the R code producing a *.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")
Produces:
Solution Type 2
When we want to automate the output of multiple model summaries in the rmarkdown itself, we could chose between 1. selecting chunk option results='asis'
which would produce code output but e.g. # Model 1
headlines, or 2. to choose not to select it, which would produce Model 1 but destroys the code formatting. The solution is to use the option and combine it with inline code that we can paste()
together with another sapply()
-loop within the sapply()
for the models.
In the main sapply
we apply @G.Grothendieck's venerable solution to nicely substitute the Call:
line of the output using do.call("lm", list(.))
. We need to wrap an invisible(.)
around it to avoid the unnecessary sapply()
output [[1]] [[2]]...
of the empty lists produced.
I included a ". "
into the cat()
, because leading white space like ` this`
will be rendered to this
in lines 6 and 10 of the summary outputs.
This is the rmarkdown script producing a *pdf
that can also be executed ordinary line by line:
---
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.
Produces:
(Note: Site 3 omitted)
这篇关于R Markdown,循环输出测试结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!