Nothing
# ============================ #
# The htmlwidget #
# ============================ #
#' Render a payload as a widget
#'
#' @title Render a payload as a widget
#' @description Puts the viewer in an R Markdown document, a \pkg{shiny}
#' application or the RStudio viewer, driving the same page that
#' [vol_standalone()] writes.
#' @param payload A `vol_payload`, or the path of a payload directory.
#' @param options Named list of viewer settings applied once the page has
#' loaded. Any state key the page carries is reachable; see Details.
#' @param panel Logical. Draw the control panel of the page, which is the
#' default and is what [shinyVolumr()] shows, or hide it and leave the
#' controls to the host, which is what a figure in a document wants.
#' @param width,height Widget size, passed to \pkg{htmlwidgets}.
#' @param elementId Optional element identifier.
#' @param inputId Identifier under which the widget reports back to \pkg{shiny}.
#' Set it to the output identifier when the widget is rendered with
#' [render_vol_widget()], so that the events below reach the server.
#' @return An object of class `htmlwidget`.
#' @details The widget hosts the viewer in a frame of its own and talks to it
#' with messages, so there is one renderer behind the file, the widget and the
#' application, and no second copy to keep in step.
#'
#' The settings `options` accepts are the ones the panel exposes: the camera
#' as `azim`, `elev`, `dist`, `panX` and `panY`; the colour scale as `vmin`,
#' `vmax`, `gamma` and `cmapRow`; the density as `od` and `ow`; the geometry
#' as `zex`, `d0`, `d1`, `x0`, `x1`, `y0`, `y1` and `ortho`; the depth slab as
#' `slab`, `slabT`, `slabZ` and `slabNorm`; the motions as `spin` and `sweep`;
#' the quality as `steps` and `adaptive`; and the appearance as `light`,
#' `showLand`, `showBox` and `panel`.
#'
#' Inside \pkg{shiny} the widget reports three events through
#' [shiny::observeEvent()]: `<inputId>_ready` when the payload has loaded,
#' carrying the grid and the depth span; `<inputId>_camera` whenever the
#' camera settles; and `<inputId>_snapshot` when a snapshot has been taken.
#' @examples
#' w <- vol_widget(vol_example_payload(),
#' options = list(slab = TRUE, slabT = 25, slabZ = 50))
#' class(w)
#' if (interactive()) w
#' @seealso [vol_payload()] for the payload, [vol_widget_output()] and
#' [render_vol_widget()] for the \pkg{shiny} pair, [vol_snapshot()] to save a
#' PNG from the server, [shinyVolumr()] for the whole workflow.
#' @export
vol_widget <- function(payload, options = list(), panel = TRUE, width = NULL,
height = NULL, elementId = NULL, inputId = NULL) {
html <- vol_standalone_string(payload)
options <- c(options, list(panel = isTRUE(panel)))
htmlwidgets::createWidget(
name = "volumr",
x = list(html = html, options = options, inputId = inputId,
camera = NULL, cameraNonce = 0),
width = width, height = height, elementId = elementId,
package = "volumr",
sizingPolicy = htmlwidgets::sizingPolicy(
defaultWidth = "100%", defaultHeight = 600, padding = 0,
browser.fill = TRUE, knitr.figure = FALSE))
}
# The standalone page as a string, which is what the frame is given.
#' @noRd
vol_standalone_string <- function(payload) {
f <- tempfile(fileext = ".html")
on.exit(unlink(f), add = TRUE)
vol_standalone(payload, path = f)
paste(readLines(f, warn = FALSE, encoding = "UTF-8"), collapse = "\n")
}
#' Widget output for shiny
#'
#' @title Widget output for shiny
#' @description Places the viewer in a \pkg{shiny} user interface.
#' @param outputId Output identifier.
#' @param width,height Size of the element.
#' @return A \pkg{shiny} output element.
#' @details The identifier is also the prefix of the inputs the widget writes
#' back, so an application that names its output `volume` observes
#' `input$volume_ready` and `input$volume_camera`.
#' @examples
#' if (requireNamespace("shiny", quietly = TRUE))
#' vol_widget_output("volume", height = "700px")
#' @seealso [render_vol_widget()] for the server half, [vol_widget()] for the
#' widget itself, [vol_snapshot()] to save a PNG, [shinyVolumr()] for a
#' complete application.
#' @export
vol_widget_output <- function(outputId, width = "100%", height = "600px") {
htmlwidgets::shinyWidgetOutput(outputId, "volumr", width, height,
package = "volumr")
}
#' Render a widget in shiny
#'
#' @title Render a widget in shiny
#' @description The server half of the \pkg{shiny} pair.
#' @param expr An expression returning a `vol_widget`.
#' @param env Environment in which to evaluate `expr`.
#' @param quoted Is `expr` already quoted?
#' @return A \pkg{shiny} render function.
#' @details The camera belongs to the pointer. A camera sent from the server is
#' applied once and only when it carries a nonce the browser has not seen, so
#' a rotation a reader has made survives every other change the server pushes.
#' @examples
#' if (interactive() && requireNamespace("shiny", quietly = TRUE)) {
#' ui <- shiny::fluidPage(
#' shiny::checkboxInput("slab", "Depth slab"),
#' shiny::actionButton("save", "Save a PNG"),
#' vol_widget_output("volume", height = "600px"))
#' server <- function(input, output, session) {
#' output$volume <- render_vol_widget(
#' vol_widget(vol_example_payload(), inputId = "volume"))
#' shiny::observeEvent(input$slab, vol_set("volume", list(slab = input$slab)))
#' shiny::observeEvent(input$save, vol_snapshot("volume", "volume.png"))
#' }
#' shiny::shinyApp(ui, server)
#' }
#' @seealso [vol_widget_output()] for the user-interface half, [vol_widget()],
#' [vol_snapshot()], [shinyVolumr()].
#' @export
render_vol_widget <- function(expr, env = parent.frame(), quoted = FALSE) {
if (!quoted) expr <- substitute(expr)
htmlwidgets::shinyRenderWidget(expr, vol_widget_output, env, quoted = TRUE)
}
#' Save the current view as a PNG
#'
#' @title Save the current view as a PNG
#' @description Asks the browser for the pixels it already holds and hands them
#' to the reader as a download.
#' @param id Output identifier of the widget.
#' @param filename Name the file is offered under.
#' @param session The \pkg{shiny} session.
#' @return `NULL`, invisibly. Called for its effect.
#' @details The canvas holds the frame, so the image is produced in the browser
#' and never crosses the websocket. What the file carries is the render alone:
#' the axis labels, the tick text and the colour bar are drawn over the canvas
#' as HTML and do not reach it.
#' @examples
#' # Inside the server function of a shiny application; see render_vol_widget()
#' # for a complete one.
#' if (interactive() && requireNamespace("shiny", quietly = TRUE)) {
#' server <- function(input, output, session) {
#' output$volume <- render_vol_widget(vol_widget(vol_example_payload()))
#' shiny::observeEvent(input$save, vol_snapshot("volume", "volume.png"))
#' }
#' }
#' @seealso [vol_widget()], [render_vol_widget()], [shinyVolumr()].
#' @export
vol_snapshot <- function(id, filename = "volume.png",
session = shiny::getDefaultReactiveDomain()) {
require_package("shiny", load = FALSE)
session$sendCustomMessage("volumr:download",
list(id = session$ns(id), filename = filename))
invisible(NULL)
}
#' Change a running widget
#'
#' @title Change a running widget
#' @description Sends settings to a widget that is already on screen, without
#' rebuilding it.
#' @param id Output identifier of the widget.
#' @param options Named list of viewer settings; see [vol_widget()] for the keys.
#' @param session The \pkg{shiny} session.
#' @return `NULL`, invisibly. Called for its effect.
#' @details Rendering the widget again would rebuild the whole page and reload
#' the payload, which for a slider that a reader is dragging is the wrong
#' trade. This sends the settings alone, so the state of the camera and of the
#' loaded textures survives.
#' @examples
#' # Inside the server function of a shiny application; see render_vol_widget()
#' # for a complete one.
#' if (interactive() && requireNamespace("shiny", quietly = TRUE)) {
#' server <- function(input, output, session) {
#' output$volume <- render_vol_widget(vol_widget(vol_example_payload()))
#' shiny::observe(vol_set("volume", list(slab = input$slab,
#' slabZ = input$depth)))
#' }
#' }
#' @seealso [vol_widget()] for the widget and the keys it takes,
#' [render_vol_widget()], [vol_snapshot()], [shinyVolumr()].
#' @export
vol_set <- function(id, options,
session = shiny::getDefaultReactiveDomain()) {
require_package("shiny", load = FALSE)
session$sendCustomMessage("volumr:set",
list(id = session$ns(id), options = options))
invisible(NULL)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.