-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathdependencies.R
167 lines (157 loc) · 3.92 KB
/
dependencies.R
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#' Dependencies for React
#'
#' Add JavaScript 'React' dependency. For this to work in RStudio Viewer, also include
#' \code{\link{html_dependency_corejs}}.
#'
#' @param offline \code{logical} to use local file dependencies. If \code{FALSE},
#' then the dependencies use the Facebook cdn as its \code{src}.
#' To use with \code{JSX} see \code{\link{babel_transform}}.
#'
#' @return \code{\link[htmltools]{htmlDependency}}
#' @importFrom htmltools htmlDependency
#' @export
#'
#' @examples
#' library(reactR)
#' library(htmltools)
#'
#' tagList(
#' tags$script(
#' "
#' ReactDOM.render(
#' React.createElement(
#' 'h1',
#' null,
#' 'Powered by React'
#' ),
#' document.body
#' )
#' "
#' ),
#' #add core-js first to work in RStudio Viewer
#' html_dependency_corejs(),
#' html_dependency_react() #offline=FALSE for CDN
#' )
html_dependency_react <- function(offline=TRUE){
hd <- htmltools::htmlDependency(
name = "react",
version = react_version(),
src = system.file("www/react",package="reactR"),
script = c("react.min.js", "react-dom.min.js")
)
if(!offline) {
hd$src <- list(href="//unpkg.com")
hd$script <- c(
"react/umd/react.production.min.js",
"react-dom/umd/react-dom.production.min.js"
)
}
hd
}
#' Shim Dependency for React in RStudio Viewer
#'
#' Add this first for 'React' to work in RStudio Viewer.
#'
#' @return \code{\link[htmltools]{htmlDependency}}
#' @importFrom htmltools htmlDependency
#' @export
html_dependency_corejs <- function() {
# shim/polyfill for ES5 and ES6 so react will show up in RStudio Viewer
# https://cdn.jsdelivr.net/npm/core-js-bundle@3.40.0/minified.min.js
htmltools::htmlDependency(
name = "core-js",
version = "3.40.0",
src = c(file=system.file("www/core-js/", package="reactR")),
script = "shim.min.js"
)
}
#' Adds window.reactR.exposeComponents and window.reactR.hydrate
#'
#' @return \code{\link[htmltools]{htmlDependency}}
#' @importFrom htmltools htmlDependency
#' @export
html_dependency_reacttools <- function(){
htmltools::htmlDependency(
name = "reactwidget",
src = "www/react-tools",
version = "2.0.0",
package = "reactR",
script = c("react-tools.js")
)
}
#' Dependencies for 'mobx'
#'
#' Add JavaScript 'mobx' and 'mobx-react' dependency. When using with 'react', the order
#' of the dependencies is important, so please add \code{html_dependency_react()} before
#' \code{html_dependency_mobx()}.
#'
#' @param react \code{logical} to add react 'mobx' dependencies.
#'
#' @return \code{\link[htmltools]{htmlDependency}}
#' @importFrom htmltools htmlDependency
#' @export
#'
#' @examples
#' if(interactive()) {
#'
#' library(htmltools)
#' library(reactR)
#'
#' browsable(
#' tagList(
#' html_dependency_mobx(react = FALSE),
#' div(id="test"),
#' tags$script(HTML(
#' "
#' var obs = mobx.observable({val: null})
#' mobx.autorun(function() {
#' document.querySelector('#test').innerText = obs.val
#' })
#' setInterval(
#' function() {obs.val++},
#' 1000
#' )
#' "
#' ))
#' )
#' )
#' }
#'
#' \dontrun{
#' # use with react
#' library(htmltools)
#' library(reactR)
#'
#' browsable(
#' tagList(
#' html_dependency_react(),
#' html_dependency_mobx(),
#' div(id="test"),
#' tags$script(HTML(babel_transform(
#' "
#' var obs = mobx.observable({val: null})
#' var App = mobxReact.observer((props) => <div>{props.obs.val}</div>)
#'
#' ReactDOM.render(<App obs = {obs}/>, document.querySelector('#test'))
#'
#' setInterval(
#' function() {obs.val++},
#' 1000
#' )
#' "
#' )))
#' )
#' )
#' }
html_dependency_mobx <- function(react = TRUE){
hd <- htmltools::htmlDependency(
name = "mobx",
version = "4.11.0",
src = system.file("www/mobx",package="reactR"),
script = c("mobx.umd.min.js")
)
if(react) {
hd$script <- c(hd$script,"mobx-react-lite.js", "mobx-react.umd.js")
}
hd
}