以R闪亮显示本地存储的图像 [英] Display locally-stored image in R Shiny
本文介绍了以R闪亮显示本地存储的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我花了相当多的时间试图解决这个问题。 当然,在这里分享我的问题之前,我做了一些功课。
我特别咨询过,但没有成功:
- local image in shiny app without img(src())?
- Shiny can not display Image locally
- adding local image with html to a Shiny app
- R Shiny img() on UI side does not render the image
- Display images from web in shiny R
- Image failing to display in R shiny
- Embedding Image in Shiny App
- How to place an image in an R Shiny title
所以我确实在RStudio项目文件的根目录下创建了一个‘www’文件夹,我在其中放置了一些图片。
这些图片在标题面板中使用,但也由应用程序调用的主htmlwidget使用。
将这些图片存储在本地对我来说至关重要,因为应用程序可能在没有任何Internet访问权限的安全环境中运行。
我尝试了这些图片的相对路径和绝对路径:未显示图片。
然后我注意到了一些不一致的地方:只有当我在RStudio中通过常规命令运行应用程序时,才会遇到这个问题。 另一方面,当我通过专用命令运行应用程序(在RStudio的右上角,绿色箭头)运行应用程序时,我不再有这个问题,图片显示得很好(但输入数据会被以某种方式检查,在应用程序启动之前需要很长时间)。最初我以为显示本地图像比存储在Internet上的远程图像容易得多,但似乎恰恰相反。
因此我的问题如下:
- 您知道为什么我们可以观察到这种差异(这对我来说是不一致的)吗?
- 您知道我如何才能继续使用常规执行命令(&Quot;运行选定行(&q;))?
致以最诚挚的问候
奥利维尔
推荐答案
对我来说,在RStudio中通过Run Selected Line(s)
运行应用程序时也可以使用以下方法:
library(shiny)
# create some local images
if(!dir.exists("myimages")){
dir.create("myimages")
}
myPlotPaths <- paste0("myimages/myplot", seq_len(3), ".png")
for (myPlot in myPlotPaths) {
png(file = myPlot, bg = "transparent")
plot(runif(10))
dev.off()
}
myImgResources <- paste0("imgResources/myplot", seq_len(3), ".png")
# Add directory of static resources to Shiny's web server
addResourcePath(prefix = "imgResources", directoryPath = "myimages")
ui <- fluidPage(
tags$img(src = myImgResources[1], width = "400px", height = "400px"),
tags$img(src = myImgResources[2], width = "400px", height = "400px"),
tags$img(src = myImgResources[3], width = "400px", height = "400px")
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
这篇关于以R闪亮显示本地存储的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文