Package 'shiny.gems'

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

Help Index


Adjust colors based on lightness

Description

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.

Usage

adjust_colors_to_darkmode(colors, threshold = c(30, 70), amount = 0.2)

Arguments

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

Value

a character of the same length as colors with the (potentially) modified values.


Bootstrap Font Information

Description

This function uses bslib::bs_current_theme() and bslib::bs_get_variables() to query the root level font information.

Usage

bs_fonts()

Details

This function should be called in a reactive context.

Value

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


Bootstrap Color Palette

Description

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.

Usage

bs_pal(dark = FALSE)

Arguments

dark

logical, default FALSE

Details

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.

Value

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


colormode_ui/srv is a shiny module for managing colors; in particular enabling dark mode.

Description

The server follows the "petite r" approach. It expects a reactiveValues parameter r. It modifies "colormode" entries

Usage

colormode_srv(id = "colormode", r, verbose = FALSE)

Arguments

id

character, shiny id. Default "colormode"

r

shiny::reactiveValues object

verbose

logical, diagnostic message, default FALSE

Details

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.

Value

used for its side effects


colormode_ui/srv is a shiny module for managing colors; in particular enabling dark mode.

Description

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".

Usage

colormode_ui(id = "colormode", ...)

Arguments

id

character, shiny id. Default "colormode"

...

further arguments that are passed to shiny::radioButtons(). In particular, "width" and "inline" can be set this way.

Details

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.

Value

a shiny::div


Shiny Version of TryCatch

Description

Use in reactive context, i.e. inside a server function only.

Usage

exec_safely(session, expr)

Arguments

session

the app session object

expr

R expression to evaluate safely


fonts_ui/srv is a shiny module for managing fonts and fonts size

Description

The server follows the "petite r" approach. It expects a reactiveValues parameter r. It modifies the "fonts" entries.

Usage

fonts_srv(id = "fonts", r, verbose = TRUE)

Arguments

id

character, shiny id. Default "colormode"

r

shiny::reactiveValues object

verbose

logical, diagnostic message, default FALSE

Details

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.

Value

used for its side effects (in particular r)


fonts_ui/srv is a shiny module for managing fonts and fonts size

Description

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.

Usage

fonts_ui(id = "fonts", ...)

Arguments

id

character, shiny id. Default "fonts"

...

further arguments that are passed to shiny::radioButtons(). In particular, "width" and "inline" can be set this way.

Details

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.

Value

a shiny::div


lstore_ui/srv is a shiny module saving/restoring data in the browser's local store

Description

The server follows the "petite r" approach. It expects a reactiveValues parameter r. It modifies "lstore" entries

Usage

lstore_srv(id = "lstore", r, verbose = TRUE)

Arguments

id

character, shiny id. Default "lstore"

r

shiny::reactiveValues object

verbose

logical, diagnostic message, default FALSE

Details

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.

Value

used for its side effects


lstore_ui/srv is a shiny module saving/restoring data in the browser's local store

Description

The UI is hidden and contains an initializiation javascript function.

Usage

lstore_ui(id)

Arguments

id

character, shiny id. Default "lstore"

Details

See shiny::runApp(system.file("examples/02_lstore", package="shiny.gems")) to see the module in action, see colormode_srv for implementation details.

Value

hidden div with initialization script only