Edit: The issue seems to be with saving .rds files within a drake pipeline, and unrelated to ggplotly. Apologies.
Original post (where I mistook this for a plotly issue):
Hi there,
I have a project where I've been using plotly::ggplotly()
to turn ggplots into interactive graphics for online use in Shiny apps. However, it soon became evident that this resulted in some outrageously large files and objects, resulting in both an unnecessarily slow and unnecessarily large shiny app.
As we have dozens and dozens of graphs that we display in the Shiny app, a plot object is only read from an .rds file when needed, on the fly. This makes the ggplotly objects pretty much unusable as a simple barchart that is 20KB as an .rds file created using plotly, can be 250MB as an .rds file created using ggplot2 and then ggplotly. Below is an example of this behaviour with a heatmap. In short the average plotly-created file/object is 1/1000 the size of the corresponding ggplotly-created file/object.
Edit: I now realise this also happens for regular plotly
objects. The two examples below differ in the sense that the smaller is not created within a drake
pipeline, while the larger is created within a drake
pipeline.
library(readr)
library(lobstr)
library(fs)
## plotly .rds file size
fs::file_size("app_cache/atvl_aldur_fjoldi_plot.rds")
# 61.1K
## ggplotly .rds file size
fs::file_size("app_cache/atvl_aldur_tala_plot.rds")
# 212M
my_plotly_heatmap <- read_rds("app_cache/atvl_aldur_fjoldi_plot.rds")
my_plotly_heatmap
my_ggplotly_heatmap <- read_rds("app_cache/atvl_aldur_tala_plot.rds")
my_ggplotly_heatmap
lobstr::obj_size(my_plotly_heatmap)
# 125,840 B
lobstr::obj_size(my_ggplotly_heatmap)
# 145,767,184 B
I'd love to be able to use the ggplotly
approach as a lot of these graphics have already been prepared as ggplots for a report. So my question is whether there are some obvious things that I can access in the ggplotly object to cut down the size? (Font information comes to mind, are there raster graphics being stored but not printed? What explains these massive objects?). Are there any guides on this or how-to blogs? I can't seem to find any.
Many thanks in advance,
Hlynur