Skip to content

Commit 89e8ccf

Browse files
committed
Clean up
1 parent 49c769f commit 89e8ccf

File tree

3 files changed

+25
-33
lines changed

3 files changed

+25
-33
lines changed

R/server.R

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
library(httpuv)
2-
31
start_server <- function(w, host = "0.0.0.0", port = 8080) {
42
app <- list(
53
onWSOpen = function(ws) {
@@ -37,7 +35,7 @@ start_server <- function(w, host = "0.0.0.0", port = 8080) {
3735
}
3836
)
3937

40-
s <- startServer(host = host, port = port, app = app)
38+
s <- httpuv::startServer(host = host, port = port, app = app)
4139
return(s)
4240
}
4341

R/widget.R

+23-29
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
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.
55
#' @param width The width of the widget as a number or CSS string. Optional.
66
#' @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.
79
#' @param element_id An element ID. Optional.
8-
#' @return The htmlwidget.
10+
#' @return The result of htmlwidgets::createWidget.
911
#'
10-
#' @export
12+
#' @keywords internal
1113
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
1414
params = list(
1515
esm = esm,
1616
values = values,
@@ -19,9 +19,8 @@ the_anyhtmlwidget <- function(esm, values = NULL, ns_id = NULL, width = NULL, he
1919
host = host
2020
)
2121

22-
# create widget
2322
htmlwidgets::createWidget(
24-
name = 'anyhtmlwidget',
23+
'anyhtmlwidget',
2524
params,
2625
width = width,
2726
height = height,
@@ -51,15 +50,15 @@ the_anyhtmlwidget <- function(esm, values = NULL, ns_id = NULL, width = NULL, he
5150
#' is useful if you want to save an expression in a variable.
5251
#' @return The Shiny UI element.
5352
#'
54-
#' @rdname anyhtmlwidget-shiny
55-
#' @export
53+
#' @keywords internal
5654
anyhtmlwidget_output <- function(output_id, width = '100%', height = '400px'){
5755
htmlwidgets::shinyWidgetOutput(output_id, 'anyhtmlwidget', width, height, package = 'anyhtmlwidget')
5856
}
5957

6058
#' @name anyhtmlwidget-shiny
6159
#' @return The Shiny server output.
62-
#' @export
60+
#'
61+
#' @keywords internal
6362
render_anyhtmlwidget <- function(expr, env = parent.frame(), quoted = FALSE) {
6463
if (!quoted) { expr <- substitute(expr) } # force quoted
6564
htmlwidgets::shinyRenderWidget(expr, anyhtmlwidget_output, env, quoted = TRUE)
@@ -86,7 +85,7 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
8685

8786
),
8887
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, ...) {
9089
private$esm <- .esm
9190
private$values <- list(...)
9291

@@ -103,9 +102,6 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
103102
private$server_host <- "0.0.0.0"
104103
private$server_port <- httpuv::randomPort(min = 8000, max = 9000, n = 1000)
105104

106-
if(is.na(.mode)) {
107-
.mode <- "static"
108-
}
109105
if(!.mode %in% c("static", "gadget", "shiny", "dynamic")) {
110106
stop("Invalid widget mode.")
111107
}
@@ -228,16 +224,14 @@ invoke_dynamic <- function(w) {
228224
}
229225

230226
invoke_gadget <- function(w) {
231-
require(shiny)
232-
233-
ui <- tagList(
227+
ui <- shiny::tagList(
234228
anyhtmlwidget_output(output_id = "my_widget", width = '100%', height = '100%')
235229
)
236230

237231
server <- function(input, output, session) {
238-
increment <- reactiveVal(0)
232+
increment <- shiny::reactiveVal(0)
239233

240-
observeEvent(input$anyhtmlwidget_on_save_changes, {
234+
shiny::observeEvent(input$anyhtmlwidget_on_save_changes, {
241235
# update values on w here
242236
for(key in names(input$anyhtmlwidget_on_save_changes)) {
243237
w$set_value(key, input$anyhtmlwidget_on_save_changes[[key]], emit_change = FALSE)
@@ -264,25 +258,25 @@ invoke_gadget <- function(w) {
264258
})
265259
}
266260

267-
runGadget(ui, server)
261+
shiny::runGadget(ui, server)
268262
}
269263

270264
# Shiny module UI
271265
widgetUI <- function(id, width = '100%', height = '400px') {
272-
ns <- NS(id)
266+
ns <- shiny::NS(id)
273267
anyhtmlwidget_output(output_id = ns("widget"), width = width, height = height)
274268
}
275269

276270
# Shiny module server
277271
widgetServer <- function(id, w) {
278-
ns <- NS(id)
279-
moduleServer(
272+
ns <- shiny::NS(id)
273+
shiny::moduleServer(
280274
id,
281275
function(input, output, session) {
282276
initial_values <- w$get_values()
283-
rv <- do.call(reactiveValues, initial_values)
277+
rv <- do.call(shiny::reactiveValues, initial_values)
284278

285-
observeEvent(input$anyhtmlwidget_on_save_changes, {
279+
shiny::observeEvent(input$anyhtmlwidget_on_save_changes, {
286280
# update values on w here
287281
for(key in names(input$anyhtmlwidget_on_save_changes)) {
288282
rv[[key]] <- input$anyhtmlwidget_on_save_changes[[key]]
@@ -291,7 +285,7 @@ widgetServer <- function(id, w) {
291285
})
292286

293287
for(key in names(initial_values)) {
294-
observeEvent(rv[[key]], {
288+
shiny::observeEvent(rv[[key]], {
295289
session$sendCustomMessage(ns("anyhtmlwidget_on_change"), list(key = key, value = rv[[key]]))
296290
})
297291
}

hello.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ export default { render };
1919
"
2020

2121
widget <- anyhtmlwidget::AnyHtmlWidget$new(.esm = esm, .mode = "dynamic", count = 1)
22-
widget$render()
22+
widget

0 commit comments

Comments
 (0)