Skip to contents

Load Package

Some filler text.

Define Function

See below for some random example code.

render_rmarkdown <- function(pkg, input, output, ..., copy_images = TRUE, quiet = TRUE) {

  input_path <- path_abs(input, pkg$src_path)
  output_path <- path_abs(output, pkg$dst_path)

  if (!file_exists(input_path)) {
    stop("Can't find ", src_path(input), call. = FALSE)
  }

  cat_line("Reading ", src_path(input))
  digest <- file_digest(output_path)

  args <- list(
    input = input_path,
    output_file = path_file(output_path),
    output_dir = path_dir(output_path),
    intermediates_dir = tempdir(),
    encoding = "UTF-8",
    envir = globalenv(),
    ...,
    quiet = quiet
  )

  path <- tryCatch(
    rmarkdown::r_safe(
      function(...) rmarkdown::render(...),
      args = args,
      show = !quiet,
      env = c(
        rmarkdown::rcmd_safe_env(),
        BSTINPUTS = bst_paths(input_path),
        TEXINPUTS = tex_paths(input_path),
        BIBINPUTS = bib_paths(input_path),
        R_CLI_NUM_COLORS = 256
      )
    ),
    error = function(cnd) {
      rule("RMarkdown error")
      cat(gsub("\r", "", cnd$stderr, fixed = TRUE))
      rule()
      abort("Failed to render RMarkdown", parent = cnd)
    }
  )

  if (identical(path_ext(path)[[1]], "html")) {
    update_html(
      path,
      tweak_rmarkdown_html,
      input_path = path_dir(input_path),
      pkg = pkg
    )
  }
  if (digest != file_digest(output_path)) {
    cat_line("Writing ", dst_path(output))
  }

  # Copy over images needed by the document
  if (copy_images) {
    ext <- rmarkdown::find_external_resources(input_path)

    # copy web + explicit files beneath vignettes/
    is_child <- path_has_parent(ext$path, ".")
    ext_path <- ext$path[(ext$web | ext$explicit) & is_child]

    src <- path(path_dir(input_path), ext_path)
    dst <- path(path_dir(output_path), ext_path)
    # Make sure destination paths exist before copying files there
    dir_create(unique(path_dir(dst)))
    file_copy(src, dst, overwrite = TRUE)
  }
  check_missing_images(pkg, input, output)

  invisible(path)
}

Session Info

sessionInfo()
#> R version 4.4.0 (2024-04-24)
#> Platform: x86_64-apple-darwin20
#> Running under: macOS Monterey 12.7.4
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRblas.0.dylib 
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> time zone: UTC
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> loaded via a namespace (and not attached):
#>  [1] digest_0.6.35     desc_1.4.3        R6_2.5.1          fastmap_1.1.1    
#>  [5] xfun_0.43         magrittr_2.0.3    cachem_1.0.8      knitr_1.46       
#>  [9] memoise_2.0.1     htmltools_0.5.8.1 rmarkdown_2.26    lifecycle_1.0.4  
#> [13] cli_3.6.2         vctrs_0.6.5       sass_0.4.9        pkgdown_2.0.9    
#> [17] textshaping_0.3.7 jquerylib_0.1.4   systemfonts_1.0.6 compiler_4.4.0   
#> [21] purrr_1.0.2       tools_4.4.0       ragg_1.3.1        bslib_0.7.0      
#> [25] evaluate_0.23     yaml_2.3.8        jsonlite_1.8.8    rlang_1.1.3      
#> [29] fs_1.6.4