Skip to content

Commit df4c737

Browse files
author
Ryan Patrick Kyle
committed
📚 add documentation, 🛃 export fns
📚 update docs for Dash class 📚 add docs for functions 📚 document and export createCallbackId 📚 document createCallbackId 📚 document createCallbackId update namespace and add dashLogger.Rd
1 parent 1461ee7 commit df4c737

8 files changed

+269
-18
lines changed

NAMESPACE

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
S3method(print,dash_component)
44
export(Dash)
5-
export(dashNoUpdate)
6-
export(createCallbackId)
75
export(clientsideFunction)
6+
export(createCallbackId)
7+
export(dashLogger)
8+
export(dashNoUpdate)
9+
export(getEnvVar)
810
export(input)
911
export(output)
1012
export(state)
11-
export(dashLogger)
1213
import(dashCoreComponents)
1314
import(dashHtmlComponents)
1415
importFrom(R6,R6Class)

R/dash.R

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
#' assets_ignore = '',
1313
#' serve_locally = TRUE,
1414
#' routes_pathname_prefix = '/',
15-
#' requests_pathname_prefix = '/'
15+
#' requests_pathname_prefix = '/',
16+
#' external_scripts = NULL,
17+
#' external_stylesheets = NULL,
18+
#' suppress_callback_exceptions = FALSE
1619
#' )
1720
#'
1821
#' @section Arguments:
@@ -88,11 +91,32 @@
8891
#' The latter may offer improved performance relative to callbacks written
8992
#' in R.
9093
#' }
94+
#' \item{`callback_context()`}{
95+
#' The `callback_context` method permits retrieving the inputs which triggered
96+
#' the firing of a given callback, and allows introspection of the input/state
97+
#' values given their names. It is only available from within a callback;
98+
#' attempting to use this method outside of a callback will result in a warning.
99+
#' }
91100
#' \item{`run_server(host = Sys.getenv('DASH_HOST', "127.0.0.1"),
92101
#' port = Sys.getenv('DASH_PORT', 8050), block = TRUE, showcase = FALSE, ...)`}{
93-
#' Launch the application. If provided, `host`/`port` set
94-
#' the `host`/`port` fields of the underlying [fiery::Fire] web
95-
#' server. The `block`/`showcase`/`...` arguments are passed along
102+
#' The `run_server` method has 13 formal arguments, several of which are optional:
103+
#' \describe{
104+
#' \item{host}{Character. A string specifying a valid IPv4 address for the Fiery server, or `0.0.0.0` to listen on all addresses. Default is `127.0.0.1` Environment variable: `DASH_HOST`.}
105+
#' \item{port}{Integer. Specifies the port number on which the server should listen (default is `8050`). Environment variable: `DASH_PORT`.}
106+
#' \item{block}{Logical. Start the server while blocking console input? Default is `TRUE`.}
107+
#' \item{showcase}{Logical. Load the Dash application into the default web browser when server starts? Default is `FALSE`.}
108+
#' \item{use_viewer}{Logical. Load the Dash application into RStudio's viewer pane? Requires that `host` is either `127.0.0.1` or `localhost`, and that Dash application is started within RStudio; if `use_viewer = TRUE` and these conditions are not satsified, the user is warned and the app opens in the default browser instead. Default is `FALSE`.}
109+
#' \item{debug}{Logical. Enable/disable all the dev tools unless overridden by the arguments or environment variables. Default is `FALSE` when called via `run_server`. Environment variable: `DASH_DEBUG`.}
110+
#' \item{dev_tools_ui}{Logical. Show Dash's dev tools UI? Default is `TRUE` if `debug == TRUE`, `FALSE` otherwise. Environment variable: `DASH_UI`.}
111+
#' \item{dev_tools_hot_reload}{Logical. Activate hot reloading when app, assets, and component files change? Default is `TRUE` if `debug == TRUE`, `FALSE` otherwise. Requires that the Dash application is loaded using `source()`, so that `srcref` attributes are available for executed code. Environment variable: `DASH_HOT_RELOAD`.}
112+
#' \item{dev_tools_hot_reload_interval}{Numeric. Interval in seconds for the client to request the reload hash. Default is `3`. Environment variable: `DASH_HOT_RELOAD_INTERVAL`.}
113+
#' \item{dev_tools_hot_reload_watch_interval}{Numeric. Interval in seconds for the server to check asset and component folders for changes. Default `0.5`. Environment variable: `DASH_HOT_RELOAD_WATCH_INTERVAL`.}
114+
#' \item{dev_tools_props_check}{Logical. Validate the types and values of Dash component properties? Default is `TRUE` if `debug == TRUE`, `FALSE` otherwise. Environment variable: `DASH_PROPS_CHECK`.}
115+
#' \item{dev_tools_prune_errors}{Logical. Reduce tracebacks to just user code, stripping out Fiery and Dash pieces? Only available with debugging. `TRUE` by default, set to `FALSE` to see the complete traceback. Environment variable: `DASH_PRUNE_ERRORS`.}
116+
#' \item{dev_tools_silence_routes_logging}{Logical. Replace Fiery's default logger with `dashLogger` instead (will remove all routes logging)? Enabled with debugging by default because hot reload hash checks generate a lot of requests.}
117+
#' }
118+
#' Starts the Fiery server in local mode. If a parameter can be set by an environment variable, that is listed too. Values provided here take precedence over environment variables.
119+
#' Launch the application. If provided, `host`/`port` set the `host`/`port` fields of the underlying [fiery::Fire] web server. The `block`/`showcase`/`...` arguments are passed along
96120
#' to the `ignite()` method of the [fiery::Fire] server.
97121
#' }
98122
#' }
@@ -1270,3 +1294,23 @@ validate_dependency <- function(layout_, dependency) {
12701294

12711295
TRUE
12721296
}
1297+
1298+
#' Retrieve Environment Variables
1299+
#'
1300+
#' This function queries an environment variable, returning the specified value when the result is undefined or an empty string.
1301+
#'
1302+
#' @param env_var Character. The name of the environment variable whose value will be retrieved.
1303+
#' @param value_if_missing Object. The value to be returned if the value of `env_var` is "", or the variable is undefined.
1304+
#'
1305+
#' @export
1306+
#' @examples
1307+
#' # returns NULL if "FOO" does not exist
1308+
#' getEnvVar("FOO", NULL)
1309+
getEnvVar <- function(env_var, value_if_missing=NULL) {
1310+
value <- Sys.getenv(env_var)
1311+
if (value == "") {
1312+
return(value_if_missing)
1313+
} else {
1314+
return(value)
1315+
}
1316+
}

R/utils.R

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,24 @@ getDashMetadata <- function(pkgname) {
926926
return(metadataFn)
927927
}
928928

929+
#' Construct a Callback ID
930+
#'
931+
#' Create a callback ID from a Dash output object.
932+
#'
933+
#' @param output The result of calling `output()`; an object whose class membership includes `dash_dependency` and `output`.
934+
#'
935+
#' @details This function is primarily used internally for inserting entries into Dash's callback map, but may also be
936+
#' used to construct IDs for testing purposes from either single or multiple output callbacks.
937+
#'
938+
#'
939+
#' @export
940+
#'
941+
#' @examples
942+
#' # single output context -- will create "stock-graph.figure"
943+
#' createCallbackId(output('stock-graph', 'figure'))
944+
#'
945+
#' # multiple output context -- will create "..stock-graph.figure...bond-graph.figure.."
946+
#' createCallbackId(list(output('stock-graph', 'figure'), output('bond-graph', 'figure')))
929947
createCallbackId <- function(output) {
930948
# check if callback uses single output
931949
if (!any(sapply(output, is.list))) {
@@ -1060,6 +1078,20 @@ changedAssets <- function(before, after) {
10601078
)
10611079
}
10621080

1081+
#' Generate a Fiery Log Which Omits Routing and Request Entries
1082+
#'
1083+
#' Use a less verbose Fiery logger when debugging Dash for R applications.
1084+
#'
1085+
#' @param event Character. The event type to log; options are `'error'`, `'warning'` or `'message'`.
1086+
#' @param message Character. The message text to include immediately after the event text.
1087+
#' @param request The request to be logged; default is `NULL`.
1088+
#' @param time The time of the event to be logged; default is `Sys.time()`, which reports the current system time.
1089+
#'
1090+
#' @details This function is specified as the logger when `dev_tools_silence_routes_logging = TRUE`. It is not
1091+
#' intended to be called directly.
1092+
#'
1093+
#'
1094+
#' @export
10631095
dashLogger <- function(event = NULL,
10641096
message = NULL,
10651097
request = NULL,
@@ -1099,6 +1131,39 @@ dashLogger <- function(event = NULL,
10991131
}
11001132
}
11011133

1134+
#' Define a clientside callback
1135+
#'
1136+
#' Create a callback that updates the output by calling a clientside (JavaScript) function instead of an R function.
1137+
#'
1138+
#' @param namespace Character. Describes where the JavaScript function resides (Dash will look
1139+
#' for the function at `window[namespace][function_name]`.)
1140+
#' @param function_name Character. Provides the name of the JavaScript function to call.
1141+
#'
1142+
#' @details With this signature, Dash's front-end will call `window.my_clientside_library.my_function` with the current
1143+
#' values of the `value` properties of the components `my-input` and `another-input` whenever those values change.
1144+
#' Include a JavaScript file by including it your `assets/` folder. The file can be named anything but you'll need to
1145+
#' assign the function's namespace to the `window`. For example, this file might look like:
1146+
#' \preformatted{window.my_clientside_library = \{
1147+
#' my_function: function(input_value_1, input_value_2) \{
1148+
#' return (
1149+
#' parseFloat(input_value_1, 10) +
1150+
#' parseFloat(input_value_2, 10)
1151+
#' );
1152+
#' \}
1153+
#'\}
1154+
#'}
1155+
#'
1156+
#'
1157+
#' @export
1158+
#' @examples \dontrun{
1159+
#' app$callback(
1160+
#' output('output-clientside', 'children'),
1161+
#' params=list(input('input', 'value')),
1162+
#' clientsideFunction(
1163+
#' namespace = 'my_clientside_library',
1164+
#' function_name = 'my_function'
1165+
#' )
1166+
#' )}
11021167
clientsideFunction <- function(namespace, function_name) {
11031168
return(list(namespace=namespace, function_name=function_name))
11041169
}

man/Dash.Rd

Lines changed: 39 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/clientsideFunction.Rd

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/createCallbackId.Rd

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dashLogger.Rd

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/getEnvVar.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)