Output and render functions for using spatial() within Shiny applications
and interactive R Markdown / Quarto documents.
Usage
spatialOutput(output_id, width = "100%", height = "560px")
renderSpatial(expr, env = parent.frame(), quoted = FALSE)Arguments
- output_id
Output variable to read from.
- width, height
Element size, passed to
htmlwidgets::shinyWidgetOutput().- expr
An expression that generates a
spatial()widget.- env
The environment in which to evaluate
expr.- quoted
Is
expralready quoted? Defaults toFALSE.
Value
spatialOutput() returns a Shiny output UI element;
renderSpatial() returns a Shiny render function.
Examples
if (interactive() && requireNamespace("shiny", quietly = TRUE)) {
# The image is a drawn stand-in, built as a data URI so the app needs
# no file. A real app puts its image in the app's www/ folder and
# passes the file name, such as "tissue.png", without "www/".
svg <- paste0(
"<svg xmlns='http://www.w3.org/2000/svg' width='600' height='600'>",
"<rect width='600' height='600' fill='#F4ECF0'/>",
"<ellipse cx='300' cy='260' rx='230' ry='190' fill='#D9A9C4'/>",
"</svg>"
)
tissue <- paste0(
"data:image/svg+xml,", utils::URLencode(svg, reserved = TRUE)
)
ui <- shiny::fluidPage(spatialOutput("sp"))
server <- function(input, output) {
output$sp <- renderSpatial({
spots <- data.frame(
x = runif(50, 0, 600),
y = runif(50, 0, 600),
color = rnorm(50)
)
spatial(spots, image = tissue, img_width = 600,
img_height = 600, spot_diameter = 12)
})
}
shiny::shinyApp(ui, server)
}