我正在尝试使用knitr基于具有for循环的R脚本生成HTML报告.我想从for循环中的注释生成markdown注释,但我不确定它是否可行.
这是一个简单的例子,这是在test.R中:
for (i in 1:5) {
## This is a heading for `i`
#' This is a comment for `i`
print(i)
}
Run Code Online (Sandbox Code Playgroud)
然后我用spin来生成一个Rmd文件:spin('test.R')
但是,Rmd文件如下所示.
```{r }
for (i in 1:5) {
## This is a heading for `i`
#' This is a comment for `i`
print(i)
}
```
Run Code Online (Sandbox Code Playgroud)
R块中的markdown注释不会编译为HTML.可能吗?
谢谢,彼得
我正在尝试生成 R Markdown 文档,其中包含从循环动态生成的标题。
我使用了类似SO问题中的建议:Knit: print text from code block as R markdown,Using bold text in a Loop for a Word document in Rmarkdown并写道
---
title: "Untitled"
output:
html_document:
toc: true
toc_depth: 5
---
## R Markdown
```{r, warning=FALSE, message=FALSE, echo=FALSE}
library(ggplot2)
library(dplyr)
```
```{r fig.width=4, fig.height=2, echo=FALSE, results='asis'}
experiment_names <- paste0("Experiment ", 1:3)
for (id in 1:3){
cat(" \n")
cat("### ", experiment_names[id])
cat(" \n")
set.seed(id)
plt <-
data.frame(x = rnorm(100)) %>%
ggplot(aes(x = x)) + …
Run Code Online (Sandbox Code Playgroud) 我找到了几个例子(如何使用 R 提取 SQL 文件的内容, 使用循环在 rmarkdown 中生成文本部分,使用 R在 Rmarkdown中 自动生成预格式化文本),但在我的情况下没有一个适用于嵌套部分,包括数字. 预期输出:
看来,我需要某种方法来处理包含的代码块(例如cat("```{r, fig.width=8.4, fig.height=7}\n") ... cat("```\n")
正确处理,但我找不到方法。
编辑:我在这里找到了这个,但仍然无法使它工作
kexpand <- function(chunkname, fig.width, fig.height) {
cat(knitr::knit(text = knitr::knit_expand(text =
sprintf("```{r %s, fig.width=%s, fig.height=%s}\n.chunk_code\n",
chunkname, fig.width, fig.height))))
}
Run Code Online (Sandbox Code Playgroud)
然后在块中:
.chunk_code <- leaflet() %>%
addTiles() %>%
addProviderTiles(providers$OpenStreetMap, group = "OSM") %>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>%
addLayersControl(baseGroups = c("OSM", "Toner Lite")) %>%
addCircleMarkers(lat = 47, lng = 9, popup = ranges[i]) …
Run Code Online (Sandbox Code Playgroud) 请参见以下可重现的示例:
---
title: "test"
output: html_document
---
## foo
```{r}
plot(1:3)
```
## bar
```{r}
plot(4:7)
```
## baz
```{r}
plot(8:12)
```
Run Code Online (Sandbox Code Playgroud)
我希望能够自动创建这些部分,因为在进一步分析之前我不知道它们的数量。
我的输入是:
my_list <- list(foo = 1:3, bar = 4:7, baz = 8:12)
my_fun <- plot
my_depth <- 2
Run Code Online (Sandbox Code Playgroud)
理想的答案(尽管我欢迎任何改进)将帮助我构建一个mdapply
函数,以便可以运行:
```{r}
mdapply(X = my_list, FUN = my_fun, title_depth = my_depth)
```
Run Code Online (Sandbox Code Playgroud)
并获得相同的输出。
我正在寻找格式良好的测试结果的降价输出,该输出在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+", …
Run Code Online (Sandbox Code Playgroud)