bslib
variables to
stylize shinylibrary(bslib, warn.conflicts = FALSE)
library(rlang)
library(dplyr, warn.conflicts = FALSE)
In this example we will start by adding styles to a ‘Figma’ file.
Then we will use these variables to define the high level
bslib
variables used for stylizing Shiny applications.
In order to get style data from a ‘Figma’ file using the REST API the following steps should followed:
Create a Figma team
Create a Figma file and move it to the Figma team
Add your favorite styles (they should appear in the Design selection at the top right of your screen)
Click Assets on the top left and select the book icon to publish your styles.
bslib
The bslib
library allows to manipulate the theme of a
shiny application using bootstrap variables. In these example we
demonstrate how to use high level bootstrap variables to stylize your
application.
The high level bootstrap variables are the arguments of the functions
bslib::bs_theme
.
::fn_fmls_names(bslib::bs_theme) %>%
rlangtail(-3)
#> [1] "bg" "fg" "primary" "secondary" "success"
#> [6] "info" "warning" "danger" "base_font" "code_font"
#> [11] "heading_font" "font_scale"
By using these names as style names into a ‘Figma’ file we can directly manipulate the theme of the Application.
Open the ‘Figma’ file and insert the styles of your choice using
blib
variable names as printed from the previous chunk. On
the right hand side of your screen you should get a View similar to the
following:
After defining some of the variables that you want to modify navigate
to the top left and select Assets. Then, push the book button to publish
the changes on the file. Now the file is ready to be accessed from an R
session using Rigma
.
We can access published data via ‘Figma’ REST API. Find the file id in the URL of the ‘Figma’ file. Then, collect style data:
<- "sFHgQh9dL6369o5wrZHmdR" #add your own file_key
file_key <- file_key %>%
style_data ::get_file_styles() %>%
Rigma::as_design_tibble() Rigma
The color is encoded in thumbnail URLs provided by the API
(alternatively you can get color metadata from node ids and using the
Rigma::get_file_nodes()
method). For this tutorial, we
include an example function that collects color data using the thumbnail
URLs, add_color()
. We can get high level bslib
color variables by executing:
<- style_data %>%
bslib_palette ::add_color() %>%
Rigma::extract_bslib_palette() Rigma
Metadata about text can be extracted using the following function:
<- Rigma::text_data_from_styles(style_data)
text_data
#get base_font Figma style variable - see also snapshot
<- text_data %>%
base_font filter(name == "base_font") %>%
pull(fontFamily) %>%
font_google()
bs_theme
objectCollecting the font and color data we can create the
bs_theme
object that can be used to stylize a shiny app
<- bslib_palette %>%
bslib_vars append(
list(base_font = base_font)
)<- exec(bs_theme, !!!bslib_vars) rigma_theme
and the shiny app should read
library(shiny)
<- navbarPage(
ui theme = rigma_theme,
...
)shinyApp(ui, function(...) {})