Description Super class Active bindings Methods
Web Application object based on fiery::Fire
via Fire
and
contains the following modifications:
Takes advantage of httpuv::staticPath()
via Fire
to serve,
assets which "happens entirely within the I/O thread, so doing so will not
block or be blocked by activity in the main R thread."
Incorporates the routr
package for routing, instead of relying on the
user to create it separately.
Adds load testing, profiling, and benchmarking functionality for the app itself and for individual routers.
Methods return the self
object to allow method chaining.
Renames some methods to conform to some sort of prefixing structure for better discovery.
Lifecycle (lifecycle_
)
Lifecycle handlers handle starting, stopping, and other development utilities for web applications.
Settings (set_
)
set clients, headers, and other things that are globally applicable to this application instance.
Data (data
)
arbitrary data that can be set or retrieved from the object data store.
Handlers (handle_
)
Handlers are functions that run on a specific event trigger.
Routers (route_
)
create handlers for specific virtual paths.
Static Paths (static_
)
create static paths for serving static files from disk.
Websockets (ws_
)
WebSocket is a bi-directional communication protocol that is very useful when you need to persistently poll or receive new information.
It's also useful to set the
ID with set_clients
so that they can easily be identified.
Plugins (plugin_
)
Fiery functionality allows you to add plugins. See
here for more
information on how to use plugins. The interface implemented is similar
except that we have access to the whole object not just to the contained
Fire
object.
webtools::Configurable
-> App
plugins
read-only field for plugins attached
fire
read-only field for the fire object
handles
read-only field for the handles attached
routers
read-only field for the routers attached
statics
read-only field for the paths attached
data
alternative way of accessing application data
sockets
read-only list of active websockets
events
help information
new()
Create a new Application
App$new( ..., host = "0.0.0.0", port = 3838L, root = "", headers = NULL, client_id_converter = function(request) { request$trust <- TRUE return(request$ip) }, path_extractor = function(msg, bin) "/", refresh_rate = 0.001, refresh_rate_nb = 0.001, trigger_dir = NULL, access_log_format = common_log_format, logger = if (interactive()) { logger_console() } else { logger_null() } )
...
(Plugin)s to attach to this Application Instance
host
(str) IP address; defaults to all addresses.
port
(int) port number from 0 to 65535.
root
(str) path at which fiery will serve requests
headers
(lst) global header that will be set on all responses
client_id_converter
(fun) function that converts a request into a client ID
path_extractor
(fun) function that converts a message to a path on which to dispatch
refresh_rate
(num) blocking refresh rate for run cycles
refresh_rate_nb
(num) non-blocking refresh rate for run cycles
trigger_dir
(path) directory in which you can save RDS files
of lists where the external-event
trigger will be triggered with
the list as arguments. See
events.
access_log_format
(str/fn) provider of the logging service.
logger
(fn) logger for this application.
browse()
Browse to the App URL
App$browse()
finalize()
Finalizer
App$finalize()
lifecycle_start()
Start the application
App$lifecycle_start(..., block = FALSE, browse = FALSE, silent = FALSE)
...
(args) passed to the start
or resume
handler
block
(flag) blocking server
browse
(flag) open a browser after starting
silent
(flag) do not log to console
lifecycle_stop()
Stop the application
App$lifecycle_stop()
lifecycle_is_running()
Checks if the app is still running
App$lifecycle_is_running()
set_client_id_converter()
Set client converter
App$set_client_id_converter(client_id_converter)
client_id_converter
(fun) takes request and returns client ID
set_path_extractor()
Set the path extractor
App$set_path_extractor(extractor)
extractor
function that takes the message and returns a path
set_headers()
Set the heaers to be sent with every response
App$set_headers(..., .list = NULL)
...
(arg) key-value pairs
.list
(lst) provide the values as a list
set_logger()
Set the logger function
App$set_logger(logger)
logger
(fun) logger function; there are some presets:
logger_null()
- only error
, warning
, message
events
logger_console()
- everything to console
logger_file()
- everything to file
logger_switch()
- file or console dependeing on need
Can also be any function that takes arguments: event
, message
,
request = NULL
, time = Sys.time()
, and ...
.
set_data()
set data inside the app object; list merge semantics
App$set_data(name, value)
name
(str) name of the data
value
(arg) key value data
get_data()
get data inside the app object
App$get_data(name)
name
(str) name of the data
clear_data()
clear all data in side the app object
App$clear_data()
log()
Send an application log
App$log(event, message, request = NULL, ..., .envir = parent.frame())
event
(str) event attached
message
(str) glue string
request
(req) reqres::Request object
...
(arg) extra arguments
.envir
(env) calling environment
handle()
Attach a handler to an event
App$handle(event, name, handler, ..., position = NULL)
event
(str) name of the event to respond to
name
(str) human-readable name for easy identification
handler
(fun) handler for the event
...
(arg) key value pairs of attributes to attach to handler
position
(int) position in the handler stack; default last
handle_find()
Find a handler to an event
App$handle_find(..., discard = FALSE)
...
(arg) extra arguments
discard
(flg) if TRUE, then selected is discarded instead of kept
handle_remove()
Remove a handler
App$handle_remove(..., discard = FALSE)
...
(arg) extra arguments
discard
(flg) if TRUE, then selected is discarded instead of kept
handle_lifecycle()
Lifecycle Handler
Triggered once per server invocation. Return value is ignored.
Arguments
server
- application itself
...
- extra arguments
Events
start/resume
- app is started, but not running; resume
is when
app_start(resume = TRUE)
is called.
end
- when app is stopped using app_stop()
.
App$handle_lifecycle( event = c("start", "resume", "end"), name, handler, ..., position = NULL )
event
(str) name of the event to respond to
name
(str) human-readable name for easy identification
handler
(fun) handler for the event
...
(arg) key value pairs of attributes to attach to handler
position
(int) position in the handler stack; default last
handle_cycle()
Cycle Handler
Triggered for every event loop. Return value is ignored.
Arguments
server
- application
Events
start
- beginning of the loop; pre-logging.
end
- end of the loop; post-logging.
App$handle_cycle( event = c("start", "end"), name, handler, ..., position = NULL )
event
(str) name of the event to respond to
name
(str) human-readable name for easy identification
handler
(fun) handler for the event
...
(arg) key value pairs of attributes to attach to handler
position
(int) position in the handler stack; default last
handle_request()
Request Handler
Triggered by actual request made to the web server.
Arguments
server
- application itself
id
- client identifier
request
- request itself
arg_list
- for request
event, the return value from the before
handler, if any.
response
- for after
event, the response object
Events
header
- when HTTP headers are received; return FALSE
to
short-circuit; return TRUE
to continue processing
before
- before the request; for injecting session specific data to
request handler; return value is passed to request
handler
as arg_list
request
- main request handling is done here; should return a valid
response, otherwise error 500 is raised.
after
- after the request; can be used to inspect the response
(but not modify it) before sending to the client;
return value is discarded
App$handle_request( event = c("header", "before", "request", "after"), name, handler, ..., position = NULL )
event
(str) name of the event to respond to
name
(str) human-readable name for easy identification
handler
(fun) handler for the event
...
(arg) key value pairs of attributes to attach to handler
position
(int) position in the handler stack; default last
handle_ws()
Websocket Event Handler
Arguments
server
- application itself
id
- client identifier
request
- request used to establish the websocket connection
message
- message itself
arg_list
- for message
event, the return value from the before
handler, if any.
binary
- for message
event, indicating if it is binary.
response
- for after
event, the response object
Events
before
- before the request; for decoding the message before passing
it through; return value is passed to request
handler
as arg_list
message
- main message handling is done here; return value discarded
after
- after the request; can be used to inspect the response
(but not modify it) before sending to the client;
return value is discarded
send
- a websocket message is sent; return value discarded
App$handle_ws( event = c("before", "message", "after", "send", "closed"), name, handler, ..., position = NULL )
event
(str) name of the event to respond to
name
(str) human-readable name for easy identification
handler
(fun) handler for the event
...
(arg) key value pairs of attributes to attach to handler
position
(int) position in the handler stack; default last
handle_trigger()
handle Triggers an event for the handlers
App$handle_trigger(event, ..., check = TRUE)
event
(str) event name
...
(arg) args passed to handler
check
(flg) check whether this is protected
router()
Adds a route handler
App$router( path, handler, ..., name = path, methods = "get", attach = "request", priority = 0L, replace = FALSE )
path
(str) endpoint
handler
(fun) handler; must have arguments
request
, response
, keys
, ...
.
...
(dta) extra metadata to identify thion_atts route.
name
(str) human-readable friendly name
methods
(chr) combination of GET, POST, PUT, PATCH, DELETE
attach
(str) event to attach. defaults to request
.
priority
(int) relative positioning of the handler; lower will be executed first.
replace
(flg) whether or not to replace same name or error out
router_find()
Find routers based on metadata
App$router_find( ..., attach = c("request", "message", "header"), discard = FALSE )
...
(sel) selector expressions
attach
(str) whether to use the request, message, or header stack
discard
(flg) if TRUE, then selected is discarded instead of kept
router_remove()
Remove routers based on metadata
App$router_remove( ..., attach = c("request", "message", "header"), discard = FALSE )
...
(arg) extra arguments
attach
(str) whether to use the request, message, or header stack
discard
(flg) if TRUE, then selected is discarded instead of kept
static()
Adds a route for server static files
App$static( path, file, ..., name = path, index_html = TRUE, fallthrough = FALSE, html_charset = "utf-8", headers = list(), validation = character(0), replace = FALSE )
path
(url) URL path to serve
file
(pth) directory to serve on that path
...
(dta) extra metadata to identify this static path.
name
(str) human-readable friendly name
index_html
(flg) whether or not to serve when index.html exists
fallthrough
(flg) if not available, use R callback
html_charset
(str) html charset, defaults to UTF-8
headers
(lst) headers that are included with the response
validation
(chr) character vector of headers needed
replace
(flg) if already exists, overwrite
static_find()
Find static paths based on metadata
App$static_find(..., discard = FALSE)
...
(arg) extra arguments
discard
(flg) if TRUE, then selected is discarded instead of kept
static_remove()
Remove static paths based on metadata
App$static_remove(..., discard = FALSE)
...
(arg) extra arguments
discard
(flg) if TRUE, then selected is discarded instead of kept
ws_send()
Send a websocket message
App$ws_send(message, id)
message
(lst) websocket message
id
(str) client identifier; all clients if missing (for send)
ws_close()
Close a websocket connection
App$ws_close(id)
id
(str) client identifier
plugin_attach()
Attach a plugin
App$plugin_attach(plugin, ..., force = FALSE, bind = TRUE, clean = TRUE)
plugin
(Plugin) instance
...
(arg) passed to the plugin's on_attach
method
force
(flg) force to attach even if there is already existing
bind
(flg) add an active binding to fetch the plugin itself
clean
(flg) remove the attach methods once they have already been run to clean up the interface
plugin_detach()
Detach a plugin
App$plugin_detach(name, ...)
name
(str) name of the plugin to detach
...
(arg) extra arguments
plugin_detach_all()
Detach all plugins
App$plugin_detach_all(...)
...
(arg) extra arguments
plugin_has()
Check if a plugin of a class is attached
App$plugin_has(class)
class
(str) class of the plugin
plugin_get()
Check if a plugin of a class is attached
App$plugin_get(class)
class
(str) class of the plugin
print()
print method
App$print()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.