| Title: | Useful Functions and Modules for Shiny Apps |
|---|---|
| Description: | 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.9 |
| Built: | 2026-06-06 09:19:40 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 font information.
bs_fonts()bs_fonts()
This function should be called in a reactive context.
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
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 "colormode" entries
colormode_srv(id = "colormode", r, verbose = FALSE)colormode_srv(id = "colormode", r, verbose = FALSE)
id |
character, shiny id. Default "colormode" |
r |
shiny::reactiveValues object |
verbose |
logical, diagnostic message, default FALSE |
To save preferences across sessions, the lstore module is used if it is available.
The module follows a singleton design pattern, hence the id is preset to "colormode". It is strongly recommended to keep that id.
See shiny::runApp(system.file("examples/01_colormode", package="shiny.gems")) to see the module in action, see colormode_srv for implementation details.
used for its side effects
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", ...)colormode_ui(id = "colormode", ...)
id |
character, shiny id. Default "colormode" |
... |
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.
Run shiny::runApp(system.file("examples/01_colormode", package="shiny.gems")) 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 |
The server follows the "petite r" approach. It expects a reactiveValues parameter r. It modifies the "fonts" entries.
fonts_srv(id = "fonts", r, verbose = TRUE)fonts_srv(id = "fonts", r, verbose = TRUE)
id |
character, shiny id. Default "colormode" |
r |
shiny::reactiveValues object |
verbose |
logical, diagnostic message, default FALSE |
To save preferences across sessions, the lstore module is used if it is available.
The module follows a singleton design pattern, hence the id is preset to "fonts". It is strongly recommended to keep that id.
Run shiny::runApp(system.file("examples/03_fonts", package="shiny.gems")) to see the module in action.
used for its side effects (in particular r)
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 "small", "standard", and "large". The default is "standard". The font family can not be altered by the user.
fonts_ui(id = "fonts", ...)fonts_ui(id = "fonts", ...)
id |
character, shiny id. Default "fonts" |
... |
further arguments that are passed to shiny::radioButtons(). In particular, "width" and "inline" can be set this way. |
For some plotting techniques (e.g. plotting with base R), the screen density is of importance. window.devicePixelRatio is a JavaScript property that specifies the ratio between physical pixels (actual screen pixels) and CSS pixels (logical units for web design). It is a number, e.g: 1: Standard resolution (1 CSS pixel = 1 physical pixel, e.g. older screens). 2: Retina/High-DPI displays (1 CSS pixel = 2x2 physical pixels, e.g. many modern smartphones, MacBooks). 3 or higher: Ultra-high-DPI displays (e.g. 4K monitors, newer devices). fonts_ui uses Javascript to query the window.devicePixelRatio
The module follows a singleton design pattern, hence the id is preset to "fonts". It is strongly recommended to keep that id.
Run shiny::runApp(system.file("examples/03_fonts", package="shiny.gems")) to see the module in action, see fonts_srv for implementation details.
a shiny::div
The server follows the "petite r" approach. It expects a reactiveValues parameter r. It modifies "lstore" entries
lstore_srv(id = "lstore", r, verbose = TRUE)lstore_srv(id = "lstore", r, verbose = TRUE)
id |
character, shiny id. Default "lstore" |
r |
shiny::reactiveValues object |
verbose |
logical, diagnostic message, default FALSE |
The module follows a singleton design pattern, hence the id is preset to "lstore". It is strongly recommended to keep that id.
See shiny::runApp(system.file("examples/02_lstore", package="shiny.gems")) to see the module in action, see colormode_srv for implementation details.
used for its side effects
The UI is hidden and contains an initializiation javascript function.
lstore_ui(id)lstore_ui(id)
id |
character, shiny id. Default "lstore" |
See shiny::runApp(system.file("examples/02_lstore", package="shiny.gems")) to see the module in action, see colormode_srv for implementation details.
hidden div with initialization script only