File: onRender.Rd

package info (click to toggle)
r-cran-htmlwidgets 1.6.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 516 kB
  • sloc: javascript: 597; sh: 13; makefile: 2
file content (85 lines) | stat: -rw-r--r-- 2,501 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/htmlwidgets.R
\name{onRender}
\alias{onRender}
\title{Execute custom JavaScript code after rendering}
\usage{
onRender(x, jsCode, data = NULL)
}
\arguments{
\item{x}{An HTML Widget object}

\item{jsCode}{Character vector containing JavaScript code (see Details)}

\item{data}{An additional argument to pass to the \code{jsCode} function.
This can be any R object that can be serialized to JSON. If you have
multiple objects to pass to the function, use a named list.}
}
\value{
The modified widget object
}
\description{
Use this function to supplement the widget's built-in JavaScript rendering
logic with additional custom JavaScript code, just for this specific widget
object.
}
\details{
The \code{jsCode} parameter must contain valid JavaScript code which
  when evaluated returns a function.

  The function will be invoked with three arguments: the first is the widget's
  main HTML element, and the second is the data to be rendered (the \code{x}
  parameter in \code{createWidget}). The third argument is the JavaScript
  equivalent of the R object passed into \code{onRender} as the \code{data}
  argument; this is an easy way to transfer e.g. data frames without having
  to manually do the JSON encoding.

  When the function is invoked, the \code{this} keyword will refer to the
  widget instance object.
}
\examples{
\dontrun{
library(leaflet)

# This example uses browser geolocation. RStudio users:
# this won't work in the Viewer pane; try popping it
# out into your system web browser.
leaflet() \%>\% addTiles() \%>\%
  onRender("
    function(el, x) {
      // Navigate the map to the user's location
      this.locate({setView: true});
    }
  ")


# This example shows how you can make an R data frame available
# to your JavaScript code.

meh <- "&#x1F610;";
yikes <- "&#x1F628;";

df <- data.frame(
  lng = quakes$long,
  lat = quakes$lat,
  html = ifelse(quakes$mag < 5.5, meh, yikes),
  stringsAsFactors = FALSE
)

leaflet() \%>\% addTiles() \%>\%
  fitBounds(min(df$lng), min(df$lat), max(df$lng), max(df$lat)) \%>\%
  onRender("
    function(el, x, data) {
      for (var i = 0; i < data.lng.length; i++) {
        var icon = L.divIcon({className: '', html: data.html[i]});
        L.marker([data.lat[i], data.lng[i]], {icon: icon}).addTo(this);
      }
    }
  ", data = df)
}

}
\seealso{
\code{\link{onStaticRenderComplete}}, for writing custom JavaScript
  that involves multiple widgets.
}