R Shiny应用本地图片无法显示问题求助
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
- Create a
wwwfolder: In the same folder where your Shiny app script is saved (looks likeC:/Users/asus/Desktop/App-1/), make a new folder namedwww(all lowercase, no spaces). - Move your image: Copy
rstudio.pnginto this newwwwfolder. - Update your
imgcode: Change thesrcparameter to just the filename of the image, since Shiny will automatically look in thewwwfolder: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

