1
- # ' The htmlwidget
2
- # '
3
- # ' TODO
4
- # '
1
+ # ' The internal function that creates the htmlwidget.
2
+ # ' @param esm The ES Module as a string.
3
+ # ' @param values The values that will be used for the initial Model state.
4
+ # ' @param ns_id Namespace ID, only used when in the Shiny module mode. Optional.
5
5
# ' @param width The width of the widget as a number or CSS string. Optional.
6
6
# ' @param height The height of the widget as a number or CSS string. Optional.
7
+ # ' @param port The port of the WebSocket server, when in dynamic mode. Optional.
8
+ # ' @param host The host of the WebSocket server, when in dynamic mode. Optional.
7
9
# ' @param element_id An element ID. Optional.
8
- # ' @return The htmlwidget .
10
+ # ' @return The result of htmlwidgets::createWidget .
9
11
# '
10
- # ' @export
12
+ # ' @keywords internal
11
13
the_anyhtmlwidget <- function (esm , values = NULL , ns_id = NULL , width = NULL , height = NULL , port = NULL , host = NULL , element_id = NULL ) {
12
-
13
- # forward widget options to javascript
14
14
params = list (
15
15
esm = esm ,
16
16
values = values ,
@@ -19,9 +19,8 @@ the_anyhtmlwidget <- function(esm, values = NULL, ns_id = NULL, width = NULL, he
19
19
host = host
20
20
)
21
21
22
- # create widget
23
22
htmlwidgets :: createWidget(
24
- name = ' anyhtmlwidget' ,
23
+ ' anyhtmlwidget' ,
25
24
params ,
26
25
width = width ,
27
26
height = height ,
@@ -51,15 +50,15 @@ the_anyhtmlwidget <- function(esm, values = NULL, ns_id = NULL, width = NULL, he
51
50
# ' is useful if you want to save an expression in a variable.
52
51
# ' @return The Shiny UI element.
53
52
# '
54
- # ' @rdname anyhtmlwidget-shiny
55
- # ' @export
53
+ # ' @keywords internal
56
54
anyhtmlwidget_output <- function (output_id , width = ' 100%' , height = ' 400px' ){
57
55
htmlwidgets :: shinyWidgetOutput(output_id , ' anyhtmlwidget' , width , height , package = ' anyhtmlwidget' )
58
56
}
59
57
60
58
# ' @name anyhtmlwidget-shiny
61
59
# ' @return The Shiny server output.
62
- # ' @export
60
+ # '
61
+ # ' @keywords internal
63
62
render_anyhtmlwidget <- function (expr , env = parent.frame(), quoted = FALSE ) {
64
63
if (! quoted ) { expr <- substitute(expr ) } # force quoted
65
64
htmlwidgets :: shinyRenderWidget(expr , anyhtmlwidget_output , env , quoted = TRUE )
@@ -86,7 +85,7 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
86
85
87
86
),
88
87
public = list (
89
- initialize = function (.esm = NA , .mode = NA , .width = NA , .height = NA , .commands = NA , ... ) {
88
+ initialize = function (.esm , .mode , .width = NA , .height = NA , .commands = NA , ... ) {
90
89
private $ esm <- .esm
91
90
private $ values <- list (... )
92
91
@@ -103,9 +102,6 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
103
102
private $ server_host <- " 0.0.0.0"
104
103
private $ server_port <- httpuv :: randomPort(min = 8000 , max = 9000 , n = 1000 )
105
104
106
- if (is.na(.mode )) {
107
- .mode <- " static"
108
- }
109
105
if (! .mode %in% c(" static" , " gadget" , " shiny" , " dynamic" )) {
110
106
stop(" Invalid widget mode." )
111
107
}
@@ -228,16 +224,14 @@ invoke_dynamic <- function(w) {
228
224
}
229
225
230
226
invoke_gadget <- function (w ) {
231
- require(shiny )
232
-
233
- ui <- tagList(
227
+ ui <- shiny :: tagList(
234
228
anyhtmlwidget_output(output_id = " my_widget" , width = ' 100%' , height = ' 100%' )
235
229
)
236
230
237
231
server <- function (input , output , session ) {
238
- increment <- reactiveVal(0 )
232
+ increment <- shiny :: reactiveVal(0 )
239
233
240
- observeEvent(input $ anyhtmlwidget_on_save_changes , {
234
+ shiny :: observeEvent(input $ anyhtmlwidget_on_save_changes , {
241
235
# update values on w here
242
236
for (key in names(input $ anyhtmlwidget_on_save_changes )) {
243
237
w $ set_value(key , input $ anyhtmlwidget_on_save_changes [[key ]], emit_change = FALSE )
@@ -264,25 +258,25 @@ invoke_gadget <- function(w) {
264
258
})
265
259
}
266
260
267
- runGadget(ui , server )
261
+ shiny :: runGadget(ui , server )
268
262
}
269
263
270
264
# Shiny module UI
271
265
widgetUI <- function (id , width = ' 100%' , height = ' 400px' ) {
272
- ns <- NS(id )
266
+ ns <- shiny :: NS(id )
273
267
anyhtmlwidget_output(output_id = ns(" widget" ), width = width , height = height )
274
268
}
275
269
276
270
# Shiny module server
277
271
widgetServer <- function (id , w ) {
278
- ns <- NS(id )
279
- moduleServer(
272
+ ns <- shiny :: NS(id )
273
+ shiny :: moduleServer(
280
274
id ,
281
275
function (input , output , session ) {
282
276
initial_values <- w $ get_values()
283
- rv <- do.call(reactiveValues , initial_values )
277
+ rv <- do.call(shiny :: reactiveValues , initial_values )
284
278
285
- observeEvent(input $ anyhtmlwidget_on_save_changes , {
279
+ shiny :: observeEvent(input $ anyhtmlwidget_on_save_changes , {
286
280
# update values on w here
287
281
for (key in names(input $ anyhtmlwidget_on_save_changes )) {
288
282
rv [[key ]] <- input $ anyhtmlwidget_on_save_changes [[key ]]
@@ -291,7 +285,7 @@ widgetServer <- function(id, w) {
291
285
})
292
286
293
287
for (key in names(initial_values )) {
294
- observeEvent(rv [[key ]], {
288
+ shiny :: observeEvent(rv [[key ]], {
295
289
session $ sendCustomMessage(ns(" anyhtmlwidget_on_change" ), list (key = key , value = rv [[key ]]))
296
290
})
297
291
}
0 commit comments