将 Rmarkdown 渲染到 R 包外的目录

Render Rmarkdown to directory outside R package

我有一个生成 RMarkdown html 报告的 R 包。 .Rmd 文件位于系统 library/package 目录中。 我想创建一个函数 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..?
}

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

到select一个文件夹,你可以使用基本R包utils中的函数choose.dir()rstudioapi中的函数selectDirectory()(这意味着它仅当函数从 rstudio)

启动时才有效

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