R/standalone.R

Defines functions find_bytes vol_viewer_template vol_standalone

Documented in vol_standalone vol_viewer_template

# ============================ #
# The single-file build        #
# ============================ #

#' Carry a payload inside one HTML file
#'
#' @title Carry a payload inside one HTML file
#' @description Inlines a payload into the viewer page, so that the result opens
#'   from a local file with no server and travels as a single attachment.
#' @param payload A `vol_payload`, or the path of a payload directory.
#' @param path Destination file. Defaults to a file named after the template,
#'   `volume_viewer_standalone.html`, in the session's temporary directory.
#' @param viewer The page template to inline into. Defaults to the copy the
#'   package ships.
#' @param manifest The manifest to embed. Taken from `payload` when it is a
#'   `vol_payload`.
#' @return The path written, invisibly.
#' @details A page served over http fetches its images. The same page opened
#'   from disk cannot, because a browser refuses a cross-origin read from a
#'   local file, so this build carries every image as a data URI instead, at the
#'   cost of the third that base64 adds. For the case study at its native grid
#'   the directory is 1.5 MB and the single file 2.07 MB.
#'
#'   The splice works on bytes and not on lines, so the accented characters of
#'   the page and its line endings pass through untouched, and the block goes in
#'   immediately before the script that reads it.
#' @examples
#' out <- vol_standalone(vol_example_payload(),
#'                       path = file.path(tempdir(), "med.html"))
#' file.size(out)
#' if (interactive()) utils::browseURL(out)
#' @seealso [vol_payload()] for the payload, [vol_widget()] for the same
#'   renderer inside \pkg{shiny}, [shinyVolumr()] for the application that
#'   drives the whole workflow.
#' @export
vol_standalone <- function(payload, path = NULL, viewer = NULL, manifest = NULL) {
    if (inherits(payload, "vol_payload")) {
        dir <- payload$dir
        if (is.null(manifest)) manifest <- payload$manifest
    } else {
        dir <- payload
        if (is.null(manifest))
            manifest <- jsonlite::fromJSON(file.path(dir, "manifest.json"),
                                           simplifyVector = FALSE)
    }
    if (is.null(viewer)) viewer <- vol_viewer_template()
    if (is.null(path))
        path <- file.path(tempdir(),
                          paste0(tools::file_path_sans_ext(basename(viewer)),
                                 "_standalone.html"))
    names_in_order <- c(vapply(manifest$slices, function(s) s$file, ""),
                        manifest$land$file, manifest$colormap_file$file)
    files <- lapply(names_in_order, function(n)
        paste0("data:image/png;base64,", base64enc::base64encode(file.path(dir, n))))
    names(files) <- names_in_order
    blob <- paste0("<script>\nwindow.__VOLUME_MANIFEST__ = ", to_json(manifest),
                   ";\nwindow.__VOLUME_FILES__ = ", to_json(files), ";\n</script>\n")
    html <- readBin(viewer, "raw", file.size(viewer))
    at <- find_bytes(html, charToRaw("<script>\n\"use strict\";"))
    if (is.na(at)) stop("cannot find the script block in ", viewer, call. = FALSE)
    con <- file(path, "wb")
    on.exit(close(con))
    writeBin(c(html[seq_len(at - 1L)], charToRaw(blob), html[at:length(html)]), con)
    invisible(path)
}

#' Path of the viewer template the package ships
#'
#' @title Path of the viewer template the package ships
#' @description Gives the path of the HTML page that [vol_standalone()] inlines
#'   a payload into, so that a caller can copy it, edit it and pass the edited
#'   copy back.
#' @return A path.
#' @details The template is the viewer with no data in it: the stylesheet, the
#'   control panel and the renderer, and nothing else. Served over http it
#'   fetches a payload directory beside it; opened from a file it says why it
#'   cannot and what to do instead.
#' @examples
#' vol_viewer_template()
#' @seealso [vol_standalone()], which inlines a payload into this file,
#'   [vol_payload()], which writes the payload it reads, [vol_widget()] for the
#'   renderer without the page around it.
#' @export
vol_viewer_template <- function() {
    system.file("extdata", "volume_viewer.html", package = "volumr", mustWork = TRUE)
}

# The first position at which `needle` occurs in `hay`, or NA.
#' @noRd
find_bytes <- function(hay, needle) {
    n <- length(needle)
    starts <- which(hay == needle[1])
    starts <- starts[starts + n - 1L <= length(hay)]
    for (s in starts)
        if (all(hay[s:(s + n - 1L)] == needle)) return(s)
    NA_integer_
}

Try the volumr package in your browser

Any scripts or data that you put into this service are public.

volumr documentation built on Dec. 2, 2026, 5:07 p.m.