how can I add a button "Download Plot" in the app that when you click it a popover appears with a text input where you can edit the name of the plot when it is downloaded as a png file.
Drop files here
or click to upload
library(shiny)
library(bslib)
ui <- fluidPage(
theme = bs_theme(version = 5),
# Main plot output
plotOutput("mainPlot"),
# Download button with popover
div(style = "margin: 20px 0;",
actionButton("downloadBtn", "Download Plot", class = "btn-primary"),
# Hidden download button that will be triggered programmatically
downloadButton("downloadPlot", "Download", style = "display: none;")
)
)
server <- function(input, output, session) {
# Create a sample plot
output$mainPlot <- renderPlot({
plot(1:10, 1:10, main = "Sample Plot")
})
# Initialize the popover when the download button is clicked
observeEvent(input$downloadBtn, {
bslib::popover(
id = "downloadBtn",
title = "Download Plot",
content = textInput("filename", "Filename:", value = "myplot.png"),
html = TRUE,
placement = "top",
trigger = "manual"
) |>
show()
})
# Handle the download
output$downloadPlot <- downloadHandler(
filename = function() {
# Get the filename from the text input, ensure it ends with .png
filename <- input$filename
if (!grepl("\\.png$", filename)) {
filename <- paste0(filename, ".png")
}
filename
},
content = function(file) {
# Save the plot as PNG
png(file, width = 800, height = 600)
plot(1:10, 1:10, main = "Sample Plot")
dev.off()
# Hide the popover after download
bslib::popover("downloadBtn") |> hide()
}
)
}
shinyApp(ui, server)
Hi! I can help you with any questions about Shiny and R. What would you like to know?