Debian Codegraph @

man/plotOutput.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/bootstrap.R
3 \name{plotOutput}
4 \alias{plotOutput}
5 \alias{imageOutput}
6 \title{Create an plot or image output element}
7 \usage{
8 imageOutput(
9 outputId,
10 width = "100\%",
11 height = "400px",
12 click = NULL,
13 dblclick = NULL,
14 hover = NULL,
15 brush = NULL,
16 inline = FALSE,
17 fill = FALSE
18 )
19
20 plotOutput(
21 outputId,
22 width = "100\%",
23 height = "400px",
24 click = NULL,
25 dblclick = NULL,
26 hover = NULL,
27 brush = NULL,
28 inline = FALSE,
29 fill = !inline
30 )
31 }
32 \arguments{
33 \item{outputId}{output variable to read the plot/image from.}
34
35 \item{width, height}{Image width/height. Must be a valid CSS unit (like
36 \code{"100\%"}, \code{"400px"}, \code{"auto"}) or a number, which will be
37 coerced to a string and have \code{"px"} appended. These two arguments are
38 ignored when \code{inline = TRUE}, in which case the width/height of a plot
39 must be specified in \code{renderPlot()}. Note that, for height, using
40 \code{"auto"} or \code{"100\%"} generally will not work as expected,
41 because of how height is computed with HTML/CSS.}
42
43 \item{click}{This can be \code{NULL} (the default), a string, or an object
44 created by the \code{\link[=clickOpts]{clickOpts()}} function. If you use a value like
45 \code{"plot_click"} (or equivalently, \code{clickOpts(id="plot_click")}),
46 the plot will send coordinates to the server whenever it is clicked, and
47 the value will be accessible via \code{input$plot_click}. The value will be
48 a named list with \code{x} and \code{y} elements indicating the mouse
49 position.}
50
51 \item{dblclick}{This is just like the \code{click} argument, but for
52 double-click events.}
53
54 \item{hover}{Similar to the \code{click} argument, this can be \code{NULL}
55 (the default), a string, or an object created by the
56 \code{\link[=hoverOpts]{hoverOpts()}} function. If you use a value like
57 \code{"plot_hover"} (or equivalently, \code{hoverOpts(id="plot_hover")}),
58 the plot will send coordinates to the server pauses on the plot, and the
59 value will be accessible via \code{input$plot_hover}. The value will be a
60 named list with \code{x} and \code{y} elements indicating the mouse
61 position. To control the hover time or hover delay type, you must use
62 \code{\link[=hoverOpts]{hoverOpts()}}.}
63
64 \item{brush}{Similar to the \code{click} argument, this can be \code{NULL}
65 (the default), a string, or an object created by the
66 \code{\link[=brushOpts]{brushOpts()}} function. If you use a value like
67 \code{"plot_brush"} (or equivalently, \code{brushOpts(id="plot_brush")}),
68 the plot will allow the user to "brush" in the plotting area, and will send
69 information about the brushed area to the server, and the value will be
70 accessible via \code{input$plot_brush}. Brushing means that the user will
71 be able to draw a rectangle in the plotting area and drag it around. The
72 value will be a named list with \code{xmin}, \code{xmax}, \code{ymin}, and
73 \code{ymax} elements indicating the brush area. To control the brush
74 behavior, use \code{\link[=brushOpts]{brushOpts()}}. Multiple
75 \code{imageOutput}/\code{plotOutput} calls may share the same \code{id}
76 value; brushing one image or plot will cause any other brushes with the
77 same \code{id} to disappear.}
78
79 \item{inline}{use an inline (\code{span()}) or block container (\code{div()})
80 for the output}
81
82 \item{fill}{Whether or not the returned tag should be treated as a fill item,
83 meaning that its \code{height} is allowed to grow/shrink to fit a fill container
84 with an opinionated height (see \code{\link[htmltools:bindFillRole]{htmltools::bindFillRole()}}) with an
85 opinionated height. Examples of fill containers include \code{bslib::card()} and
86 \code{bslib::card_body_fill()}.}
87 }
88 \value{
89 A plot or image output element that can be included in a panel.
90 }
91 \description{
92 Render a \code{\link[=renderPlot]{renderPlot()}} or \code{\link[=renderImage]{renderImage()}} within an
93 application page.
94 }
95 \note{
96 The arguments \code{clickId} and \code{hoverId} only work for R base graphics
97 (see the \pkg{\link[graphics:graphics-package]{graphics}} package). They do
98 not work for \pkg{\link[grid:grid-package]{grid}}-based graphics, such as
99 \pkg{ggplot2}, \pkg{lattice}, and so on.
100 }
101 \section{Interactive plots}{
102
103
104 Plots and images in Shiny support mouse-based interaction, via clicking,
105 double-clicking, hovering, and brushing. When these interaction events
106 occur, the mouse coordinates will be sent to the server as \verb{input$}
107 variables, as specified by \code{click}, \code{dblclick}, \code{hover}, or
108 \code{brush}.
109
110 For \code{plotOutput}, the coordinates will be sent scaled to the data
111 space, if possible. (At the moment, plots generated by base graphics and
112 ggplot2 support this scaling, although plots generated by lattice and
113 others do not.) If scaling is not possible, the raw pixel coordinates will
114 be sent. For \code{imageOutput}, the coordinates will be sent in raw pixel
115 coordinates.
116
117 With ggplot2 graphics, the code in \code{renderPlot} should return a ggplot
118 object; if instead the code prints the ggplot2 object with something like
119 \code{print(p)}, then the coordinates for interactive graphics will not be
120 properly scaled to the data space.
121 }
122
123 \examples{
124 # Only run these examples in interactive R sessions
125 if (interactive()) {
126
127 # A basic shiny app with a plotOutput
128 shinyApp(
129 ui = fluidPage(
130 sidebarLayout(
131 sidebarPanel(
132 actionButton("newplot", "New plot")
133 ),
134 mainPanel(
135 plotOutput("plot")
136 )
137 )
138 ),
139 server = function(input, output) {
140 output$plot <- renderPlot({
141 input$newplot
142 # Add a little noise to the cars data
143 cars2 <- cars + rnorm(nrow(cars))
144 plot(cars2)
145 })
146 }
147 )
148
149
150 # A demonstration of clicking, hovering, and brushing
151 shinyApp(
152 ui = basicPage(
153 fluidRow(
154 column(width = 4,
155 plotOutput("plot", height=300,
156 click = "plot_click", # Equiv, to click=clickOpts(id="plot_click")
157 hover = hoverOpts(id = "plot_hover", delayType = "throttle"),
158 brush = brushOpts(id = "plot_brush")
159 ),
160 h4("Clicked points"),
161 tableOutput("plot_clickedpoints"),
162 h4("Brushed points"),
163 tableOutput("plot_brushedpoints")
164 ),
165 column(width = 4,
166 verbatimTextOutput("plot_clickinfo"),
167 verbatimTextOutput("plot_hoverinfo")
168 ),
169 column(width = 4,
170 wellPanel(actionButton("newplot", "New plot")),
171 verbatimTextOutput("plot_brushinfo")
172 )
173 )
174 ),
175 server = function(input, output, session) {
176 data <- reactive({
177 input$newplot
178 # Add a little noise to the cars data so the points move
179 cars + rnorm(nrow(cars))
180 })
181 output$plot <- renderPlot({
182 d <- data()
183 plot(d$speed, d$dist)
184 })
185 output$plot_clickinfo <- renderPrint({
186 cat("Click:\n")
187 str(input$plot_click)
188 })
189 output$plot_hoverinfo <- renderPrint({
190 cat("Hover (throttled):\n")
191 str(input$plot_hover)
192 })
193 output$plot_brushinfo <- renderPrint({
194 cat("Brush (debounced):\n")
195 str(input$plot_brush)
196 })
197 output$plot_clickedpoints <- renderTable({
198 # For base graphics, we need to specify columns, though for ggplot2,
199 # it's usually not necessary.
200 res <- nearPoints(data(), input$plot_click, "speed", "dist")
201 if (nrow(res) == 0)
202 return()
203 res
204 })
205 output$plot_brushedpoints <- renderTable({
206 res <- brushedPoints(data(), input$plot_brush, "speed", "dist")
207 if (nrow(res) == 0)
208 return()
209 res
210 })
211 }
212 )
213
214
215 # Demo of clicking, hovering, brushing with imageOutput
216 # Note that coordinates are in pixels
217 shinyApp(
218 ui = basicPage(
219 fluidRow(
220 column(width = 4,
221 imageOutput("image", height=300,
222 click = "image_click",
223 hover = hoverOpts(
224 id = "image_hover",
225 delay = 500,
226 delayType = "throttle"
227 ),
228 brush = brushOpts(id = "image_brush")
229 )
230 ),
231 column(width = 4,
232 verbatimTextOutput("image_clickinfo"),
233 verbatimTextOutput("image_hoverinfo")
234 ),
235 column(width = 4,
236 wellPanel(actionButton("newimage", "New image")),
237 verbatimTextOutput("image_brushinfo")
238 )
239 )
240 ),
241 server = function(input, output, session) {
242 output$image <- renderImage({
243 input$newimage
244
245 # Get width and height of image output
246 width <- session$clientData$output_image_width
247 height <- session$clientData$output_image_height
248
249 # Write to a temporary PNG file
250 outfile <- tempfile(fileext = ".png")
251
252 png(outfile, width=width, height=height)
253 plot(rnorm(200), rnorm(200))
254 dev.off()
255
256 # Return a list containing information about the image
257 list(
258 src = outfile,
259 contentType = "image/png",
260 width = width,
261 height = height,
262 alt = "This is alternate text"
263 )
264 })
265 output$image_clickinfo <- renderPrint({
266 cat("Click:\n")
267 str(input$image_click)
268 })
269 output$image_hoverinfo <- renderPrint({
270 cat("Hover (throttled):\n")
271 str(input$image_hover)
272 })
273 output$image_brushinfo <- renderPrint({
274 cat("Brush (debounced):\n")
275 str(input$image_brush)
276 })
277 }
278 )
279
280 }
281 }
282 \seealso{
283 For the corresponding server-side functions, see \code{\link[=renderPlot]{renderPlot()}} and
284 \code{\link[=renderImage]{renderImage()}}.
285 }