You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

shinyapps.io无法显示图片求助:本地正常部署异常

Hey there! Let's dig into why your Shiny app's images work locally but vanish once deployed to shinyapps.io. This is one of the most frequent gotchas with Shiny deployments, and it almost always ties back to file paths, directory structure, or deployment packaging. Here's a breakdown of the most likely issues and how to fix them:

1. Absolute Paths That Don't Exist on the Server

This is the #1 reason images break on shinyapps.io. When you use a full local path like C:/Users/YourName/Documents/my_image.png, your local machine knows exactly where to look—but shinyapps.io has no access to your personal hard drive.

Fix: Use relative paths and leverage Shiny's default www/ folder. Structure your app like this:

my_shiny_app/
├── app.R
└── www/
    └── my_image.png

Then in your code, reference the image simply like:

# For UI elements
img(src = "my_image.png")

# For renderImage() in server
output$my_image <- renderImage({
  list(src = file.path("www", "my_image.png"),
       contentType = "image/png")
}, deleteFile = FALSE)

Shiny automatically looks in the www/ folder for static assets, so you don't need to include the full path.

2. Case Sensitivity Mismatch

Windows (most local dev environments) ignores case in file names, but shinyapps.io runs on Linux, which is case-sensitive. If your code says img(src = "MyImage.png") but the actual file is named myimage.png, it'll work locally but fail on the server.

Fix: Double-check that every file name and path in your code matches the actual file's case exactly.

3. Forgetting to Include Images in the Deployment Package

If your images are stored outside your app's root directory, or you didn't select the www/ folder when deploying, shinyapps.io won't have access to them.

Fix:

  • Move all images into a subfolder within your app directory (preferably www/ for simplicity)
  • When deploying via RStudio, make sure the deployment dialog lists all your image files and folders
  • If using rsconnect::deployApp(), ensure the appDir points to your app's root directory, and all necessary files are contained within it

4. Accidental Use of Local File Protocols

Avoid using file:/// in your image paths (e.g., img(src = "file:///C:/...")). This protocol only works for files on your local machine and won't resolve on the shinyapps.io server.

Fix: Stick to relative paths as outlined in point 1.

Quick Troubleshooting Hack

Add these lines to your server function to debug what files the server can see:

server <- function(input, output, session) {
  # Print working directory and available files
  print(paste("Working directory:", getwd()))
  print("Files in app root:")
  print(list.files())
  print("Files in www folder:")
  print(list.files("www"))
  
  # Rest of your server code...
}

After deploying, check the app's logs on shinyapps.io (under "Logs" in your app dashboard) to see if your images are listed. If they're not, you know the deployment package is missing them.


内容的提问来源于stack exchange,提问作者Kinsuk Ghatak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:49:20