【问题标题】:Render Rmarkdown to directory outside R package将 Rmarkdown 渲染到 R 包外的目录
【发布时间】:2023-04-03 10:39:01
【问题描述】:

我有一个生成 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..?
}

【问题讨论】:

    标签: r r-markdown knitr


    【解决方案1】:

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

    要选择一个文件夹,你可以使用基本R包utils中的函数choose.dir()rstudioapi中的函数selectDirectory()(这意味着它只有在函数从rstudio启动时才会起作用)

    要显示 html 文件使用函数 browseURL 来自基本 R 包 utils 使用由 paste0("file://",&lt;full path of your file using / separator&gt;) 构造的 url

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 2021-12-27
      • 1970-01-01
      • 2015-09-27
      • 2017-03-10
      • 1970-01-01
      • 2017-09-17
      • 2022-01-01
      • 1970-01-01
      相关资源
      最近更新 更多