Created
July 28, 2025 01:13
-
-
Save arraytools/c8707f86fa8e7200e5f0c830ecf49927 to your computer and use it in GitHub Desktop.
Shiny Image Download Examples. 'remote_image_download.R' does not need local images. 'local_image_download.R' requires local files. To run the app, runApp('remote_image_download.R').
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This method assumes that the files 'casaos.png' and 'shiny-test.txt' exist in the 'www' subdirectory. | |
# They don't have to be in the 'www' subdirectory. If they're located elsewhere, modify the file paths accordingly. | |
library(shiny) | |
# UI | |
ui <- fluidPage( | |
titlePanel("Image Download Example"), | |
mainPanel( | |
h3("Download Image File"), | |
div( | |
"Click here to download the image: ", | |
downloadButton("downloadImage", | |
label = "casaos.png", | |
class = "btn-link", | |
style = "padding: 0; border: none; background: none; color: #337ab7; text-decoration: underline;") | |
), | |
br(), | |
div( | |
"Click here to download the text file: ", | |
downloadButton("downloadText", | |
label = "shiny-test.txt", | |
class = "btn-link", | |
style = "padding: 0; border: none; background: none; color: #337ab7; text-decoration: underline;") | |
), | |
br(), br(), | |
h4("Image Preview:"), | |
imageOutput("imagePreview") | |
) | |
) | |
# Server | |
server <- function(input, output, session) { | |
# Display image preview | |
output$imagePreview <- renderImage({ | |
list( | |
src = normalizePath("www/casaos.png"), | |
contentType = "image/png", | |
width = 300, | |
alt = "CasaOS Image" | |
) | |
}, deleteFile = FALSE) | |
# Download handler for image | |
output$downloadImage <- downloadHandler( | |
filename = "casaos.png", | |
content = function(file) { | |
file.copy("www/casaos.png", file) | |
}, | |
contentType = "image/png" | |
) | |
# Download handler for text file | |
output$downloadText <- downloadHandler( | |
filename = "shiny-test.txt", | |
content = function(file) { | |
file.copy("www/shiny-test.txt", file) | |
}, | |
contentType = "text/plain" | |
) | |
} | |
# Run the app | |
shinyApp(ui = ui, server = server) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
# UI | |
ui <- fluidPage( | |
titlePanel("Remote Image Download Example"), | |
mainPanel( | |
h3("Download Remote Image File"), | |
# Method 1: Download remote image | |
div( | |
"Click here to download the remote image: ", | |
downloadButton("downloadRemoteImage", | |
label = "casaos.png", | |
class = "btn-link", | |
style = "padding: 0; border: none; background: none; color: #337ab7; text-decoration: underline;") | |
), | |
br(), br(), | |
h4("Image Preview:"), | |
# Method 1: Display remote image directly in UI (simplest) | |
h5("Method 1 - Direct HTML img tag:"), | |
tags$img(src = "https://raw.githubusercontent.com/bman2408/casaos-icons/refs/heads/main/images/casaos.png", | |
width = "300px", alt = "CasaOS Remote Image"), | |
br(), br(), | |
# Method 2: Display remote image via renderUI (better for remote images) | |
h5("Method 2 - Using renderUI:"), | |
uiOutput("remoteImagePreview") | |
) | |
) | |
# Server | |
server <- function(input, output, session) { | |
# Remote image URL | |
remote_url <- "https://raw.githubusercontent.com/bman2408/casaos-icons/refs/heads/main/images/casaos.png" | |
# Method 2: Display remote image using renderUI (works better for remote URLs) | |
output$remoteImagePreview <- renderUI({ | |
tags$img(src = remote_url, | |
width = "300px", | |
alt = "CasaOS Remote Image", | |
style = "border: 1px solid #ccc;") | |
}) | |
# Download handler for remote image | |
output$downloadRemoteImage <- downloadHandler( | |
filename = "casaos.png", | |
content = function(file) { | |
# Download the remote file | |
download.file(remote_url, file, mode = "wb") | |
}, | |
contentType = "image/png" | |
) | |
} | |
# Run the app | |
shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment