Title: | Shiny wrapper with event handlers instead of reactivity |
---|---|
Description: | Shiny is a great package. Yet, for more complex projects, with much dynamic UI, I find it hard to write clean code with its reactivity paradigm. This package wraps shiny into a more traditional programming approach: - event handlers for input changes and button clicks - explicit update functions to change outputs - no explicit server function |
Authors: | Sebastian Kranz |
Maintainer: | Sebastian Kranz <[email protected]> |
License: | GPL >= 2.0 |
Version: | 2.3 |
Built: | 2024-12-05 02:55:37 UTC |
Source: | https://github.com/skranz/shinyEvents |
Add an handler to a hotkey in an aceEditor component
aceHotkeyHandler(id, fun, ..., app = getApp(), if.handler.exists = c("replace", "add", "skip")[1], session = getAppSession(app), no.authentication.required = FALSE)
aceHotkeyHandler(id, fun, ..., app = getApp(), if.handler.exists = c("replace", "add", "skip")[1], session = getAppSession(app), no.authentication.required = FALSE)
id |
name of the button |
fun |
function that will be called if button is pressed. The function will be called with the following arguments: keyId: the id assigned to the hotkey editorId: the id of the aceEditor widget selection: if a text is selected, this selection text: the text of the aceEditor widget cursor: a list with the current cursor position: row and column with index starting with 0 session: the current session object |
... |
extra arguments that will be passed to fun when the event is triggered. |
Append HTML code to a DOM element
appendToHTML(html, selector = "body", app = getApp())
appendToHTML(html, selector = "body", app = getApp())
@param initHandler a function that takes parameters session, input, output and app. It will be called from the app$server function whenever a new session is created. It allows to initialize session specific variables and e.g. store them within the app object. The passed app object is already the local copy created for the new session.
appInitHandler(initHandler, app = getApp())
appInitHandler(initHandler, app = getApp())
set the app ready to run
appReadyToRun(app = getApp(), ui = app$ui)
appReadyToRun(app = getApp(), ui = app$ui)
If app is not running, mark script to be added at the bottom and return NULL If app is already running return script directly
bottomScript(..., app = getApp())
bottomScript(..., app = getApp())
A more efficient version of button handler via global eventId handler
buttonHandler(id, fun, ..., eventId = "buttonHandlerEvent", jscript = buttonHandlerJS(eventId), app = getApp(), no.authentication.required = FALSE)
buttonHandler(id, fun, ..., eventId = "buttonHandlerEvent", jscript = buttonHandlerJS(eventId), app = getApp(), no.authentication.required = FALSE)
id |
buttonId |
fun |
the handler fun that will be called when the button is clicked |
... |
additional arguments passed to the handler fun |
Call a javascript function or method with R arguments
callJS(.fun, ..., .args = NULL, .app = getApp())
callJS(.fun, ..., .args = NULL, .app = getApp())
Add an handler to an input that is called when the input value changes
changeHandler(id, fun, ..., app = getApp(), on.create = FALSE, if.handler.exists = c("replace", "add", "skip")[1], session = getAppSession(app), no.authentication.required = FALSE)
changeHandler(id, fun, ..., app = getApp(), on.create = FALSE, if.handler.exists = c("replace", "add", "skip")[1], session = getAppSession(app), no.authentication.required = FALSE)
id |
name of the input element |
fun |
function that will be called if the input value changes. The function will be called with the arguments: 'id', 'value' and 'session'. One can assign the same handler functions to several input elements. |
... |
extra arguments that will be passed to fun when the event is triggered. |
An event handler for objects with given class
classEventHandler(class, fun, event = "change", css.locator = "", inner.js.code = NULL, shiny.value.code = NULL, eventId = paste0(class, "_class_", event, "_event"), class.prefix = ".", stop.propagation = FALSE, ...)
classEventHandler(class, fun, event = "change", css.locator = "", inner.js.code = NULL, shiny.value.code = NULL, eventId = paste0(class, "_class_", event, "_event"), class.prefix = ".", stop.propagation = FALSE, ...)
A custom event handler. Need to write correct css.locator
customEventHandler(eventId, fun, css.locator, event = "change", inner.js.code = NULL, shiny.value.code = NULL, extra.shiny.value.code = "", id = NULL, stop.propagation = FALSE, ...)
customEventHandler(eventId, fun, css.locator, event = "change", inner.js.code = NULL, shiny.value.code = NULL, extra.shiny.value.code = "", id = NULL, stop.propagation = FALSE, ...)
Directly setUI , also works for hidden UI
dsetUI(id, ui, selector = paste0("#", id, collapse = ", "), app = getApp(), ...)
dsetUI(id, ui, selector = paste0("#", id, collapse = ", "), app = getApp(), ...)
Evaluate arbitrary java script code in the client's web browser
evalJS(js, ..., .args = list(...), app = getApp())
evalJS(js, ..., .args = list(...), app = getApp())
Generate an empty shiny events app
eventsApp(set.as.default = TRUE, verbose = TRUE, single.instance = FALSE, add.events = getDefaultAppEvents(), no.events = FALSE, need.authentication = FALSE, adapt.ui = TRUE)
eventsApp(set.as.default = TRUE, verbose = TRUE, single.instance = FALSE, add.events = getDefaultAppEvents(), no.events = FALSE, need.authentication = FALSE, adapt.ui = TRUE)
If the app is already running, gets by default the local app copy corresponding to the current session
getApp(session = NULL)
getApp(session = NULL)
Get the session associated with the app object
getAppSession(app = NULL)
getAppSession(app = NULL)
Get the current session object
getCurrentSession()
getCurrentSession()
Get an input value from the current session
getInputValue(id, session = getAppSession(app), app = getApp())
getInputValue(id, session = getAppSession(app), app = getApp())
Checks whether the value of an input item has been changed (internal function)
hasWidgetValueChanged(id, new.value, on.create = FALSE, app = getApp())
hasWidgetValueChanged(id, new.value, on.create = FALSE, app = getApp())
An event handler for an object with given id
idEventHandler(id, fun, event = "change", css.locator = "", inner.js.code = NULL, shiny.value.code = NULL, eventId = paste0(id, "_id_", event, "_event"), stop.propagation = FALSE, ...)
idEventHandler(id, fun, event = "change", css.locator = "", inner.js.code = NULL, shiny.value.code = NULL, eventId = paste0(id, "_id_", event, "_event"), stop.propagation = FALSE, ...)
Transform a vector of ids to a jQuery selector string
ids2sel(ids)
ids2sel(ids)
Handler for an image click
imageClickHandler(id, fun, ..., eventId = "imageClickEvent", app = getApp(), no.authentication.required = FALSE)
imageClickHandler(id, fun, ..., eventId = "imageClickEvent", app = getApp(), no.authentication.required = FALSE)
id |
id of the HTML img object |
fun |
the handler fun that will be called when the image is clicked |
... |
additional arguments passed to the handler fun |
Can be called inside initApp handler fun is a function that gets an argument query and can do some initial dispatch depending on the query. For some reason we need to use the observer trick to get access to the query object. This means dispatch takes place after other commands in the initApp handler.
initialQueryDispatch(fun, app = getApp(), ...)
initialQueryDispatch(fun, app = getApp(), ...)
Given a tag object, extract out any children of tags$head and return them separate from the body.
moveBottomScripts(ui, reset.app = FALSE)
moveBottomScripts(ui, reset.app = FALSE)
Prpend HTML code to a DOM element
prependToHTML(html, selector = "body", app = getApp())
prependToHTML(html, selector = "body", app = getApp())
run shiny events app
runEventsApp(app = getApp(), ui = NULL, ...)
runEventsApp(app = getApp(), ui = NULL, ...)
Add an handler to an input or select that is called when the input value changes
selectChangeHandler(id, fun, ..., eventId = "selectChangeHandlerEvent", jscript = selectChangeHandlerJS(eventId), app = getApp())
selectChangeHandler(id, fun, ..., eventId = "selectChangeHandlerEvent", jscript = selectChangeHandlerJS(eventId), app = getApp())
id |
name of the input element |
fun |
function that will be called if the input value changes. The function will be called with the arguments: 'id', 'value' and 'session'. One can assign the same handler functions to several input elements. |
... |
extra arguments that will be passed to fun when the event is triggered. |
set the main ui object for the app
setAppUI(ui, app = getApp())
setAppUI(ui, app = getApp())
Can be used instead of renderDataTable. Similar to updateDataTable but no need to provide session object
setDataTable(id, value, app = getApp(), ...)
setDataTable(id, value, app = getApp(), ...)
Shiny events version of downloadHandler
setDownloadHandler(id, filename, content, contentType = NA, ..., app = getApp())
setDownloadHandler(id, filename, content, contentType = NA, ..., app = getApp())
id |
name of the downloadButton or downloadLink |
filename |
A string of the filename, including extension, that the user's web browser should default to when downloading the file; or a function that returns such a string. |
content |
A function that takes a single argument file that is a file path (string) of a nonexistent temp file, and writes the content to that file path. |
contentType |
A string of the download's content type, for example "text/csv" or "image/png". If NULL or NA, the content type will be guessed based on the filename extension, or application/octet-stream if the extension is unknown. |
Set attributes of HTML elements
setHtmlAttribute(id = NULL, attr, class = NULL, selector = paste0(c(sc("#", id), sc(".", class)), collapse = ", "), app = getApp())
setHtmlAttribute(id = NULL, attr, class = NULL, selector = paste0(c(sc("#", id), sc(".", class)), collapse = ", "), app = getApp())
Set css style of HTML elements
setHtmlCSS(id = NULL, attr, class = NULL, selector = paste0(c(sc("#", id), sc(".", class)), collapse = ", "), app = getApp())
setHtmlCSS(id = NULL, attr, class = NULL, selector = paste0(c(sc("#", id), sc(".", class)), collapse = ", "), app = getApp())
Hide HTML elements
setHtmlHide(id = NULL, class = NULL, display = "none", selector = paste0(c(sc("#", id), sc(".", class)), collapse = ", "))
setHtmlHide(id = NULL, class = NULL, display = "none", selector = paste0(c(sc("#", id), sc(".", class)), collapse = ", "))
Show HTML elements
setHtmlShow(id = NULL, class = NULL, display = "block", visibility = "visible", selector = paste0(c(sc("#", id), sc(".", class)), collapse = ", "))
setHtmlShow(id = NULL, class = NULL, display = "block", visibility = "visible", selector = paste0(c(sc("#", id), sc(".", class)), collapse = ", "))
Similar to updateImage but no need to provide session object
setImage(id, value, app = getApp(), ...)
setImage(id, value, app = getApp(), ...)
Similar to updatePlot but no need to provide session object
setPlot(id, expr, app = getApp(), update.env = parent.frame(), quoted = FALSE, ...)
setPlot(id, expr, app = getApp(), update.env = parent.frame(), quoted = FALSE, ...)
Similar to updatePrint but no need to provide session object
setPrint(id, expr, app = getApp(), ...)
setPrint(id, expr, app = getApp(), ...)
Similar to updateRHandsontable but no need to provide session object
setRHandsontable(id, value, app = getApp(), ...)
setRHandsontable(id, value, app = getApp(), ...)
Similar to updateTable but no need to provide session object
setTable(id, value, app = getApp(), ...)
setTable(id, value, app = getApp(), ...)
Similar to updateText but no need to provide session object
setText(id, value, app = getApp(), ...)
setText(id, value, app = getApp(), ...)
Similar to updateUI but no need to provide session object
setUI(id, ui, app = getApp(), ...)
setUI(id, ui, app = getApp(), ...)
If app is not running, mark script to be added at the bottom and return NULL If app is already running return script directly
singletonBottomScript(..., app = getApp())
singletonBottomScript(..., app = getApp())
Handler for an image click
svgClickHandler(id, fun, ..., eventId = if (stop.propagation) "svgClickEvent" else "svgClickEventWithPropagation", class = "clickable_svg", app = getApp(), no.authentication.required = FALSE, stop.propagation = TRUE)
svgClickHandler(id, fun, ..., eventId = if (stop.propagation) "svgClickEvent" else "svgClickEventWithPropagation", class = "clickable_svg", app = getApp(), no.authentication.required = FALSE, stop.propagation = TRUE)
id |
id of the HTML img object |
fun |
the handler fun that will be called when the image is clicked |
... |
additional arguments passed to the handler fun |
Add an handler that triggers every intervalMs milliseconds
timerHandler(id, intervalMs, fun, ..., app = getApp(), on.create = FALSE, if.handler.exists = c("replace", "add", "skip")[1], verbose = FALSE, session = getAppSession(app))
timerHandler(id, intervalMs, fun, ..., app = getApp(), on.create = FALSE, if.handler.exists = c("replace", "add", "skip")[1], verbose = FALSE, session = getAppSession(app))
id |
name of the input element |
fun |
function that will be called if the input value changes. The function will be called with the arguments: 'id', 'value' and 'session'. One can assign the same handler functions to several input elements. |
... |
extra arguments that will be passed to fun when the event is triggered. |
Update an dataTableOutput object. Can be used instead of renderDataTable
updateDataTable(session = NULL, id, value, app = getApp(session), ...)
updateDataTable(session = NULL, id, value, app = getApp(session), ...)
Shiny events version of downloadHandler
updateDownloadHandler(session = NULL, id, filename, content, contentType = NA, ..., app = getApp(session))
updateDownloadHandler(session = NULL, id, filename, content, contentType = NA, ..., app = getApp(session))
id |
name of the downloadButton or downloadLink |
filename |
A string of the filename, including extension, that the user's web browser should default to when downloading the file; or a function that returns such a string. |
content |
A function that takes a single argument file that is a file path (string) of a nonexistent temp file, and writes the content to that file path. |
contentType |
A string of the download's content type, for example "text/csv" or "image/png". If NULL or NA, the content type will be guessed based on the filename extension, or application/octet-stream if the extension is unknown. |
Update an output object. Can be used instead of renderImage
updateImage(session = NULL, id, value, app = getApp(session), ...)
updateImage(session = NULL, id, value, app = getApp(session), ...)
update an plotOutput object. Can be used instead of renderPlot.
updatePlot(session = NULL, id, expr, app = getApp(session), update.env = parent.frame(), quoted = FALSE)
updatePlot(session = NULL, id, expr, app = getApp(session), update.env = parent.frame(), quoted = FALSE)
Update an textOutput object. Can be used instead of renderPrint
updatePrint(session = NULL, id, expr, app = getApp(session), ...)
updatePrint(session = NULL, id, expr, app = getApp(session), ...)
Update an RHandsontable object. Can be used instead of renderRHandsontable
updateRHandsontable(session = NULL, id, value, app = getApp(session), ...)
updateRHandsontable(session = NULL, id, value, app = getApp(session), ...)
Update an tableOutput object. Can be used instead of renderTable
updateTable(session = NULL, id, value, app = getApp(session), ...)
updateTable(session = NULL, id, value, app = getApp(session), ...)
Update an textOutput object. Can be used instead of renderText
updateText(session = NULL, id, value, app = getApp(session), ...)
updateText(session = NULL, id, value, app = getApp(session), ...)
Update an uiOutput object. Can be used instead of renderUI
updateUI(session, id, ui, app = getApp(session), ...)
updateUI(session, id, ui, app = getApp(session), ...)
view shiny events app in RStudio viewer
viewApp(app = getApp(), ui = NULL, launch.browser = rstudio::viewer, ...)
viewApp(app = getApp(), ui = NULL, launch.browser = rstudio::viewer, ...)
Checks whether a button has been pressed again (internal function)
wasAceHotkeyPressed(keyId, value, app = getApp())
wasAceHotkeyPressed(keyId, value, app = getApp())