Detecting the start of shiny in an Rmarkdown with runtime: shiny

From Shiny - Packaging JavaScript code for Shiny :

(One caveat for runtime: shiny apps, regardless of which option you choose: your Rmd is not rendered until well after the browser page has loaded, so you can’t rely on the DOMContentLoaded event, or its jQuery equivalent, $(document).ready() .)

In an Rmarkdown with runtime: shiny I have also tried

$(document).on('shiny:connected', function(event) { alert('shiny:connected 2'); Shiny.onInputChange('shinyconnected2', 2); });

Whether putting this in tags$head(tags$script(...)) or a {js} chunk, it doesn't work.
(In a shiny app, it works fine.)

My question: how to detect the readiness of the shiny runtime in Rmarkdown?

I wonder if runtime:shiny_prerendered could be the solution here ? :thinking: One reason it exists:

The traditional way to add Shiny components to an R Markdown document is through the use of runtime: shiny . (...)
However, because it requires a full document render for each end user browser session it can perform poorly for documents that don’t render quickly.
For information, there is a dedicated page on

Personally I used it to speed up the opening of an Rmd document with shiny element. Basically, all the Rmd chunk are run once. I let you try if it changes thinks for you. The function is rmarkdown :package: BTW.

1 Like

This shiny_prerendered is terrific! Thanks.
I will try it.

I found ways to achieve my goals anyway.
(I need to provide some advice to the user if using the Chrome browser, or if the window zoom is not right.)