Welcome to SO! Please provide a reprex the next time - this will help to get help.
renderImage
needs to return a list with at least a src
slot. From the help of renderImage
The expression expr
must return a list containing the attributes for the img
object on the client web page. For the image to display, properly, the list must have at least one entry, src
, which is the path to the image file.
So you have to find a way how to tell shiny
not to render the image at all when your button is pressed. A neat way to do so is to use req
like in the example below. The button render
shows the pic and whenever you press delete
it goes away. This is due to the fact that req
silently raises an error, which tells shiny
not to proceed in the observer/render*
function and thus not to show the picture in the first place.
library(shiny)
ui <- fluidPage(imageOutput("pic"),
actionButton("go", "Render Print"),
actionButton("delete", "Remove Pic"))
server <- function(input, output, session) {
toggle_pic <- reactiveVal(FALSE)
observeEvent(input$go, toggle_pic(TRUE), ignoreInit = TRUE)
observeEvent(input$delete, toggle_pic(FALSE), ignoreInit = TRUE)
output$pic <- renderImage({
req(toggle_pic())
outfile <- tempfile(fileext = ".png")
# Generate a png
png(outfile, width=400, height=400)
hist(rnorm(100))
dev.off()
# Return a list
list(src = outfile,
alt = "This is alternate text")
}, deleteFile = TRUE)
}
shinyApp(ui, server)