mirror of
https://github.com/rstudio/shiny.git
synced 2026-02-05 12:15:14 -05:00
236 lines
7.6 KiB
R
236 lines
7.6 KiB
R
% Generated by roxygen2 (4.1.0): do not edit by hand
|
|
% Please edit documentation in R/bootstrap.R
|
|
\name{imageOutput}
|
|
\alias{imageOutput}
|
|
\alias{plotOutput}
|
|
\title{Create an plot or image output element}
|
|
\usage{
|
|
imageOutput(outputId, width = "100\%", height = "400px", clickId = NULL,
|
|
clickClip = TRUE, hoverId = NULL, hoverDelay = NULL,
|
|
hoverDelayType = NULL, hoverOpts = hoverOptions(), brushId = NULL,
|
|
brushOpts = brushOptions(), inline = FALSE)
|
|
|
|
plotOutput(outputId, width = "100\%", height = "400px", clickId = NULL,
|
|
clickClip = TRUE, hoverId = NULL, hoverDelay = NULL,
|
|
hoverDelayType = NULL, hoverOpts = hoverOptions(), brushId = NULL,
|
|
brushOpts = brushOptions(), inline = FALSE)
|
|
}
|
|
\arguments{
|
|
\item{outputId}{output variable to read the plot/image from.}
|
|
|
|
\item{width,height}{Image width/height. Must be a valid CSS unit (like
|
|
\code{"100\%"}, \code{"400px"}, \code{"auto"}) or a number, which will be
|
|
coerced to a string and have \code{"px"} appended. These two arguments are
|
|
ignored when \code{inline = TRUE}, in which case the width/height of a plot
|
|
must be specified in \code{renderPlot()}. Note that, for height, using
|
|
\code{"auto"} or \code{"100\%"} generally will not work as expected,
|
|
because of how height is computed with HTML/CSS.}
|
|
|
|
\item{clickId}{If not \code{NULL}, the plot will send coordinates to the
|
|
server whenever it is clicked. This information will be accessible on the
|
|
\code{input} object using \code{input$}\emph{\code{clickId}}. The value
|
|
will be a named list or vector with \code{x} and \code{y} elements
|
|
indicating the mouse position in user units.}
|
|
|
|
\item{clickClip}{Should clicking events be constrained to the plotting area?
|
|
If \code{FALSE}, it will respond to clicks even outside of the plotting area.}
|
|
|
|
\item{hoverId}{If not \code{NULL}, the plot will send coordinates to the
|
|
server whenever the mouse pauses on the plot for more than the number of
|
|
milliseconds determined by \code{hoverTimeout}. This information will be
|
|
accessible on the \code{input} object using
|
|
\code{input$}\emph{\code{hoverId}}. The value will be \code{NULL} if the
|
|
user is not hovering, and a named list or vector with \code{x} and \code{y}
|
|
elements indicating the mouse position in user units.}
|
|
|
|
\item{hoverDelay}{Deprecated; use hoverOpts insetead. Also see the
|
|
\code{\link{hoverOptions}} function.}
|
|
|
|
\item{hoverDelayType}{Deprecated; use hoverOpts insetead. Also see the
|
|
\code{\link{hoverOptions}} function.}
|
|
|
|
\item{hoverOpts}{An object representing hovering options. This should be
|
|
generated by the \code{\link{hoverOptions}} function.}
|
|
|
|
\item{brushId}{If not \code{NULL}, the plot will allow the user to "brush" in
|
|
the plotting area, and will send information about the brushed area to the
|
|
server. Brushing means that the user will be able to draw a rectangle in
|
|
the plotting area and drag it around.}
|
|
|
|
\item{brushOpts}{An object representing brushing options. This should be
|
|
generated by the \code{\link{brushOptions}} function.}
|
|
|
|
\item{inline}{use an inline (\code{span()}) or block container (\code{div()})
|
|
for the output}
|
|
}
|
|
\value{
|
|
A plot or image output element that can be included in a panel.
|
|
}
|
|
\description{
|
|
Render a \code{\link{renderPlot}} or \code{\link{renderImage}} within an
|
|
application page.
|
|
}
|
|
\note{
|
|
The arguments \code{clickId} and \code{hoverId} only work for R base
|
|
graphics (see the \pkg{\link{graphics}} package). They do not work for
|
|
\pkg{\link[grid:grid-package]{grid}}-based graphics, such as \pkg{ggplot2},
|
|
\pkg{lattice}, and so on.
|
|
}
|
|
\section{Interactive plots}{
|
|
|
|
|
|
Plots and images in Shiny support mouse-based interaction, via clicking,
|
|
hovering, and brushing. When these interaction events occur, the mouse
|
|
coordinates will be sent to the server as \code{input$} variables, as
|
|
specified by \code{clickId}, \code{hoverId}, or \code{brushId}.
|
|
|
|
For \code{plotOutput}, the coordinates will be sent scaled to the data
|
|
space, if possible. (At the moment, plots generated by base graphics
|
|
support this scaling, although plots generated by grid or ggplot2 do not.)
|
|
If scaling is not possible, the raw pixel coordinates will be sent. For
|
|
\code{imageOutput}, the coordinates will be sent in raw pixel coordinates.
|
|
}
|
|
\examples{
|
|
# Only run these examples in interactive R sessions
|
|
if (interactive()) {
|
|
|
|
# A basic shiny app with a plotOutput
|
|
shinyApp(
|
|
ui = fluidPage(
|
|
sidebarLayout(
|
|
sidebarPanel(
|
|
actionButton("newplot", "New plot")
|
|
),
|
|
mainPanel(
|
|
plotOutput("plot")
|
|
)
|
|
)
|
|
),
|
|
server = function(input, output) {
|
|
output$plot <- renderPlot({
|
|
input$newplot
|
|
# Add a little noise to the cars data
|
|
cars2 <- cars + rnorm(nrow(cars))
|
|
plot(cars2)
|
|
})
|
|
}
|
|
)
|
|
|
|
|
|
# A demonstration of clicking, hovering, and brushing
|
|
shinyApp(
|
|
ui = basicPage(
|
|
fluidRow(
|
|
column(width = 4,
|
|
plotOutput("plot", height=300,
|
|
clickId="plot_click",
|
|
hoverId="plot_hover",
|
|
hoverOpts = hoverOptions(delayType = "throttle"),
|
|
brushId="plot_brush",
|
|
brushOpts = brushOptions(color = "red")
|
|
)
|
|
),
|
|
column(width = 3,
|
|
verbatimTextOutput("plot_clickinfo"),
|
|
verbatimTextOutput("plot_hoverinfo")
|
|
),
|
|
column(width = 3,
|
|
wellPanel(actionButton("newplot", "New plot")),
|
|
verbatimTextOutput("plot_brushinfo")
|
|
)
|
|
)
|
|
),
|
|
server = function(input, output, session) {
|
|
output$plot <- renderPlot({
|
|
input$newplot
|
|
# Add a little noise to the cars data
|
|
cars2 <- cars + rnorm(nrow(cars))
|
|
plot(cars2)
|
|
})
|
|
output$plot_clickinfo <- renderPrint({
|
|
cat("Click:\\n")
|
|
str(input$plot_click)
|
|
})
|
|
output$plot_hoverinfo <- renderPrint({
|
|
cat("Hover (throttled):\\n")
|
|
str(input$plot_hover)
|
|
})
|
|
output$plot_brushinfo <- renderPrint({
|
|
cat("Brush (debounced):\\n")
|
|
str(input$plot_brush)
|
|
})
|
|
}
|
|
)
|
|
|
|
|
|
# Demo of clicking, hovering, brushing with imageOutput
|
|
# Note that coordinates are in pixels
|
|
shinyApp(
|
|
ui = basicPage(
|
|
fluidRow(
|
|
column(width = 4,
|
|
imageOutput("image", height=300,
|
|
clickId="image_click",
|
|
hoverId="image_hover",
|
|
hoverOpts = hoverOptions(delay = 500, delayType = "throttle"),
|
|
brushId="image_brush",
|
|
brushOpts = brushOptions(color = "red")
|
|
)
|
|
),
|
|
column(width = 3,
|
|
verbatimTextOutput("image_clickinfo"),
|
|
verbatimTextOutput("image_hoverinfo")
|
|
),
|
|
column(width = 3,
|
|
wellPanel(actionButton("newimage", "New image")),
|
|
verbatimTextOutput("image_brushinfo")
|
|
)
|
|
)
|
|
),
|
|
server = function(input, output, session) {
|
|
output$image <- renderImage({
|
|
input$newimage
|
|
|
|
# Get width and height of image output
|
|
width <- session$clientData$output_image_width
|
|
height <- session$clientData$output_image_height
|
|
|
|
# Write to a temporary PNG file
|
|
outfile <- tempfile(fileext = ".png")
|
|
|
|
png(outfile, width=width, height=height)
|
|
plot(rnorm(200), rnorm(200))
|
|
dev.off()
|
|
|
|
# Return a list containing information about the image
|
|
list(
|
|
src = outfile,
|
|
contentType = "image/png",
|
|
width = width,
|
|
height = height,
|
|
alt = "This is alternate text"
|
|
)
|
|
})
|
|
output$image_clickinfo <- renderPrint({
|
|
cat("Click:\\n")
|
|
str(input$image_click)
|
|
})
|
|
output$image_hoverinfo <- renderPrint({
|
|
cat("Hover (throttled):\\n")
|
|
str(input$image_hover)
|
|
})
|
|
output$image_brushinfo <- renderPrint({
|
|
cat("Brush (debounced):\\n")
|
|
str(input$image_brush)
|
|
})
|
|
}
|
|
)
|
|
|
|
}
|
|
}
|
|
\seealso{
|
|
For the corresponding server-side functions, see
|
|
\code{\link{renderPlot}} and \code{\link{renderImage}}.
|
|
}
|
|
|