【问题标题】:image_composite (Magick) return error in Shiny Flexdashboard闪亮的 Flexdashboard 中的 image_composite (Magick) 返回错误
【发布时间】:2022-01-21 10:30:28
【问题描述】:

我需要在 Shiny Flexdashboard 中构建合成图片,以将 Ggplot2 图表放置在背景图像上。

我使用以下代码:

```{r}

renderImage({

bkgDom <- image_read('background.png')
myChart <- ggplot(data=individualObs, aes(individualObs$SA)) + geom_histogram()
fig <- image_graph(width = 600, height = 550)
myChart
dev.off()
image_composite(bkgDom, fig, offset = "+1030+50", operator = "multiply") }, deleteFile = TRUE)

renderImage() 中的代码在 RStudio 中运行良好,但在 Shiny Flexdashboard 代码中使用时返回错误“'externalptr' 类型的对象不是子集”。

关于如何解决这个问题的任何想法?

提前感谢您的支持!

【问题讨论】:

    标签: r shiny flexdashboard


    【解决方案1】:

    无论出于何种原因,临时文件存在问题,因此我使用了 image_write() 函数来保存 image_composite() 的结果,然后使用 list() 函数检索文件。

    除此之外,为了避免创建空文件,必须通过 print() 函数公开 Ggplot2 图表。

    这是在 Shiny Flexidashboard 中工作的代码:

    renderImage({
    
    #Set the first image-magic pointer
    bkgDom <- image_read('background.png')
    
    myChart <- ggplot(data=individualObs, aes(individualObs$SA)) + geom_histogram()
    
    #Set the space to load the Ggplot chart in
    fig <- image_graph(width = 400, height = 400)
    
    #Print the Ggplot chart into the fig pointer
    print(myChart)
    
    #Create the composite image into a image-magick pointer and adjust the position
    pic <- image_composite(bkgDom, fig, offset = "+580-25", operator = "multiply")
    
    #Save the pointer into a png file
    image_write(pic,"test.png")
    
    #Read and show the png file
    list(src = "test.png", contentType = 'image/png', width="100%")
    
    }, deleteFile = TRUE)
    

    【讨论】:

    • 很高兴它可以工作:)
    【解决方案2】:

    查看the docs,它说表达式应该返回一个列表。由于您编写的内容返回了“externalptr”类型的对象(或者我猜image_composite() 文档没有告诉我输出的值),我认为错误是因为闪亮找不到src。此外,您的合成图像可能还需要临时保存。

    没有足够的代码来重现,但从文档中这应该可以解决:

    renderImage({
      
      bkgDom <- image_read('background.png')
      myChart <- ggplot(data=individualObs, aes(individualObs$SA)) + geom_histogram()
      fig <- image_graph(width = 600, height = 550)
      myChart
      dev.off()
    
      # A temp file to save the output.
      outfile <- tempfile(fileext='.png')
      png(outfile)
      image_composite(bkgDom, fig, offset = "+1030+50", operator = "multiply")
      dev.off() 
      
      # Return a list containing the filename
      list(src = outfile,
           alt = "This is alternate text")
      }, deleteFile = TRUE)
    

    我单独保存了合成,但这可能与您修改的其他图像同步完成?

    如果这不起作用,您能否提供更多代码以使其可重现?

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 2017-03-29
    • 2019-09-11
    • 2018-01-25
    • 2019-12-21
    • 1970-01-01
    • 2016-07-01
    相关资源
    最近更新 更多