Skip to content

Commit b588447

Browse files
authored
Prefix internal fields (#22)
* Prefixes * ignore things * Vignette cleanup
1 parent 09d8251 commit b588447

File tree

6 files changed

+83
-75
lines changed

6 files changed

+83
-75
lines changed

.Rbuildignore

+7
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
^.*\.Rproj$
22
^\.Rproj\.user$
3+
^\.BBSoptions$
4+
^.github$
5+
^pkgdown$
6+
^vignettes$
7+
^docs$
8+
^index\.md$
9+
^.ipynb_checkpoints$

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.Rhistory
22
.Rproj.user/
33
.DS_Store
4-
docs/
4+
docs/
5+
.ipynb_checkpoints/
6+
.RData

R/server.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ start_server <- function(w, host = "0.0.0.0", port = 8080) {
44
# The ws object is a WebSocket object
55
cat("Server connection opened.\n")
66

7-
w$on_change(function(key, new_val) {
7+
w$.on_change(function(key, new_val) {
88
msg_list <- list(
99
type = jsonlite::unbox("on_change"),
1010
payload = list(
@@ -22,7 +22,7 @@ start_server <- function(w, host = "0.0.0.0", port = 8080) {
2222

2323
if(msg_type == "on_save_changes") {
2424
for(key in names(msg_val)) {
25-
w$set_value(key, msg_val[[key]], emit_change = FALSE)
25+
w$.set_value(key, msg_val[[key]], emit_change = FALSE)
2626
}
2727
}
2828

R/widget.R

+68-68
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
7474
# TODO: prefix with dot
7575
# since R6 requires all items in
7676
# 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,
8484
.width = NULL,
8585
.height = NULL
8686
),
@@ -95,8 +95,8 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
9595
#' @param .commands TODO
9696
#' @param ... All other named arguments will be used to create active bindings on the instance.
9797
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(...)
100100

101101
if(is.na(.width)) {
102102
.width <- "100%"
@@ -108,24 +108,24 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
108108
private$.width <- .width
109109
private$.height <- .height
110110

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)
113113

114114
if(!.mode %in% c("static", "gadget", "shiny", "dynamic")) {
115115
stop("Invalid widget mode.")
116116
}
117-
private$mode <- .mode
117+
private$.mode <- .mode
118118

119119
active_env <- self$`.__enclos_env__`$`.__active__`
120120

121121
# TODO: check that values is not NA
122-
for(key in names(private$values)) {
122+
for(key in names(private$.values)) {
123123
active_binding <- function(val) {
124124
if(missing(val)) {
125-
return(self$get_value(key))
125+
return(self$.get_value(key))
126126
} else {
127-
self$set_value(key, val)
128-
if(private$mode == "static") {
127+
self$.set_value(key, val)
128+
if(private$.mode == "static") {
129129
self$print()
130130
}
131131
}
@@ -139,98 +139,98 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
139139
#' Set a value.
140140
#' @param key The key of the value to set.
141141
#' @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)) {
146146
# 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)
148148
}
149149
},
150150
#' @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.
152152
#' @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
155155
},
156156
#' @description
157157
#' Get a particular value.
158158
#' @param key The key of the value to get.
159159
#' @returns The value.
160-
get_value = function(key) {
161-
return(private$values[[key]])
160+
.get_value = function(key) {
161+
return(private$.values[[key]])
162162
},
163163
#' @description
164164
#' Get the ESM string.
165165
#' @returns The ESM string.
166-
get_esm = function() {
167-
return(private$esm)
166+
.get_esm = function() {
167+
return(private$.esm)
168168
},
169169
#' @description
170170
#' Get all widget values
171171
#' @returns List of values.
172-
get_values = function() {
173-
return(private$values)
172+
.get_values = function() {
173+
return(private$.values)
174174
},
175175
#' @description
176176
#' Get the widget width.
177177
#' @returns The width.
178-
get_width = function() {
178+
.get_width = function() {
179179
return(private$.width)
180180
},
181181
#' @description
182182
#' Get the widget height.
183183
#' @returns The height.
184-
get_height = function() {
184+
.get_height = function() {
185185
return(private$.height)
186186
},
187187
#' @description
188188
#' Set all values. TODO: is this ever used?
189189
#' @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
192192
},
193193
#' @description
194194
#' Set the widget mode.
195195
#' @param mode The new widget mode.
196-
set_mode = function(mode) {
196+
.set_mode = function(mode) {
197197
if(!mode %in% c("static", "gadget", "shiny", "dynamic")) {
198198
stop("Invalid widget mode.")
199199
}
200-
private$mode <- mode
200+
private$.mode <- mode
201201
},
202202
#' @description
203203
#' 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)
207207
}
208208
},
209209
#' @description
210210
#' 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()
214214
}
215215
},
216216
#' @description
217217
#' Get the server hostname.
218218
#' @return The hostname as a string.
219-
get_host = function() {
220-
return(private$server_host)
219+
.get_host = function() {
220+
return(private$.server_host)
221221
},
222222
#' @description
223223
#' Get the server port.
224224
#' @returns The port number.
225-
get_port = function() {
226-
return(private$server_port)
225+
.get_port = function() {
226+
return(private$.server_port)
227227
},
228228
#' @description
229229
#' Custom print function for the R6 class.
230230
#' If mode is "shiny", falls back to original R6 print behavior.
231231
#' Otherwise, renders the widget.
232232
print = function() {
233-
if(private$mode == "shiny") {
233+
if(private$.mode == "shiny") {
234234
# If Shiny mode, we just want to use the original R6 print behavior.
235235
# Reference: https://github.com/r-lib/R6/blob/507867875fdeaffbe7f7038291256b798f6bb042/R/print.R#L35C5-L35C36
236236
print(cat(format(self), sep = "\n"))
@@ -242,11 +242,11 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
242242
#' @description
243243
#' Render the widget.
244244
render = function() {
245-
if(private$mode == "static") {
245+
if(private$.mode == "static") {
246246
invoke_static(self)
247-
} else if(private$mode == "gadget") {
247+
} else if(private$.mode == "gadget") {
248248
invoke_gadget(self)
249-
} else if(private$mode == "dynamic") {
249+
} else if(private$.mode == "dynamic") {
250250
invoke_dynamic(self)
251251
} else {
252252
stop("render is meant for use with static, gadget, and dynamic modes")
@@ -258,24 +258,24 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
258258
#' @keywords internal
259259
invoke_static <- function(w) {
260260
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()
265265
)
266266
print(w)
267267
}
268268

269269
#' @keywords internal
270270
invoke_dynamic <- function(w) {
271-
w$start_server()
271+
w$.start_server()
272272
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()
279279
)
280280
print(w)
281281
}
@@ -292,27 +292,27 @@ invoke_gadget <- function(w) {
292292
shiny::observeEvent(input$anyhtmlwidget_on_save_changes, {
293293
# update values on w here
294294
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)
296296
}
297297
increment(increment() + 1)
298298
})
299299

300300
# output$values <- renderPrint({
301301
# increment()
302-
# w$get_values()
302+
# w$.get_values()
303303
# })
304304
#
305305
# observeEvent(input$go, {
306306
# w$count <- 999
307307
# increment(increment() + 1)
308308
# })
309309

310-
w$on_change(function(key, new_val) {
310+
w$.on_change(function(key, new_val) {
311311
session$sendCustomMessage("anyhtmlwidget_on_change", list(key = key, value = new_val))
312312
})
313313

314314
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())
316316
})
317317
}
318318

@@ -343,14 +343,14 @@ widgetServer <- function(id, w) {
343343
shiny::moduleServer(
344344
id,
345345
function(input, output, session) {
346-
initial_values <- w$get_values()
346+
initial_values <- w$.get_values()
347347
rv <- do.call(shiny::reactiveValues, initial_values)
348348

349349
shiny::observeEvent(input$anyhtmlwidget_on_save_changes, {
350350
# update values on w here
351351
for(key in names(input$anyhtmlwidget_on_save_changes)) {
352352
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)
354354
}
355355
})
356356

@@ -361,7 +361,7 @@ widgetServer <- function(id, w) {
361361
}
362362

363363
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)
365365
})
366366

367367
return(rv)

vignettes/basics.Rmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function render({ el, model }) {
4141
export default { render };
4242
"
4343
44-
widget <- anyhtmlwidget::AnyHtmlWidget$new(
44+
widget <- AnyHtmlWidget$new(
4545
.esm = esm,
4646
.mode = "static",
4747
.height='400px',

vignettes/shiny.Rmd

+2-3
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ function render({ el, model }) {
3232
model.save_changes();
3333
});
3434
model.on('change:count', () => {
35-
btn.innerHTML = `count is ${count()}`;
35+
btn.innerHTML = `count is ${count()}`;
3636
});
3737
el.appendChild(btn);
3838
}
3939
export default { render };
4040
"
4141
42-
widget <- anyhtmlwidget::AnyHtmlWidget$new(.esm = esm, .mode = "shiny", count = 2)
42+
widget <- AnyHtmlWidget$new(.esm = esm, .mode = "shiny", count = 2)
4343
4444
4545
ui <- fluidPage(
@@ -62,5 +62,4 @@ server <- function(input, output, session) {
6262
}
6363
6464
shinyApp(ui, server)
65-
6665
```

0 commit comments

Comments
 (0)