# NOT RUN {
## Only run examples in interactive R sessions
if (interactive()) {
options(device.ask.default = FALSE)
ui <- fluidPage(
sliderInput("n", "Number of observations", 2, 1000, 500),
plotOutput("plot1"),
plotOutput("plot2"),
plotOutput("plot3")
)
server <- function(input, output, session) {
# A plot of fixed size
output$plot1 <- renderImage({
# A temp file to save the output. It will be deleted after renderImage
# sends it, because deleteFile=TRUE.
outfile <- tempfile(fileext='.png')
# Generate a png
png(outfile, width=400, height=400)
hist(rnorm(input$n))
dev.off()
# Return a list
list(src = outfile,
alt = "This is alternate text")
}, deleteFile = TRUE)
# A dynamically-sized plot
output$plot2 <- renderImage({
# Read plot2's width and height. These are reactive values, so this
# expression will re-run whenever these values change.
width <- session$clientData$output_plot2_width
height <- session$clientData$output_plot2_height
# A temp file to save the output.
outfile <- tempfile(fileext='.png')
png(outfile, width=width, height=height)
hist(rnorm(input$n))
dev.off()
# Return a list containing the filename
list(src = outfile,
width = width,
height = height,
alt = "This is alternate text")
}, deleteFile = TRUE)
# Send a pre-rendered image, and don't delete the image after sending it
# NOTE: For this example to work, it would require files in a subdirectory
# named images/
output$plot3 <- renderImage({
# When input$n is 1, filename is ./images/image1.jpeg
filename <- normalizePath(file.path('./images',
paste('image', input$n, '.jpeg', sep='')))
# Return a list containing the filename
list(src = filename)
}, deleteFile = FALSE)
}
shinyApp(ui, server)
}
# }
Run the code above in your browser using DataLab