Title: | Useful Functions and Modules for Shiny Apps |
---|---|
Description: | This package contains several helper functions and demos for Shiny applications. For example, there are examples for exception handling and darkmode support. |
Authors: | Karsten Weinert |
Maintainer: | Karsten Weinert <[email protected]> |
License: | file LICENSE |
Version: | 0.0.6 |
Built: | 2025-03-21 19:14:33 UTC |
Source: | https://github.com/kweinert/shiny.gems |
We may change the colors when entering dark mode. If the color is too dark, we make it a bit lighter. If the color is bright, we make it a bit darker. We use L from the HCL colorspace to determine the lightness/darkness. We use the colorspace::lighten function.
adjust_colors_to_darkmode(colors, threshold = c(30, 70), amount = 0.2)
adjust_colors_to_darkmode(colors, threshold = c(30, 70), amount = 0.2)
colors |
character vector of colors, e.g. hex codes |
threshold |
numeric, if the L is below the first value, it gets lightened, if above the second value, it gets darkened. It's possible to pass one value only. |
amount |
numeric, how much lighter/darker. Default 0.15 |
a character of the same length as colors with the (potentially) modified values.
This function uses bslib::bs_current_theme() and bslib::bs_get_variables() to query the root level colors. If dark ist TRUE, it returns the "-dark" variables. The names of the returned vector are however those used for the light mode.
bs_pal(dark = FALSE)
bs_pal(dark = FALSE)
dark |
logical, default FALSE |
Note that there is a slightly different naming convention for highlight/highlight-bg. In dark mode, these colors are stored under mark-color-dark/mark-bg-dark.
This function should be called in a reactive context.
See https://getbootstrap.com/docs/5.3/customize/color-modes/ for more information on the color mode.
a named character vector, with names body-bg, body-color, body-emphasis-color, body-secondary-color, body-secondary-bg, body-tertiary-color, body-tertiary-bg, headings-color, link-color, link-hover-color, code-color, highlight-color, highlight-bg, border-color, border-color-translucent, form-valid-color, form-valid-border-color, form-invalid-color, form-invalid-border-color
The server follows the "petite r" approach. It expects a reactiveValues parameter r. It modifies entries of the "colormode
colormode_srv(id = "colormode", r)
colormode_srv(id = "colormode", r)
id |
character, shiny id. Default "colormode" |
r |
shiny::reactiveValues object |
Currently, it is not possible to save the setting across session. This would require a user management.
The module follows a singleton design pattern, hence the id is preset to "colormode". It is strongly recommended to keep that id.
See colormode_demo to see the module in action, see colormode_srv for implementation details.
the output of shiny::radioButtons()
The UI produces a subform that can be integrated in a settings/prefences tab. Inspired by the wikipedia mobile version (Feb. 2025), it displays a radiobutton choice between "light", "dark", and "automatic". The default is "light".
colormode_ui(id = "colormode", lang = c("en", "de"), ...)
colormode_ui(id = "colormode", lang = c("en", "de"), ...)
id |
character, shiny id. Default "colormode" |
lang |
character, currently supported are "en" and "de". Default "en". |
... |
further arguments that are passed to shiny::radioButtons(). In particular, "width" and "inline" can be set this way. |
For the automatic setting, Javascript is used to determine the local hour. The Javascript code curates a variable "auto_status" that is accessible tin the server module. In particular, the Javascript updates "auto_status" when at 8pm and 6am.
The module follows a singleton design pattern, hence the id is preset to "colormode". It is strongly recommended to keep that id.
See colormode_demo to see the module in action, see colormode_srv for implementation details.
a shiny::div
Use in reactive context, i.e. inside a server function only.
exec_safely(session, expr)
exec_safely(session, expr)
session |
the app session object |
expr |
R expression to evaluate safely |