R Shiny bslib – How to Work With Bootstrap Themes in Shiny
Posted on September 21, 2022 by Dario Radečić in R bloggers | 0 Comments
[This article was first published on Tag: r - Appsilon | Enterprise R Shiny Dashboards , and kindly contributed to R-bloggers ]. (You can report issue about the content on this page here )
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
R Shiny bslib is a package that makes working with Bootstrap themes a breeze. Let’s face it – Shiny apps look dull and mainstream by default. Not everyone has a keen eye for design, and sometimes spending days or weeks playing around with CSS isn’t an option. You need something that will look better, or at least brandable right now.
If that sounds like you, look no further than Bootstrap and bslib. Today you’ll learn all about it, from the basics to creating custom themes for R Shiny applications.
Could your Shiny app benefit from a little CSS? Here’s how to use TailwindCSS in R Shiny .
Table of contents:
Summary of R Shiny bslib
What is Bootstrap and R Shiny bslib?
In the most simple terms, Bootstrap is a free and open-source web development framework. It enables you to make responsive and mobile-first websites and apps with ease. Bootstrap provides a collection of styles you can use out of the box. For example, once you have Bootstrap configured, you only need to assign class attributes to HTML elements in order for styles to apply.
So, why would you want that? By using a prebuilt CSS framework, you can save hours, days, or even weeks by not writing the entire application style from scratch. Bootstrap is easy to use and comes with a predefined grid system, so your website will be responsive and mobile-friendly immediately (read: less or no media queries).
Now, Bootstrap isn’t a be-all-end-all solution. It provides you with everything you need to get started, but you should still know how to change colors, fonts, and others by yourself.
When it comes to R and R Shiny, you have two options if you want to use Bootstrap. You can either download the framework and embed it into your app, or you can use an R package such as bslib to do most of the heavy lifting for you.
But what is bslib? This R package provides tools for customizing Bootstrap themes directly in R, and it primarily targets Shiny apps and R Markdown documents.
To use it, you’ll first need to install it:
Let’s see how you can use bslib next.
How to Make Bootstrap Themes with R Shiny bslib
What’s neat about bslib is that you don’t have to download any specific version of Bootstrap manually – you can specify versions 3, 4, or 5 straight from R.
To get your hands dirty with your first theme, simply store the results of a bs_theme() function call to a variable. The only parameter we’ll specify for now is version, and we’ll set it to 5. It instructs bslib to use the latest version of Bootstrap, or Bootstrap 5: