Skip to content

Commit e4fffaf

Browse files
author
Ryan Patrick Kyle
committed
🔨 use app$callback for clientside
1 parent b85c6b7 commit e4fffaf

File tree

2 files changed

+17
-29
lines changed

2 files changed

+17
-29
lines changed

R/dash.R

+16-28
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,20 @@
7676
#' \describe{
7777
#' \item{output}{a named list including a component `id` and `property`}
7878
#' \item{params}{an unnamed list of [input] and [state] statements, each with defined `id` and `property`}
79-
#' \item{func}{any valid R function which generates [output] provided [input] and/or [state] arguments}
79+
#' \item{func}{any valid R or JavaScript function which generates [output] provided [input] and/or [state] arguments}
8080
#' }
8181
#' The `output` argument defines which layout component property should
8282
#' receive the results (via the [output] object). The events that
8383
#' trigger the callback are then described by the [input] (and/or [state])
8484
#' object(s) (which should reference layout components), which become
85-
#' argument values for the callback handler defined in `func`.
86-
#' }
87-
#' \item{`clientside_callback(output = NULL, params = NULL, clientside_function = NULL)`}{
88-
#' A callback function defintion. The `clientside_function` argument accepts a call
89-
#' to clientsideFunction(), which describes the locally served JavaScript
90-
#' function to call, and `output` defines which layout component property
91-
#' should adopt the results (via an [output] object). To determine what
92-
#' events trigger this callback, provide [input] (and/or [state]) object(s)
93-
#' (which should reference layout components) by passing them within `params` to `clientside_function`.
85+
#' argument values for the callback handler defined in `func`.]
86+
#'
87+
#' `func` may either be an anonymous R function, or a call to
88+
#' `clientsideFunction()`, which describes a locally served JavaScript
89+
#' function instead. The latter defines a "clientside callback", which
90+
#' updates components without passing data to and from the Dash backend.
91+
#' The latter may offer improved performance relative to callbacks written
92+
#' in R.
9493
#' }
9594
#' \item{`run_server(host = Sys.getenv('DASH_HOST', "127.0.0.1"),
9695
#' port = Sys.getenv('DASH_PORT', 8050), block = TRUE, showcase = FALSE, ...)`}{
@@ -544,30 +543,19 @@ Dash <- R6::R6Class(
544543
inputs <- params[vapply(params, function(x) 'input' %in% attr(x, "class"), FUN.VALUE=logical(1))]
545544
state <- params[vapply(params, function(x) 'state' %in% attr(x, "class"), FUN.VALUE=logical(1))]
546545

546+
if (is.function(func)) {
547+
clientside_function <- NULL
548+
} else {
549+
clientside_function <- func
550+
func <- NULL
551+
}
552+
547553
# register the callback_map
548554
private$callback_map <- insertIntoCallbackMap(private$callback_map,
549555
inputs,
550556
output,
551557
state,
552558
func,
553-
clientside_function=NULL)
554-
},
555-
556-
# ------------------------------------------------------------------------
557-
# clientside callback registration
558-
# ------------------------------------------------------------------------
559-
560-
clientside_callback = function(output, params, clientside_function) {
561-
assert_valid_callbacks(output, params, clientside_function)
562-
563-
inputs <- params[vapply(params, function(x) 'input' %in% attr(x, "class"), FUN.VALUE=logical(1))]
564-
state <- params[vapply(params, function(x) 'state' %in% attr(x, "class"), FUN.VALUE=logical(1))]
565-
566-
private$callback_map <- insertIntoCallbackMap(private$callback_map,
567-
inputs,
568-
output,
569-
state,
570-
func = NULL,
571559
clientside_function)
572560
},
573561

tests/integration/clientside/test_clientside.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
}
2727
)
2828
29-
app$clientside_callback(
29+
app$callback(
3030
output('output-clientside', 'children'),
3131
params=list(input('input', 'value')),
3232
clientsideFunction(

0 commit comments

Comments
 (0)