我正在编写一个个人使用包,用于训练/测试模型,并最终在模型上运行大量的LIME和DALEX解释。我将它们保存为它们自己的ggplot2对象(比如lime_plot_1),在函数结束时,它们都被返回到全局环境。
然而,我希望发生的是,在函数的末尾,我不仅在环境中有这些图形,而且还会呈现一个小的html报表-包含所有生成的图形。
我想指出的是,虽然我知道我可以通过简单地使用Rmarkdown或Rnotebook中的函数来做到这一点,但我希望避免这种情况,因为我计划将它用作.R脚本来简化整个过程(因为我将以一定的频率运行它),并且根据我在.Rmd中运行大块的经验,它往往会导致R崩溃。
理想情况下,我应该有这样的东西:
s_plot <- function(...){
1. constructs LIME explanations
2. constructs DALEX explanations
3. saves explanations as ggplot2 objects, and list them under graphs_list
4. render graphs_list as an html file
}1、2和3都可以工作,但我还没有找到处理4的方法。这不包括在.Rmd文件中完成整个过程。
编辑:感谢@Richard Telford和@Axeman的评论,我弄明白了。下面是函数:
s_render <- function(graphs_list = graphs_list, meta = NULL, cacheable = NA){
currentDate <- Sys.Date()
rmd_file <- paste("/path/to/folder",currentDate,"/report.Rmd", sep="")
file.create(rmd_file)
graphs_list <- c(roc_plot, prc_plot, mp_boxplot, vi_plot, corr_plot)
c(Yaml file headers here, just like in a regular .Rmd) %>% write_lines(rmd_file)
rmarkdown::render(rmd_file,
params = list(
output_file = html_document(),
output_dir = rmd_file))}发布于 2019-06-25 02:36:24
首先,创建一个简单的Rmarkdown文件,该文件接受一个参数。此文件的唯一目标是创建报告。例如,您可以传递一个文件名:
---
title: "test"
author: "Axeman"
date: "24/06/2019"
output: html_document
params:
file: 'test.RDS'
---
```{r}文件<- readRDS(参数$ plot_list )
lapply(plot_list,打印)
我将其另存为test.Rmd。
然后在主脚本中,将打印列表写入磁盘上的临时文件,并将文件名传递给标记报告:
library(ggplot2)
plot_list <- list(
qplot(1:10, 1:10),
qplot(1:10)
)
file <- tempfile()
saveRDS(plot_list, file)
rmarkdown::render('test.Rmd', params = list(file = file))现在,磁盘上有一个包含绘图的.html文件:

https://stackoverflow.com/questions/56739395
复制相似问题