@@ -35,10 +35,7 @@ the_anyhtmlwidget <- function(esm, values = NULL, ns_id = NULL, width = NULL, he
35
35
)
36
36
}
37
37
38
- # ' Shiny bindings for anyhtmlwidget
39
- # '
40
- # ' Output and render functions for using anyhtmlwidget within Shiny
41
- # ' applications and interactive Rmd documents.
38
+ # ' Internal Shiny UI binding for anyhtmlwidget.
42
39
# '
43
40
# ' @param output_id output variable to read from
44
41
# ' @param width,height Must be a valid CSS unit (like \code{'100\%'},
@@ -55,15 +52,21 @@ anyhtmlwidget_output <- function(output_id, width = '100%', height = '400px'){
55
52
htmlwidgets :: shinyWidgetOutput(output_id , ' anyhtmlwidget' , width , height , package = ' anyhtmlwidget' )
56
53
}
57
54
58
- # ' @name anyhtmlwidget-shiny
59
- # ' @return The Shiny server output.
55
+ # ' Internal Shiny server binding for anyhtmlwidget.
60
56
# '
61
57
# ' @keywords internal
62
58
render_anyhtmlwidget <- function (expr , env = parent.frame(), quoted = FALSE ) {
63
59
if (! quoted ) { expr <- substitute(expr ) } # force quoted
64
60
htmlwidgets :: shinyRenderWidget(expr , anyhtmlwidget_output , env , quoted = TRUE )
65
61
}
66
62
63
+ # ' AnyHtmlWidget
64
+ # ' @title AnyHtmlWidget Class
65
+ # ' @docType class
66
+ # ' @description
67
+ # ' Class representing a widget.
68
+ # '
69
+ # ' @rdname AnyHtmlWidget
67
70
# ' @export
68
71
AnyHtmlWidget <- R6 :: R6Class(" AnyHtmlWidget" ,
69
72
lock_objects = FALSE ,
@@ -81,10 +84,16 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
81
84
.width = NULL ,
82
85
.height = NULL
83
86
),
84
- active = list (
85
-
86
- ),
87
+ active = list (),
87
88
public = list (
89
+ # ' @description
90
+ # ' Create a new widget instance.
91
+ # ' @param .esm The EcmaScript module as a string.
92
+ # ' @param .mode The widget mode.
93
+ # ' @param .width The widget width. Optional.
94
+ # ' @param .height The widget height. Optional.
95
+ # ' @param .commands TODO
96
+ # ' @param ... All other named arguments will be used to create active bindings on the instance.
88
97
initialize = function (.esm , .mode , .width = NA , .height = NA , .commands = NA , ... ) {
89
98
private $ esm <- .esm
90
99
private $ values <- list (... )
@@ -126,56 +135,100 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
126
135
}
127
136
self $ `.__enclos_env__` $ `.__active__` <- active_env
128
137
},
138
+ # ' @description
139
+ # ' Set a value.
140
+ # ' @param key The key of the value to set.
141
+ # ' @param val The new value.
142
+ # ' @param emit_change Should the on_change handler be called?
129
143
set_value = function (key , val , emit_change = TRUE ) {
130
144
private $ values [[key ]] <- val
131
145
if (emit_change && ! is.null(private $ change_handler )) {
132
146
# Should this only call the callback if the current value is different than the new value?
133
147
private $ change_handler(key , val )
134
148
}
135
149
},
150
+ # ' @description
151
+ # ' Register a change handler to call if emit_change is TRUE in set_value.
152
+ # ' @param callback A callback function to register.
136
153
on_change = function (callback ) {
137
154
private $ change_handler <- callback
138
155
},
156
+ # ' @description
157
+ # ' Get a particular value.
158
+ # ' @param key The key of the value to get.
159
+ # ' @returns The value.
139
160
get_value = function (key ) {
140
161
return (private $ values [[key ]])
141
162
},
163
+ # ' @description
164
+ # ' Get the ESM string.
165
+ # ' @returns The ESM string.
142
166
get_esm = function () {
143
167
return (private $ esm )
144
168
},
169
+ # ' @description
170
+ # ' Get all widget values
171
+ # ' @returns List of values.
145
172
get_values = function () {
146
173
return (private $ values )
147
174
},
175
+ # ' @description
176
+ # ' Get the widget width.
177
+ # ' @returns The width.
148
178
get_width = function () {
149
179
return (private $ .width )
150
180
},
181
+ # ' @description
182
+ # ' Get the widget height.
183
+ # ' @returns The height.
151
184
get_height = function () {
152
185
return (private $ .height )
153
186
},
187
+ # ' @description
188
+ # ' Set all values. TODO: is this ever used?
189
+ # ' @param new_values A list of new values.
154
190
set_values = function (new_values ) {
155
191
private $ values <- new_values
156
192
},
193
+ # ' @description
194
+ # ' Set the widget mode.
195
+ # ' @param mode The new widget mode.
157
196
set_mode = function (mode ) {
158
197
if (! mode %in% c(" static" , " gadget" , " shiny" , " dynamic" )) {
159
198
stop(" Invalid widget mode." )
160
199
}
161
200
private $ mode <- mode
162
201
},
202
+ # ' @description
203
+ # ' Start the server, if not running.
163
204
start_server = function () {
164
205
if (is.null(private $ server )) {
165
206
private $ server <- start_server(self , host = private $ server_host , port = private $ server_port )
166
207
}
167
208
},
209
+ # ' @description
210
+ # ' Stop the server, if running.
168
211
stop_server = function () {
169
212
if (! is.null(private $ server )) {
170
213
private $ server $ stop()
171
214
}
172
215
},
216
+ # ' @description
217
+ # ' Get the server hostname.
218
+ # ' @return The hostname as a string.
173
219
get_host = function () {
174
220
return (private $ server_host )
175
221
},
222
+ # ' @description
223
+ # ' Get the server port.
224
+ # ' @returns The port number.
176
225
get_port = function () {
177
226
return (private $ server_port )
178
227
},
228
+ # ' @description
229
+ # ' Custom print function for the R6 class.
230
+ # ' If mode is "shiny", falls back to original R6 print behavior.
231
+ # ' Otherwise, renders the widget.
179
232
print = function () {
180
233
if (private $ mode == " shiny" ) {
181
234
# If Shiny mode, we just want to use the original R6 print behavior.
@@ -186,6 +239,8 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
186
239
self $ render()
187
240
}
188
241
},
242
+ # ' @description
243
+ # ' Render the widget.
189
244
render = function () {
190
245
if (private $ mode == " static" ) {
191
246
invoke_static(self )
@@ -200,6 +255,7 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
200
255
)
201
256
)
202
257
258
+ # ' @keywords internal
203
259
invoke_static <- function (w ) {
204
260
w <- the_anyhtmlwidget(
205
261
esm = w $ get_esm(),
@@ -210,6 +266,7 @@ invoke_static <- function(w) {
210
266
print(w )
211
267
}
212
268
269
+ # ' @keywords internal
213
270
invoke_dynamic <- function (w ) {
214
271
w $ start_server()
215
272
w <- the_anyhtmlwidget(
@@ -223,6 +280,7 @@ invoke_dynamic <- function(w) {
223
280
print(w )
224
281
}
225
282
283
+ # ' @keywords internal
226
284
invoke_gadget <- function (w ) {
227
285
ui <- shiny :: tagList(
228
286
anyhtmlwidget_output(output_id = " my_widget" , width = ' 100%' , height = ' 100%' )
@@ -261,13 +319,25 @@ invoke_gadget <- function(w) {
261
319
shiny :: runGadget(ui , server )
262
320
}
263
321
264
- # Shiny module UI
322
+ # ' Shiny module UI for anyhtmlwidgets.
323
+ # '
324
+ # ' @param id The output ID.
325
+ # ' @param width The widget width. Optional.
326
+ # ' @param height The widget height. Optional.
327
+ # '
328
+ # ' @export
265
329
widgetUI <- function (id , width = ' 100%' , height = ' 400px' ) {
266
330
ns <- shiny :: NS(id )
267
331
anyhtmlwidget_output(output_id = ns(" widget" ), width = width , height = height )
268
332
}
269
333
270
- # Shiny module server
334
+ # ' Shiny module server for anyhtmlwidgets.
335
+ # '
336
+ # ' @param id The matching output ID used in the Shiny UI.
337
+ # ' @param w The widget instance.
338
+ # ' @returns reactiveValues corresponding to the widget's active bindings.
339
+ # '
340
+ # ' @export
271
341
widgetServer <- function (id , w ) {
272
342
ns <- shiny :: NS(id )
273
343
shiny :: moduleServer(
0 commit comments