我试图弄清楚如何创建一个循环,将一些文本插入到rmarkdown文件中,然后生成与该标题对应的图形或表格.以下是我的工作原理:
for(i in 1:max(month)){
### `r month.name[i]` Air quaility
```{r, echo=FALSE}
plot(airquality[airquality$Month == 5,])
```
}
Run Code Online (Sandbox Code Playgroud)
这个当然只是打印for循环作为文本,如果我用r``环绕for循环,我只会得到一个错误.
我希望代码生成一个如下所示的rmd文件:
情节
情节
等等等等.有任何想法吗?我不能使用乳胶因为我在工作时他们不让我们下载exe文件,而且我不知道如何使用乳胶.我想制作一个word文档.
我需要生成一个由几个部分组成的报告,所有部分看起来都很相似,只有一些数据差异.部分的数量也取决于数据.我最终想拥有的是这样的:
```{r}
section_names = c("A","B","C")
section_data = c(13,14,16)
```
# some looping mechanism here with variable i
This is section `r section_names[i]`
This section's data is `r section_data[i]`
#more things go here for the section
#end of loop should go here
Run Code Online (Sandbox Code Playgroud)
结果应该是一个单独的html /文档,所有部分一个接一个.
你能指点我用循环生成这样一个Rmd文件的方法吗?
理想情况下,我希望看到像PHP中的东西:
<$php for(i=0;i<10;i++) { ?>
## some html template + code chunks here
<$php } ?>
Run Code Online (Sandbox Code Playgroud) 'asis' 块对于在 Markdown 文档中输出对象列表非常有用,请参阅以下示例:Highcharter、DT、Leaflet,...
但是,在上面的示例中,如果渲染器在前一个块中没有被调用一次,则对象列表将不会打印,以便它被初始化:这是一个棘手的解决方法,我通过试验找到了更多解决方案/错误比通过在文档中查找它更容易。
这是一个可重现的问题,也发布在https://github.com/rstudio/rmarkdown/issues/1877上:
---
title: "Test"
output:
html_document
---
```{r,echo=F}
library(DT)
library(rmarkdown)
library(purrr)
library(knitr)
df_list <- list("cars" = mtcars, "flowers" = iris)
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
```
```{r}
# If this first initialization isn't run, tabs won't print
DT::datatable(data.frame())
```
# Test tabs {.tabset}
```{r, results='asis' }
imap(df_list, ~{
cat('## Subtab ',.y,'\n')
cat('\n')
DT::datatable(.x) %>%
htmltools::tagList() %>% as.character() %>% cat() })
```
Run Code Online (Sandbox Code Playgroud) 我目前正在努力让 knitr 渲染我的传单地图,从集合中获取以正确显示在渲染的 RMD html 输出中。我已经意识到循环集合并使用 RMD/knitr 生成图形输出时的一些潜在问题,但我仍然无法弄清楚如何使我的示例适用于传单地图。
可重现的工作示例(Test_1.Rmd):
---
title: "test1"
author: "phabee"
date: "22 Mai 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Title 1
```{r, fig.show='asis', echo=FALSE, results='asis'}
for (i in 1:4) {
cat("### Plot Number ", i, "\n")
plot(1,1)
# use plot.new() here to force rendering of potential plot-duplicates
plot.new()
cat("\n\n")
}
```
Run Code Online (Sandbox Code Playgroud)
上面的例子按预期渲染(至少在添加plot.new()之后,这是我从Freedomtowin学到的。但是当我尝试对传单地图做同样的事情时,它根本不起作用。没有渲染任何单个地图:
可重现的失败示例 (Test_2.Rmd)
---
title: "test2"
author: "phabee"
date: "22 Mai 2018"
output: html_document
--- …Run Code Online (Sandbox Code Playgroud) 我的一位同事的文件夹/目录中有很多sql陈述。该文件夹也由他每天更新。我想sql为期货同事记录这些声明。但是,我正在寻找一种使该过程“自动化”的方法。我想crontab每周使用一次并运行一个R-Markdown文件,该文件会自动更新现有R-Markdown文件。
我的方法如下:
path = "c:/SQL_files/"
out.file<-""
file.names <- dir(path, pattern =".sql") # here I changed `.txt` to `.sql`
for(i in 1:length(file.names)){
file <- read.csv2.sql(file.names[i],header=TRUE, sep=";", stringsAsFactors=FALSE)
out.file <- rbind(out.file, file)
}
# That second approach comes very close, but just generates a `.txt` for the first
#`.sql` file in the directory with the error:
Error in match.names(clabs, names(xi)) :
names do not match previous names
Run Code Online (Sandbox Code Playgroud)
文件在哪里:
[1] "c:/SQL_files/first.sql"
[2] "c:/SQL_files/second.sql" …Run Code Online (Sandbox Code Playgroud) 我正在尝试动态创建和循环,htmlwidgets例如DT、plotly、 或rbokeh生成自动针织报告。有没有办法将knitr格式(例如 )添加到此 github 问题https://github.com/ramnathv/htmlwidgets/pull/110tabset中概述的方法中?我也在那里发布了这个问题。tagList
下面是我的想法的一些示例代码,但它不太有效。我想做的是创建 10 个选项卡,每个选项卡都有一个从plot_list. 现在发生的事情是所有绘图都进入最后一个选项卡。在实践中,plot_list会有不同的图/表。
#' ---
#' title: htmltools::tagList test
#' output:
#' html_document
#' ---
#' # {.tabset}
#+ results='asis', echo=FALSE
library(plotly)
library(printr)
plot_list = lapply(1:10,
function(i){
as.widget(plot_ly(iris,
x = iris[["Sepal.Length"]],
y = iris[["Sepal.Width"]],
mode = "markers"))
}
)
htmltools::tagList( lapply(1:10,
function(i) {
pandoc.header(paste0("Tab",i,' {.tabset}'), 2)
plot_list[[i]]
}
)
)
# rmarkdown::render("your_path/htmltoolsTagList_test.r")
Run Code Online (Sandbox Code Playgroud)
之前,我使用嵌套 for 循环成功地完成了类似的操作,但是一旦我尝试使用具有 HTML …