Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

onStaticRenderComplete

Execute JavaScript code after static render


Description

Convenience function for wrapping a JavaScript code string with a <script> tag and the boilerplate necessary to delay the execution of the code until after the next time htmlwidgets completes rendering any widgets that are in the page. This mechanism is designed for running code to customize widget instances, which can't be done at page load time since the widget instances will not have been created yet.

Usage

onStaticRenderComplete(jsCode)

Arguments

jsCode

A character vector containing JavaScript code. No R error will be raised if the code is invalid, not even on JavaScript syntax errors. However, the web browser will throw errors at runtime.

Details

Each call to onStaticRenderComplete will result in at most one invocation of the given code. In some edge cases in Shiny, it's possible for static rendering to happen more than once (e.g. a renderUI that contains static HTML widgets). onStaticRenderComplete calls only schedule execution for the next static render operation.

The pure JavaScript equivalent of onStaticRenderComplete is HTMLWidgets.addPostRenderHandler(callback), where callback is a JavaScript function that takes no arguments.

Value

An htmltools tags$script object.

Examples

## Not run: 
library(leaflet)
library(htmltools)
library(htmlwidgets)

page <- tagList(
  leaflet() %>% addTiles(),
  onStaticRenderComplete(
    "HTMLWidgets.find('.leaflet').setZoom(4);"
  )
)
print(page, browse = TRUE)

## End(Not run)

htmlwidgets

HTML Widgets for R

v1.5.3
MIT + file LICENSE
Authors
Ramnath Vaidyanathan [aut, cph], Yihui Xie [aut], JJ Allaire [aut], Joe Cheng [aut], Carson Sievert [aut, cre] (<https://orcid.org/0000-0002-4958-2844>), Kenton Russell [aut, cph], Ellis Hughes [ctb], RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.