首页 > 解决方案 > 将 Rmarkdown 渲染到 R 包外的目录

问题描述

我有一个生成 RMarkdown html 报告的 R 包。.Rmd 文件位于系统库/包目录中。我想创建一个函数htmlreport()来将 html 报告保存到指定的输出目录路径。如果output_path未指定,该功能应该可以浏览到输出文件夹。

渲染后我还想打开生成的 html 报告。

我不知道该怎么做。似乎输出文件的路径总是相对于 .Rmd 文件。我怎样才能做到这一点?

htmlreport <- function(output_path = NULL){
 f <- system.file("inst/rmd", "html_report.Rmd", package = "abc")

 if (is.null(outputh_path)) { 
    output_path <- ?? "Choose output folder" ....??
 }

 rmarkdown::render(f, output_dir = output_path) 

 # and open the html report..?
}

标签: rr-markdownknitr

解决方案


output_dir可以是任何路径,不仅是相对于 rmd 文件。

要选择文件夹,您可以使用choose.dir()基本 R 包中的utils函数或函数selectDirectory()中的函数rstudioapi(这意味着它仅在函数从 启动时才有效rstudio

使用构造的 url显示browseURL来自基本 R 包的 html 文件使用函数utilspaste0("file://",<full path of your file using / separator>)


推荐阅读