File: deployDoc.R

package info (click to toggle)
r-cran-rsconnect 1.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,044 kB
  • sloc: python: 185; sh: 13; makefile: 5
file content (88 lines) | stat: -rw-r--r-- 2,820 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#' Deploy a single document
#'
#' @description
#' Deploys a single R Markdown, Quarto document, or other file (e.g. `.html` or
#' `.pdf`).
#'
#' When deploying an `.Rmd`, `.Qmd`, or `.html`, `deployDoc()` will attempt to
#' automatically discover dependencies using [rmarkdown::find_external_resources()],
#' and include an `.Rprofile` if present. If you find that the document is
#' missing dependencies, either specify the dependencies explicitly in the
#' document (see [rmarkdown::find_external_resources()] for details), or call
#' [deployApp()] directly and specify your own file list in `appFiles`.
#'
#' @param doc Path to the document to deploy.
#' @param ... Additional arguments to [deployApp()]. Do not supply `appDir`,
#'   `appFiles`, or `appPrimaryDoc`; these three parameters are automatically
#'   generated by `deployDoc` from the document.
#' @inheritParams deployApp
#' @family Deployment functions
#' @export
#' @examples
#' \dontrun{
#' deployDoc("my-report.Rmd")
#' deployDoc("static-file.html")
#' }
deployDoc <- function(doc, ..., logLevel = c("normal", "quiet", "verbose")) {
  logLevel <- arg_match(logLevel)

  doc <- standardizeSingleDocDeployment(doc, quiet = logLevel == "quiet")
  deployApp(
    appDir = doc$appDir,
    appPrimaryDoc = doc$appPrimaryDoc,
    appFiles = doc$appFiles,
    ...,
    logLevel = logLevel
  )
}

standardizeSingleDocDeployment <- function(path,
                                           quiet = FALSE,
                                           error_call = caller_env(),
                                           error_arg = caller_arg(path)) {
  check_installed(
    "rmarkdown",
    version = "0.5.2",
    reason = "to deploy individual R Markdown documents"
  )
  check_file(path, error_call = error_call, error_arg = error_arg)
  path <- normalizePath(path)

  if (isShinyRmd(path)) {
    # deploy entire directory
    appFiles <- NULL
  } else if (isStaticFile(path)) {
    taskStart(quiet, "Discovering document dependencies...")
    resources <- rmarkdown::find_external_resources(path)
    taskComplete(quiet, "Document dependencies discovered")

    appFiles <- c(basename(path), resources$path)

    if (isRenderedFile(path)) {
      candidates <- c(".Rprofile", "renv.lock", "requirements.txt")
      exists <- file.exists(file.path(dirname(path), candidates))
      appFiles <- c(appFiles, candidates[exists])
    }

    appFiles
  } else {
    # deploy just the file
    appFiles <- basename(path)
  }

  list(
    appDir = normalizePath(dirname(path)),
    appPrimaryDoc = basename(path),
    appFiles = appFiles
  )
}

isStaticFile <- function(path) {
  ext <- tolower(tools::file_ext(path))
  ext %in% c("rmd", "qmd", "html", "htm")
}

isRenderedFile <- function(path) {
  ext <- tolower(tools::file_ext(path))
  ext %in% c("rmd", "qmd")
}