From c25c80cf1996af4cbf7eb748950a7ea5639c7a55 Mon Sep 17 00:00:00 2001 From: Leonidas Zhak <70497898+LeonidasZhak@users.noreply.github.com> Date: Sat, 6 Jun 2026 10:00:20 +0800 Subject: [PATCH] docs: add htmlDependency examples --- NEWS.md | 4 ++++ R/html_dependency.R | 42 ++++++++++++++++++++++++++++++++++++++++++ man/htmlDependency.Rd | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/NEWS.md b/NEWS.md index b98ba7d0..ad5c5844 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # htmltools (development version) +* `htmlDependency()` now includes examples showing dependencies from local + files, URL-based dependencies with script attributes, and package-bundled + dependencies created from a runtime wrapper (#362). + * The `.noWS` documentation now lists `inside` for tags and correctly describes `outside` for `HTML()` (#303). diff --git a/R/html_dependency.R b/R/html_dependency.R index 9cb5a569..be407949 100644 --- a/R/html_dependency.R +++ b/R/html_dependency.R @@ -88,6 +88,48 @@ #' The shape of the `htmlDependency` object is described (in TypeScript code) #' [here](https://github.com/rstudio/shiny/blob/474f1400/srcts/src/shiny/render.ts#L79-L115). #' +#' @examples +#' # A dependency from files on disk. The script and stylesheet paths are +#' # relative to the dependency's `src` directory. +#' dir <- tempfile("html-dependency-") +#' dir.create(dir) +#' writeLines("console.log('demo dependency');", file.path(dir, "demo.js")) +#' writeLines(".demo { color: steelblue; }", file.path(dir, "demo.css")) +#' +#' htmlDependency( +#' name = "demo", +#' version = "1.0.0", +#' src = dir, +#' script = "demo.js", +#' stylesheet = "demo.css" +#' ) +#' +#' # A dependency loaded from a URL can use `href` and script attributes such as +#' # integrity and crossorigin. +#' htmlDependency( +#' name = "demo-cdn", +#' version = "1.0.0", +#' src = c(href = "https://example.com/demo/1.0.0"), +#' script = list( +#' src = "demo.min.js", +#' integrity = "sha384-...", +#' crossorigin = "anonymous" +#' ) +#' ) +#' +#' # If dependency files live under a package's inst/ directory, wrap the +#' # dependency in a function and use a relative `src` with `package`. +#' myDependency <- function() { +#' htmlDependency( +#' name = "my-package-assets", +#' version = "1.0.0", +#' src = "www", +#' package = "mypackage", +#' script = "app.js", +#' stylesheet = "app.css" +#' ) +#' } +#' #' @export htmlDependency <- function(name, version, diff --git a/man/htmlDependency.Rd b/man/htmlDependency.Rd index 0d6dfc10..2c9775d8 100644 --- a/man/htmlDependency.Rd +++ b/man/htmlDependency.Rd @@ -111,6 +111,49 @@ installs the binary package. If there are any absolute paths, instead of calling \code{htmlDependency} at build-time, it should be called at run-time. This can be done by wrapping the \code{htmlDependency} call in a function. +} +\examples{ +# A dependency from files on disk. The script and stylesheet paths are +# relative to the dependency's `src` directory. +dir <- tempfile("html-dependency-") +dir.create(dir) +writeLines("console.log('demo dependency');", file.path(dir, "demo.js")) +writeLines(".demo { color: steelblue; }", file.path(dir, "demo.css")) + +htmlDependency( + name = "demo", + version = "1.0.0", + src = dir, + script = "demo.js", + stylesheet = "demo.css" +) + +# A dependency loaded from a URL can use `href` and script attributes such as +# integrity and crossorigin. +htmlDependency( + name = "demo-cdn", + version = "1.0.0", + src = c(href = "https://example.com/demo/1.0.0"), + script = list( + src = "demo.min.js", + integrity = "sha384-...", + crossorigin = "anonymous" + ) +) + +# If dependency files live under a package's inst/ directory, wrap the +# dependency in a function and use a relative `src` with `package`. +myDependency <- function() { + htmlDependency( + name = "my-package-assets", + version = "1.0.0", + src = "www", + package = "mypackage", + script = "app.js", + stylesheet = "app.css" + ) +} + } \seealso{ Use \code{\link[=attachDependencies]{attachDependencies()}} to associate a list of