Image
#| standalone: true
#| components: [viewer]
#| viewerHeight: 200
library(shiny)
library(bslib)
ui <- page_fluid(
imageOutput("image")
)
server <- function(input, output) {
output$image <- renderImage(
{
list(src = "shiny.png", width = "100px")
},
deleteFile = FALSE
)
}
shinyApp(ui = ui, server = server)
library(shiny)
library(bslib)
ui <- page_fluid(
imageOutput("image")
)
server <- function(input, output) {
output$image <- renderImage(
{
list(src = "shiny.png", height = "25%")
},
deleteFile = FALSE
)
}
shinyApp(ui = ui, server = server)Relevant Functions
-
imageOutput
imageOutput(outputId, width = "100%", height = "400px", click = NULL, dblclick = NULL, hover = NULL, brush = NULL, inline = FALSE, fill = FALSE) -
renderImage
renderImage(expr, env = parent.frame(), quoted = FALSE, deleteFile, outputArgs = list())
Details
To make a reactive image, follow three steps:
Call
imageOutput()in the UI of your app to create a div in which to display the image. Where you call this function within the UI functions will determine where the image will appear within the layout of the app. Set theoutputIdargument ofimageOutput()to a unique value.Optionally, set
inline = TRUEto place the image inline with the text or elements that precede it or usefill = TRUEto allow the image to grow or shrink to fill its container as the app is resized.Within the server function, call
renderImage()and save its output as an element of theoutputlist. Name the element after theoutputIdused above. For example,output$image <- renderImage().imageOutput()will display the value of theoutputelement whose name matches itsoutputId.Pass
renderImage()a block of code, surrounded with{}. The code should return a list containing at least one entry,src, providing the path to the image file. You can also add other elements such aswidth,height,class, andaltto the list, and they will be used as attributes for the rendered image.Set the
deleteFileargument ofrenderImage()toTRUEif you want the file deleted after it is sent to the client browser, andFALSEotherwise. Typically, you’ll want to usedeleteFile = TRUEif the image is a temp file generated withinrenderImage(), anddeleteFile = FALSEotherwise.