Introduction to bscui

library(knitr)
opts_chunk$set(
   include=TRUE,
   echo=TRUE,
   message=TRUE,
   warning=TRUE,
   cache=FALSE,
   cache.lazy=FALSE
)
library(bscui)
library(xml2)
library(dplyr)
library(readr)
library(stringr)
library(glue)
library(scales)

```{js, echo=FALSE} function toggleCollapse(id) { var content = document.querySelector('#'+id); if(content.style.display === 'none' || content.style.display === ''){ content.style.display = 'block'; }else{ content.style.display = 'none'; } }

```{css, echo=FALSE}
/* Hide the content of the collapsible div by default */
.collapsible {
   display: none;
   border: solid 1px black;
   background-color: #FAFA000F;
   margin: 5px 0px 5px 0px;
   padding: 5px;
}

.collapse-button {
   border: solid 1px black;
   border-radius: 3px;
   background-color: #CFCFCF;
   margin-top: 10px;
   margin-bottom: 20px;
}

The aim of the 'bscui' R package is to render any SVG image as an interactive figure and convert identified elements into an actionable interface. This figure can be seamlessly integrated into 'rmarkdown' and 'Quarto' documents, as well as 'shiny' applications, allowing manipulation of elements and reporting actions performed on them.

Here are the main features of 'bscui' figures:

The main motivation behind the development of this package was to be able to leverage SVG images kindly shared in public knowledge resources (e.g., EBI anatomograms, SwissBioPics, or wikipathways ) to display and browse information in the relevant contexts.

The package comes with several pre-processed examples used to demonstrate the main available features described in this document.

Installation and requirements

The 'bscui' R package is available on CRAN.

install.packages('bscui')

Development version from github

The development version is available in github.

## Dependencies
install.packages("htmlwidgets")
## Install from github
devtools::install_github("patzaw/bscui")

Vignette requirements

The following packages are not dependencies of 'bscui' strictly speaking but are required to run code examples discussed in this document.

library(bscui)
library(xml2)
library(dplyr)
library(readr)
library(stringr)
library(glue)
library(scales)
library(reactable)
library(reactable.extras)


::: {#sessionInfo class="collapsible" style="margin-top:10px; margin-bottom:10px;"}

Built on r Sys.Date()

sessionInfo()

:::

Building figures

The main function of the package is bscui(). It takes as main argument a character string with SVG code. The use of xml2 to read SVG files is not mandatory but it can be useful to validate the SVG and manipulate it before displaying it.

Simple example: Animal cells

This example relies on a figure of animal cells taken from SwissBioPics.

svg <- xml2::read_xml(system.file(
   "examples", "Animal_cells.svg.gz",
   package="bscui"
))

The simplest way to display the figure is shown below.

figure <- bscui(svg)
figure

The figure can be grabbed with mouse and enlarged or shrunk using the mouse wheel. Clicking on the button at the top-left corner of the figure displays a menu with various functions, including resetting the view and exporting the figure in SVG or PNG format. Several configuration choices are made by default but can be changed, such as the zoom range or the width of the menu.

Defining UI elements

User Interface (UI) elements are provided as a data frame with the following columns:

Information about the different part of a cell were taken from UniProt.

info <- readr::read_tsv(system.file(
   "examples", "uniprot_cellular_locations.txt.gz",
   package="bscui"
), col_types=strrep("c", 6)) |> 
   mutate(id = str_remove(`Subcellular location ID`, "-"))

The title feature can take simple text or a valid html element as shown below.

ui_elements <- info |> 
   mutate(
      ui_type = "selectable",
      title = glue(
         '<div style="width:300px; height:100px; overflow:auto; padding:5px;',
         'font-size:75%;',
         'border:black 1px solid; background:#FFFFF0AA;">',
         "<strong>{Name}</strong>: {Description}",
         "</div>",
         .sep=" "
      )
   ) |>
   select(id, ui_type, title)
ui_elements
figure <- figure |> 
   set_bscui_ui_elements(ui_elements)
figure

Setting element styles

The style of the elements can be changed with the set_bscui_styles() function. The styles are are provided in a data frame with an id column, providing the element identifier, and one column per style. Column names should correspond to a style name in camel case (e.g., "strokeOpacity"). This function can be called several times to change different elements or different options.

figure <- figure |> 
   set_bscui_styles(
      bind_rows(
         info |>
            filter(Name == "Cytosol") |>
            mutate(fill = "#FF7F7F"),
         info |>
            filter(Name == "Nucleoplasm") |>
            mutate(fill = "#7F7FFF")
      ) |> 
         select(
            id, fill
         )
   ) |> 
   set_bscui_styles(
      info |>
         filter(Name == "Endosome") |>
         mutate(stroke = "yellow", strokeWidth = "2px") |> 
         select(id, stroke, strokeWidth)
   )
figure

Setting element attributes

Element attributes can also be changed with the set_bscui_attributes() function. The example below shows how to focus on the nucleus and its content starting from the original figure, by setting the "display" attribute of all other elements to "none". Also the size of the nucleolus is slightly increased.

nucleus_part <- c(
   "SL0191", "SL0190", "SL0182", "SL0188", "SL0494", "SL0180",
   "SL0031", "SL0465", "SL0127", "SL0186"
)
figure <- figure |>
   set_bscui_attributes(
      info |>
         filter(
            !id %in% nucleus_part
         ) |>
         mutate(display="none") |>
         select(id, display)
   ) |> 
   set_bscui_attributes(tibble(id="sib_copyright", display="none")) |>
   set_bscui_attributes(tibble(id="SL0188", transform="scale(1.8 1.8)")) |> 
   set_bscui_attributes(
      tibble(id="SL0188", transform="translate(-237 -202)"),
      append=TRUE
   )
figure |> 
   set_bscui_options(
      show_menu=FALSE, zoom_min=1, zoom_max=1, clip=TRUE,
      hover_width=1
   )

In the example above the set_bscui_options() function is used to hide the menu and to disable view changes (zoom and pan). Other options can be changed with this function.

Save and export widget

The htmlwidgets::saveWidget() function can be used to save the figure in an interactive html file.

bscui(svg) |>
   htmlwidgets::saveWidget(file = "figure.html")

And the export_bscui_to_image() is used to export the figure as an image.

bscui(svg) |>
   set_bscui_options(show_menu=FALSE) |> 
   export_bscui_to_image(file = "figure.png", zoom=6)

This function relies on the 'webshot2' package and requires the Chrome browser or other browsers based on Chromium, such as Chromium itself, Edge, Vivaldi, Brave, Opera or Thorium. The following code chunk shows how to rely on Microsoft Edge.

Sys.setenv(
    "CHROMOTE_CHROME" = "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe"
)

Mapping data: Wikipathways

The following example demonstrates how to use 'bscui' to map numeric data with colors on the figure. In this example, gene expression data are used to color elements of the Principal pathways of carbon metabolism taken from WikiPathways.

svg <- xml2::read_xml(system.file(
   "examples", "WP112.svg.gz",
   package="bscui"
))
info <- read_tsv(system.file(
   "examples", "WP112.txt.gz",
   package="bscui"
), col_types="c")

Gene expression data were taken from a publication describing the effects of the provided nitrogen sources on baker's yeast gene expression (Godard et al. 2007). The supplementary Table S1 provides a list of genes displaying significant variations in expression under at least one tested nitrogen condition compared to urea.

deg <- read_tsv(system.file(
   "examples", "DEG-by-nitrogen-source_MCB-Godard-2007.txt.gz",
   package="bscui"
), col_types=paste0(strrep("c", 3), strrep("n", 41)))

Below, we focus on the ALANINE condition and extract the M value comparing the expression of genes when this amino-acid is used as the nitrogen source instead of urea in yeast culture: $M = log_2\left(Expression\,on\,alanine \over Expression\,on\,urea\right)$

condition <- "ALANINE"
toTake <- c("ORF", paste(condition, "M"))
cond_deg <- deg |> 
   select(all_of(toTake)) |> 
   setNames(c("ensembl", "M")) |> 
   filter(!is.na(M))

We define a color scale on M values and define the style of the SVG elements accordingly.

col_scale <- col_numeric(
   "RdYlBu", domain=range(cond_deg$M), reverse=TRUE
)
styles <- cond_deg |> 
   mutate(
      fill=col_scale(M)
   ) |> 
   inner_join(select(info,id, ensembl), by="ensembl") |> 
   select(id, fill)

Finally, we create tooltips on SVG elements showing the type of biological object and the M value when available.

elements <- info |> 
   mutate(
      ui_type="selectable",
      bg = case_when(
         category == "GeneProduct" ~ "#FDFDBD",
         category == "Metabolite" ~ "#BDFDFD",
         TRUE ~ "white"
      )
   ) |> 
   left_join(cond_deg, by="ensembl") |> 
   mutate(
      de = ifelse(
         !is.na(M),
         glue("log2({condition}/UREA) = {round(M,2)}<br/>"),
         ""
      )
   ) |> 
   mutate(
      title = glue(
         '<div style="padding:5px;border:solid;background:{bg}">',
         '<strong>{name}</strong><br/>',
         '{de}',
         '<a href={href} target="_blank">{category} information</a>',
         '</div>'
      )
   ) |> 
   select(id, ui_type, title)

And we use this information to create the figure.

bscui(svg) |> 
   set_bscui_ui_elements(elements) |> 
   set_bscui_styles(styles)

'shiny' applications

The following example is used to describe the capabilities of 'bscui' within 'shiny'. It relies on the human female anatomical diagram taken from the EBI gene expression group.

shiny::runApp(system.file("examples", "shiny-anatomogram", package = "bscui"))

Anatomogram 'shiny' app.

The application is divided in four main parts:

Building and inserting the figure (P1)

Building the figure and inserting it in 'shiny' user interface is straightforward and relies on the bscuiOutput() and renderBscui() functions:

ui <- fluidPage(
   bscuiOutput("anatomogram")
)
server <- function(input, output, session){
   output$anatomogram <- renderBscui({
      bscui(svg)|>
         set_bscui_ui_elements(ui_elements)
   })
}

UI elements need to be defined with set_bscui_ui_elements() to make the figure interactive and to allow 'shiny' interactions.

Interactions (P2)

The following information are exposed to 'shiny' from the 'bscui' widget:

bscuiID is used to refer to the figure output id, "anatomogram" in the example above.

Modifying the figure (P3)

Creating a "bscui_Proxy" object allows figure modification without redrawing it. It's done by calling the bscuiProxy() within the sever function:

server <- function(input, output, session){
   output$anatomogram <- renderBscui({
      bscui(svg)|>
         set_bscui_ui_elements(ui_elements)
   })
   anatomogram_proxy <- bscuiProxy("anatomogram")
}

The following functions can then be used to modify the figure:

Finally, the get_bscui_svg() function makes the updated figure SVG available via input$bscuiID_svg (input$anatomogram_svg in the example).

Updating figure elements (P4)

The example above relies on the 'reactable' and 'reactable.extras' packages to display available elements (organs) and change their properties. The following functions are applied to a "bscui_Proxy" (anatomogram_proxy in the example above).

Additional notes

The functions of the 'bscui' package do their best to handle and modify SVG elements as accurately as possible. However, depending on the structure of the SVG it can be more or less successful. Simple and unnested structures should be preferred when building SVG specifically for this use.

However, the aim of the 'bscui' package is also to leverage SVG from existing sources as shown in all the examples described here. The vignette Preparing SVG: examples, tips and tricks describes how the SVG used in 'bscui' package examples were prepared. This experience can be used when preparing SVG from new sources.



Try the bscui package in your browser

Any scripts or data that you put into this service are public.

bscui documentation built on June 8, 2025, 11:01 a.m.