如何在Shiny中加载外部图片

3

在我的当前项目中,我正在尝试使用 R 将图像加载到 shiny dashboard。以下是代码片段:

dashboardBody(
          hr(),
          fluidRow(
            column(6,align="center",imageOutput("ginger"))
          )
        )
      )

      server <- function(input, output) { 
        output$ginger <- renderImage({
          return(list(
            src = "images/ginger.jpg",
            contentType = "image/jpeg",
            width = 300,
            height = 200,
            alt = "Face"
          ))
        }, deleteFile = FALSE)

基本上,它只是在闪亮的仪表板上显示图像。 这里的图像存储在本地计算机中。现在,我想从Google Drive或Web加载图像。我正在尝试从我的Google驱动器加载图像,URL为https://drive.google.com/file/d/0By6SOdXnt-LFaDhpMlg3b3FiTEU/view

我无法弄清楚如何在Shiny中从Google Drive或Web加载图像,以及如何添加图像标题?我错过了什么吗?


可能是一个愚蠢的问题,但你的图片是否在www文件夹中? - Daniel
@Daniel- 图像可能在谷歌云端硬盘中,也可能在网络上。 - Saurabh Chauhan
@saursabh13 那么你的 'src = "images/ginger.jpg"' 只是一个虚拟的东西?好的,我误解了你的问题,抱歉 : ( - Daniel
@Daniel- 那种方式是可以的。如果图像位于本地机器的图像文件夹中,则会将图像显示到仪表板上。但是我想从网络加载图像,在这种情况下,图像不在本地机器上。 - Saurabh Chauhan
1个回答

6

这个答案很有启示性。以下是一个简单的shiny应用程序,通过外部图像调用来显示你在Google Drive账户上提到的图像。

library(shiny)

# Define UI with external image call
ui <- fluidPage(
  titlePanel("Look at the image below"),

  sidebarLayout(sidebarPanel(),

                mainPanel(htmlOutput("picture"))))

# Define server with information needed to hotlink image
server <- function(input, output) {
  output$picture <-
    renderText({
      c(
        '<img src="',
        "http://drive.google.com/uc?export=view&id=0By6SOdXnt-LFaDhpMlg3b3FiTEU",
        '">'
      )
    })
}

shinyApp(ui = ui, server = server)

谢谢@Andrew。但是如何为图像添加标题呢?在这里,您已将标题添加为titlePanle,但如果我们有多个图像,该如何添加标题? - Saurabh Chauhan
它对我不起作用。我尝试通过提供驱动器和Dropbox图像URL来解决问题,但是图像无法呈现。 - Abhinandan Satpute
@AbhinandanSatpute- 你有尝试使用谷歌上的图片吗?因为我之前使用drive和Dropbox时也遇到了同样的问题。 - Saurabh Chauhan
1
对我来说,在本地环境中这个无法运行,但一旦部署应用程序就可以工作。 - TTS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接