我根据与rmarkdown.
运行的类似数据有几个报告每个报告的最后一张幻灯片是一个“数据来源”幻灯片,它只提供有关数据的详细信息,提取时间,报告#,哈希现在,每个报告都有单独的最终幻灯片,我必须单独维护,因此,如果我必须进行更改,我必须更改每个报告中的幻灯片。这很麻烦。
我希望能够有一个函数返回幻灯片文本和R
代码,这些代码在运行时被评估并呈现为markdown和R
代码,但我遇到了问题得到那个设置。这就是我到目前为止所做的:
library(git2r)
library(dplyr)
base <- data.frame(Date = Sys.Date(),
DateTime = Sys.time()) # Simple example data
params <- list(version = "0.0.5")
tmp_file <- tempfile(fileext = ".rds") # Write so that I can hash
base %>% saveRDS(tmp_file)
data_provenance <- function(){
print("* Data as of: `r base %>% arrange(desc(Date)) %>% select(DateTime) %>% slice(1) %>% .$DateTime`
* Dataset: `r hash(tmp_file)`
* Report version: `r params$version`")
}
当我在此
上运行rmarkdown
时
## Data, test
Report details
`r data_provenance()`
我得到的是带有标题和“报告详细信息”行的幻灯片,但没有来自data provenance()
函数的输出。但是,data_provenance()
内容会打印到控制台。
我在本报告前面的条件函数中使用了print()
语句:
## Overview
```{r neg_ideas_chunk, echo = FALSE}
neg_ideas <- nrow(base) > 0
```
`r if(neg_ideas)(print("* Negative ideas removed:"))`
并且r if(...)(...)
无误地填充幻灯片。
我是否可以使用单独的函数将.md
和R
代码组合在一起,并让它在.Rmd
文件中成功运行并呈现?我在这里缺少什么?
答案 0 :(得分:0)
这个示例演示如何:
---
title: "example"
author: "Luke C"
date: "December 11, 2017"
output: ioslides_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Data, test
Report details
```{r}
data_provenance()
```
然后您可以使用R脚本的修改版本运行(假设.Rmd和R文件位于同一目录中,也是工作目录):
library(dplyr)
library(git2r)
base <- data.frame(Date = Sys.Date(),
DateTime = Sys.time()) # Simple example data
params <- list(version = "0.0.5")
tmp_file <- tempfile(fileext = ".rds") # Write so that I can hash
base %>% saveRDS(tmp_file)
data_provenance <- function(){
x <- sapply(list(
paste("* Data as of:",
base %>% arrange(desc(Date)) %>% select(DateTime) %>% slice(1) %>% .$DateTime,
"\n"),
paste("* Dataset:", hash(tmp_file), "\n"),
paste("* Report version:", params$version)),
paste, collapse = "")
cat(x, sep = "")
}
data_provenance()
rmarkdown::render("example_presentation.Rmd")
输出如下:
但是,如果您没有从R脚本渲染降价文件,您有什么理由不能在降价文件中定义该功能吗?
编辑2
好的,所以这有点笨拙:
data_provenance
中所需的行。标志因此,使用此演示文稿设置时,Data, test
幻灯片空白除了&#34;报告详细信息&#34;:
---
title: "example"
author: "Luke C"
date: "December 11, 2017"
output: ioslides_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Data, test
Report details
与以前相同的基本设置,修改后的data-provenance
:
library(dplyr)
library(git2r)
base <- data.frame(Date = Sys.Date(),
DateTime = Sys.time()) # Simple example data
params <- list(version = "0.0.5")
tmp_file <- tempfile(fileext = ".rds") # Write so that I can hash
base %>% saveRDS(tmp_file)
data_provenance <- function(){
paste(
paste("* Data as of:",
base %>% arrange(desc(Date)) %>% select(DateTime) %>% slice(1) %>% .$DateTime,
"\n"),
paste("* Dataset:", hash(tmp_file), "\n"),
paste("* Report version:", params$version))
}
现在,这个函数接收文件名和输出所需数据详细信息的函数,结构如上data_provenance
:
render_dynamic_prov <- function(filename, det_fun){
f.out.name = paste(gsub(".Rmd", "", filename), ".html", sep = "")
file(description = filename, open = "r")
file(description = "outfile_temp.Rmd", open = "w")
for (i in readLines(filename)) {
if (i == "Report details") {
write(x = paste(i, "\n"), file = "outfile_temp.Rmd", append = TRUE)
write(x = det_fun, file = "outfile_temp.Rmd", append = TRUE)
} else {
write(x = i, file = "outfile_temp.Rmd", append = TRUE)
}
}
closeAllConnections()
rmarkdown::render("outfile_temp.Rmd", output_file = f.out.name)
file.remove("outfile_temp.Rmd")
}
现在您可以使用要使用的.Rmd文件和所需的数据详细信息函数调用该函数:
render_dynamic_prov("example_presentation.Rmd", data_provenance())
生成一张幻灯片:
但是,如果您现在创建一个新的数据详细信息功能,例如:
data_provenance_fake <- function(){
paste(
paste("* Data as of:",
base %>%
arrange(desc(Date)) %>%
select(DateTime) %>%
slice(1) %>%
.$DateTime),
paste("* Dataset:", hash(tmp_file)),
paste("* Report version:", params$version),
paste("* Fake number:", runif(n = 1)),
paste("* Here is another bullet:", rnorm(1))
, sep = "\n")
}
您可以使用此新数据详情功能再次运行render_dynamic_prov
功能,以输出不同的幻灯片:
render_dynamic_prov("example_presentation.Rmd", data_provenance_fake())