I need to build a composite picture in a Shiny Flexdashboard to place a Ggplot2 chart on a background image.
I use the following code:
```{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)
The code inside the renderImage() works fine in RStudio but returns the error "object of type 'externalptr' is not subsettable" when used in the Shiny Flexdashboard code.
Any idea on how to solve this issue?
Thank you in advance for your kind support!
For whatever reason there is an issue with the tempfile and therefore I've used the image_write() function to save the result of image_composite() and then retrieve the file with the list() function.
Further to this and to avoid the creation of an empty file, the Ggplot2 chart has to be exposed through the print() function.
Here is the code that works within 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)