A framework for building analytical web applications, Dash offers a pleasant and productive development experience. No JavaScript required.
Dash
An R6::R6Class generator object
Dash$new( name = NULL, server = fiery::Fire$new(), assets_folder = 'assets', assets_url_path = '/assets', eager_loading = FALSE, assets_ignore = '', serve_locally = TRUE, meta_tags = NULL, url_base_pathname = '/', routes_pathname_prefix = NULL, requests_pathname_prefix = NULL, external_scripts = NULL, external_stylesheets = NULL, suppress_callback_exceptions = FALSE, show_undo_redo = FALSE )
name |
Character. The name of the Dash application (placed in the title
of the HTML page). DEPRECATED; please use index_string() or interpolate_index() instead. |
|
server |
The web server used to power the application. Must be a fiery::Fire object. | |
assets_folder |
Character. A path, relative to the current working directory,
for extra files to be used in the browser. Default is "assets". All .js and
.css files will be loaded immediately unless excluded by assets_ignore ,
and other files such as images will be served if requested. Default is assets . |
|
assets_url_path |
Character. Specify the URL path for asset serving. Default is assets . |
|
eager_loading |
Logical. Controls whether asynchronous resources are prefetched (if TRUE ) or loaded on-demand (if FALSE ). |
|
assets_ignore |
Character. A regular expression, to match assets to omit from immediate loading. Ignored files will still be served if specifically requested. You cannot use this to prevent access to sensitive files. | |
serve_locally |
Logical. Whether to serve HTML dependencies locally or remotely (via URL). | |
compress |
Logical. Whether to try to compress files and data served by Fiery.
By default, brotli is attempted first, then gzip , then the deflate algorithm, before falling
back to identity . |
|
meta_tags |
List of lists. HTML <meta> tags to be added to the index page.
Each list element should have the attributes and values for one tag, eg:
list(name = 'description', content = 'My App') . |
|
url_base_pathname |
Character. A local URL prefix to use app-wide. Default is
/ . Both requests_pathname_prefix and routes_pathname_prefix default to url_base_pathname .
Environment variable is DASH_URL_BASE_PATHNAME . |
|
routes_pathname_prefix |
Character. A prefix applied to the backend routes.
Environment variable is DASH_ROUTES_PATHNAME_PREFIX . |
|
requests_pathname_prefix |
Character. A prefix applied to request endpoints
made by Dash's front-end. Environment variable is DASH_REQUESTS_PATHNAME_PREFIX . |
|
external_scripts |
List. An optional list of valid URLs from which to serve JavaScript source for rendered pages. | |
external_stylesheets |
List. An optional list of valid URLs from which to serve CSS for rendered pages. | |
suppress_callback_exceptions |
Logical. Whether to relay warnings about possible layout mis-specifications when registering a callback. |
server
A cloned (and modified) version of the fiery::Fire object
provided to the server
argument (various routes will be added which enable
Dash functionality).
config
A list of configuration options passed along to dash-renderer. Users shouldn't need to alter any of these options unless they are constructing their own authorization front-end or otherwise need to know where the application is making API calls.
layout(...)
Set the layout (i.e., user interface). The layout should be either a collection of Dash components (e.g., dccSlider, htmlDiv, etc) or a function which returns a collection of components.
layout_get(render = TRUE)
Retrieves the layout. If render is TRUE
, and the layout is a function,
the result of the function (rather than the function itself) is returned.
callback(output, params, func)
The callback
method has three formal arguments:
a named list including a component id
and property
an unnamed list of input and state statements, each with defined id
and property
any valid R function which generates output provided input and/or state arguments,
a character string containing valid JavaScript, or a call to clientsideFunction including namespace
and function_name
arguments for a locally served JavaScript function
output
argument defines which layout component property should
receive the results (via the output object). The events that
trigger the callback are then described by the input (and/or state)
object(s) (which should reference layout components), which become
argument values for R callback handlers defined in func
. Here func
may
either be an anonymous R function, a JavaScript function provided as a
character string, or a call to clientsideFunction()
, which describes a
locally served JavaScript function instead. The latter two methods define
a "clientside callback", which updates components without passing data to and
from the Dash backend. The latter may offer improved performance relative
to callbacks written purely in R.title("dash")
The title of the app. If no title is supplied, Dash for R will use 'dash'.
title("dash")
The title of the app. If no title is supplied, Dash for R will use 'dash'.
callback_context()
The callback_context
method permits retrieving the inputs which triggered
the firing of a given callback, and allows introspection of the input/state
values given their names. It is only available from within a callback;
attempting to use this method outside of a callback will result in a warning.
get_asset_url(asset_path, prefix)
The get_asset_url
method permits retrieval of an asset's URL given its filename.
For example, app$get_asset_url('style.css')
should return /assets/style.css
when
assets_folder = 'assets'
. By default, the prefix is the value of requests_pathname_prefix
,
but this is configurable via the prefix
parameter. Note: this method will
present a warning and return NULL
if the Dash app was not loaded via source()
if the DASH_APP_PATH
environment variable is undefined.
get_relative_path(path, requests_pathname_prefix)
The get_relative_path
method simplifies the handling of URLs and pathnames for apps
running locally and on a deployment server such as Dash Enterprise. It handles the prefix
for requesting assets similar to the get_asset_url
method, but can also be used for URL handling
in components such as dccLink
or dccLocation
. For example, app$get_relative_url("/page/")
would return /app/page/
for an app running on a deployment server. The path must be prefixed with
a /
.
Character. A path string prefixed with a leading /
which directs at a path or asset directory.
Character. The pathname prefix for the app on a deployed application. Defaults to the environment variable set by the server, or ""
if run locally.
strip_relative_path(path, requests_pathname_prefix)
The strip_relative_path
method simplifies the handling of URLs and pathnames for apps
running locally and on a deployment server such as Dash Enterprise. It acts almost opposite the get_relative_path
method, by taking a relative path
as an input, and returning the path
stripped of the requests_pathname_prefix
,
and any leading or trailing /
. For example, a path string /app/homepage/
, would be returned as
homepage
. This is particularly useful for dccLocation
URL routing.
Character. A path string prefixed with a leading /
and requests_pathname_prefix
which directs at a path or asset directory.
Character. The pathname prefix for the app on a deployed application. Defaults to the environment variable set by the server, or ""
if run locally.
index_string(string)
The index_string
method allows the specification of a custom index by changing
the default HTML
template that is generated by the Dash UI. Meta tags, CSS, Javascript,
are some examples of features that can be modified.
This method will present a warning if your HTML template is missing any necessary elements
and return an error if a valid index is not defined. The following interpolation keys are
currently supported:
{%metas%}
Optional - The registered meta tags.
{%favicon%}
Optional - A favicon link tag if found in assets.
{%css%}
Optional - Link tags to css resources.
{%config%}
Required - Config generated by dash for the renderer.
{%app_entry%}
Required - The container where dash react components are rendered.
{%scripts%}
Required - Collected dependencies scripts tags.
"<!DOCTYPE html> <html> <head> {%meta_tags%} <title>{{ {%favicon%} {%css_tags%} </head> <body> {%app_entry%} <footer> {%config%} {%scripts%} </footer> </body> </html>"
interpolate_index(template_index, ...)
With the interpolate_index
method, we can pass a custom index with template string
variables that are already evaluated. We can directly pass arguments to the template_index
by assigning them to variables present in the template. This is similar to the index_string
method
but offers the ability to change the default components of the Dash index as seen in the example below:
app$interpolate_index( template_index, metas = "<meta_charset='UTF-8'/>", renderer = renderer, config = config)
Character. A formatted string with the HTML index string. Defaults to the initial template
Named List. The unnamed arguments can be passed as individual named lists corresponding to the components
of the Dash html index. These include the same arguments as those found in the index_string()
template.
run_server(host = Sys.getenv('HOST', "127.0.0.1"), port = Sys.getenv('PORT', 8050), block = TRUE, showcase = FALSE, ...)
The run_server
method has 13 formal arguments, several of which are optional:
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: HOST
.
Integer. Specifies the port number on which the server should listen (default is 8050
). Environment variable: PORT
.
Logical. Start the server while blocking console input? Default is TRUE
.
Logical. Load the Dash application into the default web browser when server starts? Default is FALSE
.
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 satisfied, the user is warned and the app opens in the default browser instead. Default is FALSE
.
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
.
Logical. Show Dash's dev tools UI? Default is TRUE
if debug == TRUE
, FALSE
otherwise. Environment variable: DASH_UI
.
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
.
Numeric. Interval in seconds for the client to request the reload hash. Default is 3
. Environment variable: DASH_HOT_RELOAD_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
.
Integer. Maximum number of failed reload hash requests before failing and displaying a pop up. Default 0.5
. Environment variable: DASH_HOT_RELOAD_MAX_RETRY
.
Logical. Validate the types and values of Dash component properties? Default is TRUE
if debug == TRUE
, FALSE
otherwise. Environment variable: DASH_PROPS_CHECK
.
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
.
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.
host
/port
set the host
/port
fields of the underlying fiery::Fire web server. The block
/showcase
/...
arguments are passed along
to the ignite()
method of the fiery::Fire server.# NOT RUN {
library(dashCoreComponents)
library(dashHtmlComponents)
library(dash)
app <- Dash$new()
app$layout(
dccInput(id = "inputID", value = "initial value", type = "text"),
htmlDiv(id = "outputID")
)
app$callback(output = list(id="outputID", property="children"),
params = list(input(id="inputID", property="value"),
state(id="inputID", property="type")),
function(x, y)
sprintf("You've entered: '%s' into a '%s' input control", x, y)
)
app$run_server(showcase = TRUE)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab