Faster loading of R Markdown html document

Dear Posit Community members,

I am creating big html documents using R Markdown. These documents contain 200 till 300 charts generated using ggplotly. The creation/knitting of these files works well. Though, these files can be 40-50 MB in size and are very slow to load in a web browser. It can take multiple minutes to open the html document in a web browser.

I tried speeding up the loading of these html documents by using the lazyrmd package, but this didn't work. Also, the lazyrmd package seems discontinued.

Are there alternative ways to speed up the loading of big html documents with many ggplotly charts that were generated using R Markdown?

Thank you!

Are you creating a self-contained document ? If so, maybe you shouldn't so that files are loaded from disk and not embed in the HTML file which will be very big

Also you should consider maybe using a website to show your different graphics into several pages. This means that the plot will load only when a page is viewed.

You can also consider lazy loading of your image

Overall, this is a WEB problem, and not specific to R Markdown.

Maybe reducing the size of your image can be useful - if you plan to have only web viewer and not print version, often DPI can be reduced. Also you could use servive like tinypng and tools like optipng to compress your files with or without much loss in quality.

From R see

  • xfun::optipng()
  • xfun::tinify()
  • webshot2::shrink()

If you work only with interactive graphics (like plotly), then you should really consider splitting the webpage in several pages - so that each plots load when needed. Maybe iframe can also be lazy loaded, and you could have each plot in a file, and included using an iframe with lazy load.

Hope this helps give hint on what you could do

Thank you for the suggestions!

Yes, I am creating one self-contained document. The reason for this is that having one self-contained document allows to be sent by e-mail or be saved on a network disk with a guarantee that it can still be opened by anybody on any network after multiple years (even if the Posit Connect server won't be available to that person (anymore)). Therefore, creating a web site with multiple pages in this case wouldn't be an optimal solution.

You can also consider lazy loading of your image

Do you know how I could implement such lazy loading of my image in Markdown/Quarto?