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

R Shiny应用本地图片无法显示问题求助

Fixing Image Not Loading in Your Shiny App

Hey there! I see the issue with your image not displaying properly—let's get this sorted out quickly.

The Root Problem

Shiny has a specific way of handling static assets like images: it looks for them in a www folder located in the same directory as your Shiny app script. Using an absolute local path (like C:/Users/asus/Desktop/App-1/rstudio.png) doesn't work because Shiny restricts access to files outside its expected resource structure, and this will also break if you ever deploy the app later.

Step-by-Step Fix

  1. Create a www folder: In the same folder where your Shiny app script is saved (looks like C:/Users/asus/Desktop/App-1/), make a new folder named www (all lowercase, no spaces).
  2. Move your image: Copy rstudio.png into this new www folder.
  3. Update your img code: Change the src parameter to just the filename of the image, since Shiny will automatically look in the www folder:
    img(src="rstudio.png", height=70, width=200)
    

Modified Full Code

Here's your updated app code with the fix applied (I also fixed a small typo in your install command!):

ui <- fluidPage(
  titlePanel("My Shiny App"),
  sidebarLayout(
    sidebarPanel(
      h4("Installation"),
      p("Shiny is available on CRAN, so you can install it on the usual way from your R console:"),
      div("install.packages('shiny')", style="color:red"),
      img(src="rstudio.png", height=70, width=200)
    ),
    mainPanel()
  )
)
#define server logic
server <- function(input, output) {
}
# Run the app ----
shinyApp(ui = ui, server = server)

Alternative (If You Must Use Absolute Paths)

If for some reason you need to keep the image outside the www folder, you can use renderImage in the server to serve it:

ui <- fluidPage(
  titlePanel("My Shiny App"),
  sidebarLayout(
    sidebarPanel(
      h4("Installation"),
      p("Shiny is available on CRAN, so you can install it on the usual way from your R console:"),
      div("install.packages('shiny')", style="color:red"),
      uiOutput("my_image")
    ),
    mainPanel()
  )
)

server <- function(input, output) {
  output$my_image <- renderUI({
    img(src = normalizePath("C:/Users/asus/Desktop/App-1/rstudio.png"), 
        height=70, width=200)
  })
}

shinyApp(ui = ui, server = server)

But the www folder method is always the cleaner, more standard approach for Shiny apps.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 13:13:14