@@ -74,13 +74,13 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
74
74
# TODO: prefix with dot
75
75
# since R6 requires all items in
76
76
# public, private, and active to have unique names
77
- esm = NULL ,
78
- values = NULL ,
79
- mode = NULL ,
80
- change_handler = NULL ,
81
- server = NULL ,
82
- server_host = NULL ,
83
- server_port = NULL ,
77
+ . esm = NULL ,
78
+ . values = NULL ,
79
+ . mode = NULL ,
80
+ . change_handler = NULL ,
81
+ . server = NULL ,
82
+ . server_host = NULL ,
83
+ . server_port = NULL ,
84
84
.width = NULL ,
85
85
.height = NULL
86
86
),
@@ -95,8 +95,8 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
95
95
# ' @param .commands TODO
96
96
# ' @param ... All other named arguments will be used to create active bindings on the instance.
97
97
initialize = function (.esm , .mode , .width = NA , .height = NA , .commands = NA , ... ) {
98
- private $ esm <- .esm
99
- private $ values <- list (... )
98
+ private $ . esm <- .esm
99
+ private $ . values <- list (... )
100
100
101
101
if (is.na(.width )) {
102
102
.width <- " 100%"
@@ -108,24 +108,24 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
108
108
private $ .width <- .width
109
109
private $ .height <- .height
110
110
111
- private $ server_host <- " 0.0.0.0"
112
- private $ server_port <- httpuv :: randomPort(min = 8000 , max = 9000 , n = 1000 )
111
+ private $ . server_host <- " 0.0.0.0"
112
+ private $ . server_port <- httpuv :: randomPort(min = 8000 , max = 9000 , n = 1000 )
113
113
114
114
if (! .mode %in% c(" static" , " gadget" , " shiny" , " dynamic" )) {
115
115
stop(" Invalid widget mode." )
116
116
}
117
- private $ mode <- .mode
117
+ private $ . mode <- .mode
118
118
119
119
active_env <- self $ `.__enclos_env__` $ `.__active__`
120
120
121
121
# TODO: check that values is not NA
122
- for (key in names(private $ values )) {
122
+ for (key in names(private $ . values )) {
123
123
active_binding <- function (val ) {
124
124
if (missing(val )) {
125
- return (self $ get_value(key ))
125
+ return (self $ . get_value(key ))
126
126
} else {
127
- self $ set_value(key , val )
128
- if (private $ mode == " static" ) {
127
+ self $ . set_value(key , val )
128
+ if (private $ . mode == " static" ) {
129
129
self $ print()
130
130
}
131
131
}
@@ -139,98 +139,98 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
139
139
# ' Set a value.
140
140
# ' @param key The key of the value to set.
141
141
# ' @param val The new value.
142
- # ' @param emit_change Should the on_change handler be called?
143
- set_value = function (key , val , emit_change = TRUE ) {
144
- private $ values [[key ]] <- val
145
- if (emit_change && ! is.null(private $ change_handler )) {
142
+ # ' @param emit_change Should the . on_change handler be called?
143
+ . set_value = function (key , val , emit_change = TRUE ) {
144
+ private $ . values [[key ]] <- val
145
+ if (emit_change && ! is.null(private $ . change_handler )) {
146
146
# Should this only call the callback if the current value is different than the new value?
147
- private $ change_handler(key , val )
147
+ private $ . change_handler(key , val )
148
148
}
149
149
},
150
150
# ' @description
151
- # ' Register a change handler to call if emit_change is TRUE in set_value.
151
+ # ' Register a change handler to call if emit_change is TRUE in . set_value.
152
152
# ' @param callback A callback function to register.
153
- on_change = function (callback ) {
154
- private $ change_handler <- callback
153
+ . on_change = function (callback ) {
154
+ private $ . change_handler <- callback
155
155
},
156
156
# ' @description
157
157
# ' Get a particular value.
158
158
# ' @param key The key of the value to get.
159
159
# ' @returns The value.
160
- get_value = function (key ) {
161
- return (private $ values [[key ]])
160
+ . get_value = function (key ) {
161
+ return (private $ . values [[key ]])
162
162
},
163
163
# ' @description
164
164
# ' Get the ESM string.
165
165
# ' @returns The ESM string.
166
- get_esm = function () {
167
- return (private $ esm )
166
+ . get_esm = function () {
167
+ return (private $ . esm )
168
168
},
169
169
# ' @description
170
170
# ' Get all widget values
171
171
# ' @returns List of values.
172
- get_values = function () {
173
- return (private $ values )
172
+ . get_values = function () {
173
+ return (private $ . values )
174
174
},
175
175
# ' @description
176
176
# ' Get the widget width.
177
177
# ' @returns The width.
178
- get_width = function () {
178
+ . get_width = function () {
179
179
return (private $ .width )
180
180
},
181
181
# ' @description
182
182
# ' Get the widget height.
183
183
# ' @returns The height.
184
- get_height = function () {
184
+ . get_height = function () {
185
185
return (private $ .height )
186
186
},
187
187
# ' @description
188
188
# ' Set all values. TODO: is this ever used?
189
189
# ' @param new_values A list of new values.
190
- set_values = function (new_values ) {
191
- private $ values <- new_values
190
+ . set_values = function (new_values ) {
191
+ private $ . values <- new_values
192
192
},
193
193
# ' @description
194
194
# ' Set the widget mode.
195
195
# ' @param mode The new widget mode.
196
- set_mode = function (mode ) {
196
+ . set_mode = function (mode ) {
197
197
if (! mode %in% c(" static" , " gadget" , " shiny" , " dynamic" )) {
198
198
stop(" Invalid widget mode." )
199
199
}
200
- private $ mode <- mode
200
+ private $ . mode <- mode
201
201
},
202
202
# ' @description
203
203
# ' Start the server, if not running.
204
- start_server = function () {
205
- if (is.null(private $ server )) {
206
- private $ server <- start_server(self , host = private $ server_host , port = private $ server_port )
204
+ . start_server = function () {
205
+ if (is.null(private $ . server )) {
206
+ private $ . server <- start_server(self , host = private $ . server_host , port = private $ . server_port )
207
207
}
208
208
},
209
209
# ' @description
210
210
# ' Stop the server, if running.
211
- stop_server = function () {
212
- if (! is.null(private $ server )) {
213
- private $ server $ stop()
211
+ . stop_server = function () {
212
+ if (! is.null(private $ . server )) {
213
+ private $ . server$ stop()
214
214
}
215
215
},
216
216
# ' @description
217
217
# ' Get the server hostname.
218
218
# ' @return The hostname as a string.
219
- get_host = function () {
220
- return (private $ server_host )
219
+ . get_host = function () {
220
+ return (private $ . server_host )
221
221
},
222
222
# ' @description
223
223
# ' Get the server port.
224
224
# ' @returns The port number.
225
- get_port = function () {
226
- return (private $ server_port )
225
+ . get_port = function () {
226
+ return (private $ . server_port )
227
227
},
228
228
# ' @description
229
229
# ' Custom print function for the R6 class.
230
230
# ' If mode is "shiny", falls back to original R6 print behavior.
231
231
# ' Otherwise, renders the widget.
232
232
print = function () {
233
- if (private $ mode == " shiny" ) {
233
+ if (private $ . mode == " shiny" ) {
234
234
# If Shiny mode, we just want to use the original R6 print behavior.
235
235
# Reference: https://github.com/r-lib/R6/blob/507867875fdeaffbe7f7038291256b798f6bb042/R/print.R#L35C5-L35C36
236
236
print(cat(format(self ), sep = " \n " ))
@@ -242,11 +242,11 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
242
242
# ' @description
243
243
# ' Render the widget.
244
244
render = function () {
245
- if (private $ mode == " static" ) {
245
+ if (private $ . mode == " static" ) {
246
246
invoke_static(self )
247
- } else if (private $ mode == " gadget" ) {
247
+ } else if (private $ . mode == " gadget" ) {
248
248
invoke_gadget(self )
249
- } else if (private $ mode == " dynamic" ) {
249
+ } else if (private $ . mode == " dynamic" ) {
250
250
invoke_dynamic(self )
251
251
} else {
252
252
stop(" render is meant for use with static, gadget, and dynamic modes" )
@@ -258,24 +258,24 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
258
258
# ' @keywords internal
259
259
invoke_static <- function (w ) {
260
260
w <- the_anyhtmlwidget(
261
- esm = w $ get_esm(),
262
- values = w $ get_values(),
263
- width = w $ get_width(),
264
- height = w $ get_height()
261
+ esm = w $ . get_esm(),
262
+ values = w $ . get_values(),
263
+ width = w $ . get_width(),
264
+ height = w $ . get_height()
265
265
)
266
266
print(w )
267
267
}
268
268
269
269
# ' @keywords internal
270
270
invoke_dynamic <- function (w ) {
271
- w $ start_server()
271
+ w $ . start_server()
272
272
w <- the_anyhtmlwidget(
273
- esm = w $ get_esm(),
274
- values = w $ get_values(),
275
- width = w $ get_width(),
276
- height = w $ get_height(),
277
- port = w $ get_port(),
278
- host = w $ get_host()
273
+ esm = w $ . get_esm(),
274
+ values = w $ . get_values(),
275
+ width = w $ . get_width(),
276
+ height = w $ . get_height(),
277
+ port = w $ . get_port(),
278
+ host = w $ . get_host()
279
279
)
280
280
print(w )
281
281
}
@@ -292,27 +292,27 @@ invoke_gadget <- function(w) {
292
292
shiny :: observeEvent(input $ anyhtmlwidget_on_save_changes , {
293
293
# update values on w here
294
294
for (key in names(input $ anyhtmlwidget_on_save_changes )) {
295
- w $ set_value(key , input $ anyhtmlwidget_on_save_changes [[key ]], emit_change = FALSE )
295
+ w $ . set_value(key , input $ anyhtmlwidget_on_save_changes [[key ]], emit_change = FALSE )
296
296
}
297
297
increment(increment() + 1 )
298
298
})
299
299
300
300
# output$values <- renderPrint({
301
301
# increment()
302
- # w$get_values()
302
+ # w$. get_values()
303
303
# })
304
304
#
305
305
# observeEvent(input$go, {
306
306
# w$count <- 999
307
307
# increment(increment() + 1)
308
308
# })
309
309
310
- w $ on_change(function (key , new_val ) {
310
+ w $ . on_change(function (key , new_val ) {
311
311
session $ sendCustomMessage(" anyhtmlwidget_on_change" , list (key = key , value = new_val ))
312
312
})
313
313
314
314
output $ my_widget <- render_anyhtmlwidget(expr = {
315
- the_anyhtmlwidget(esm = w $ get_esm(), values = w $ get_values(), width = w $ get_width(), height = w $ get_height())
315
+ the_anyhtmlwidget(esm = w $ . get_esm(), values = w $ . get_values(), width = w $ . get_width(), height = w $ . get_height())
316
316
})
317
317
}
318
318
@@ -343,14 +343,14 @@ widgetServer <- function(id, w) {
343
343
shiny :: moduleServer(
344
344
id ,
345
345
function (input , output , session ) {
346
- initial_values <- w $ get_values()
346
+ initial_values <- w $ . get_values()
347
347
rv <- do.call(shiny :: reactiveValues , initial_values )
348
348
349
349
shiny :: observeEvent(input $ anyhtmlwidget_on_save_changes , {
350
350
# update values on w here
351
351
for (key in names(input $ anyhtmlwidget_on_save_changes )) {
352
352
rv [[key ]] <- input $ anyhtmlwidget_on_save_changes [[key ]]
353
- w $ set_value(key , input $ anyhtmlwidget_on_save_changes [[key ]], emit_change = FALSE )
353
+ w $ . set_value(key , input $ anyhtmlwidget_on_save_changes [[key ]], emit_change = FALSE )
354
354
}
355
355
})
356
356
@@ -361,7 +361,7 @@ widgetServer <- function(id, w) {
361
361
}
362
362
363
363
output $ widget <- render_anyhtmlwidget(expr = {
364
- the_anyhtmlwidget(esm = w $ get_esm(), values = initial_values , width = w $ get_width(), height = w $ get_height(), ns_id = id )
364
+ the_anyhtmlwidget(esm = w $ . get_esm(), values = initial_values , width = w $ . get_width(), height = w $ . get_height(), ns_id = id )
365
365
})
366
366
367
367
return (rv )
0 commit comments