Debian Codegraph @

man/renderImage.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/shinywrappers.R
3 \name{renderImage}
4 \alias{renderImage}
5 \title{Image file output}
6 \usage{
7 renderImage(
8 expr,
9 env = parent.frame(),
10 quoted = FALSE,
11 deleteFile,
12 outputArgs = list()
13 )
14 }
15 \arguments{
16 \item{expr}{An expression that returns a list.}
17
18 \item{env}{The parent environment for the reactive expression. By default,
19 this is the calling environment, the same as when defining an ordinary
20 non-reactive expression. If \code{expr} is a quosure and \code{quoted} is \code{TRUE},
21 then \code{env} is ignored.}
22
23 \item{quoted}{If it is \code{TRUE}, then the \code{\link[=quote]{quote()}}ed value of \code{expr}
24 will be used when \code{expr} is evaluated. If \code{expr} is a quosure and you
25 would like to use its expression as a value for \code{expr}, then you must set
26 \code{quoted} to \code{TRUE}.}
27
28 \item{deleteFile}{Should the file in \code{func()$src} be deleted after
29 it is sent to the client browser? Generally speaking, if the image is a
30 temp file generated within \code{func}, then this should be \code{TRUE};
31 if the image is not a temp file, this should be \code{FALSE}. (For backward
32 compatibility reasons, if this argument is missing, a warning will be
33 emitted, and if the file is in the temp directory it will be deleted. In
34 the future, this warning will become an error.)}
35
36 \item{outputArgs}{A list of arguments to be passed through to the implicit
37 call to \code{\link[=imageOutput]{imageOutput()}} when \code{renderImage} is used in an
38 interactive R Markdown document.}
39 }
40 \description{
41 Renders a reactive image that is suitable for assigning to an \code{output}
42 slot.
43 }
44 \details{
45 The expression \code{expr} must return a list containing the attributes for
46 the \code{img} object on the client web page. For the image to display,
47 properly, the list must have at least one entry, \code{src}, which is the
48 path to the image file. It may also useful to have a \code{contentType}
49 entry specifying the MIME type of the image. If one is not provided,
50 \code{renderImage} will try to autodetect the type, based on the file
51 extension.
52
53 Other elements such as \code{width}, \code{height}, \code{class}, and
54 \code{alt}, can also be added to the list, and they will be used as
55 attributes in the \code{img} object.
56
57 The corresponding HTML output tag should be \code{div} or \code{img} and have
58 the CSS class name \code{shiny-image-output}.
59 }
60 \examples{
61 ## Only run examples in interactive R sessions
62 if (interactive()) {
63 options(device.ask.default = FALSE)
64
65 ui <- fluidPage(
66 sliderInput("n", "Number of observations", 2, 1000, 500),
67 plotOutput("plot1"),
68 plotOutput("plot2"),
69 plotOutput("plot3")
70 )
71
72 server <- function(input, output, session) {
73
74 # A plot of fixed size
75 output$plot1 <- renderImage({
76 # A temp file to save the output. It will be deleted after renderImage
77 # sends it, because deleteFile=TRUE.
78 outfile <- tempfile(fileext='.png')
79
80 # Generate a png
81 png(outfile, width=400, height=400)
82 hist(rnorm(input$n))
83 dev.off()
84
85 # Return a list
86 list(src = outfile,
87 alt = "This is alternate text")
88 }, deleteFile = TRUE)
89
90 # A dynamically-sized plot
91 output$plot2 <- renderImage({
92 # Read plot2's width and height. These are reactive values, so this
93 # expression will re-run whenever these values change.
94 width <- session$clientData$output_plot2_width
95 height <- session$clientData$output_plot2_height
96
97 # A temp file to save the output.
98 outfile <- tempfile(fileext='.png')
99
100 png(outfile, width=width, height=height)
101 hist(rnorm(input$n))
102 dev.off()
103
104 # Return a list containing the filename
105 list(src = outfile,
106 width = width,
107 height = height,
108 alt = "This is alternate text")
109 }, deleteFile = TRUE)
110
111 # Send a pre-rendered image, and don't delete the image after sending it
112 # NOTE: For this example to work, it would require files in a subdirectory
113 # named images/
114 output$plot3 <- renderImage({
115 # When input$n is 1, filename is ./images/image1.jpeg
116 filename <- normalizePath(file.path('./images',
117 paste('image', input$n, '.jpeg', sep='')))
118
119 # Return a list containing the filename
120 list(src = filename)
121 }, deleteFile = FALSE)
122 }
123
124 shinyApp(ui, server)
125 }
126 }
127 \seealso{
128 \itemize{
129 \item For more details on how the images are generated, and how to control
130 the output, see \code{\link[=plotPNG]{plotPNG()}}.
131 \item Use \code{\link[=outputOptions]{outputOptions()}} to set general output options for an image output.
132 }
133 }