
Tom Mock & Shannon Haggerty | Theming Shiny and RMarkdown with {thematic} & {bslib} | RStudio
From rstudio::global(2021) Shiny X-Sessions, sponsored by Appsilon: this presentation covers the basics of how the thematic and bslib packages can be used to consistently style all the components of a shiny app at once. About Tom Mock: Thomas is involved in the local and global data science community, serving as Outreach Coordinator for the Dallas R User Group, as a mentor for the R for Data Science Online Learning Community, as co-founder of #TidyTuesday, attending various Data Science and R-related conferences/meetups, and participated in Startup Weekend Fort Worth as a data scientist/entrepreneur. About Shannon Haggerty: Shannon is on RStudio’s Customer Success team working with teams across the Life Sciences and Healthcare. In her free time, she likes to bake, hang out with her dogs, and explore new hobbies. Learn more about the rstudio::global(2021) X-Sessions: https://blog.rstudio.com/2021/01/11/x-sessions-at-rstudio-global/
image: thumbnail.jpg
Transcript#
This transcript was generated automatically and may contain errors.
Alright, thank you so much, Sam. Shannon and I are going to be talking a little bit about the thematic and bootstrap lib package today. In short, these are new packages to theme shiny as well as RMarkdown and traditional web content. And we're really excited about kind of the breadth of what this can do for theming and kind of the ease of use to get started.
So first off, introducing these packages bootstrap lib, or we're calling bslib is really just simplified theming of web content via bootstrap. Now web content meaning could be shiny could be RMarkdown and, you know, bootstrap as well as for traditional web pages. So there's a lot of power there. Thematic, on the other hand, is kind of a complementary package to bslib, which is simplified theming of our plots, meaning ggplot, lattice, base and plotly. That is kind of works in conjunction with bslib. Both of these have packaged down websites where you can find out a lot more info about them. Thematic is on CRAN already and bootstrap lib is kind of waiting in the CRAN queue. So it should hopefully be the next few days. If you want to get started with them today, you can always install the dev versions from GitHub, as you see here with this code. And a big thanks to Carson Seaver who let me adapt some of his slides for today's presentation. He's one of the core devs working on both bootstrap lib and thematic as part of our Shiny team.
Bootstrap and bslib basics
So first off, for some folks, you may have been using Shiny before, you may have been using RMarkdown before, but less people are kind of aware of using bootstrap natively. And what this is, is really just a lot of CSS and JavaScript plugins for front end web development. You can dive deeper into bootstrap and we're going to have a great presentation about SAS, which is kind of using these variables right after this presentation. But in short, this is kind of working behind the scenes in bslib and other packages and kind of provides you all these niceties without you having to know about it.
So bootstrap lib is a wrapper around custom bootstrap themes and makes it really easy to theme your documents and use things like SAS variables without having to write all this custom CSS out of the box. Now, if you do know those skills and if you gain those skills from someone like Absalon or have them working with you, they can further extend your applications with CSS or SAS variables. But again, this is really kind of a R interface and functions where you don't have to know all of it to get started. And again, you've been using components of this through Shiny and R Markdown for years, you just probably didn't know it.
So to start using bslib, it's really just a one function call. So we have a very basic Shiny up here for the UI component and we're also going to load the bslib package. So we could call a fluid page, although it could really be any component of Shiny. And this theme argument will take bs underscore theme as the argument. So this will provide kind of an interface to bslib and all the different theming components you could do like background, foreground, fonts, changing all these different components in bulk on the page without having to write individual CSS for each item. So fluid page, navbar page, bootstrap page, all these different things have the same kind of theme equals bs theme argument. And again, you may have been using this theme argument of something like Shiny themes or writing your own custom bootstrap CSS bootstrap theme is more powerful kind of in our minds because you are providing, you know, again, SAS variables and all the niceties of bootstrap in there. Again, either the power or the ease of use kind of both balanced together.
Now, something to note here is that for bootstrap users, this is moving from bootstrap three to bootstrap four, which is a more modern, more recent implementation of bootstrap. So there is some compatibility layers here that should help most shiny apps just work out of the box and upgrade to bootstrap four. However, if you have some legacy applications, you could always try version equals three here in the BS theme argument if it is kind of messing with you with your overall theming. But for new applications, just stick to bootstrap four. It's, again, a more recent version, and that'll be the good recommendation.
Now, in terms of what bootstrap three versus bootstrap four looks like, really, it's hard to tell the difference. You might see it here in terms of like this blue is slightly lighter. The fonts are a little bit different, maybe not as bold. Really, most of the benefits are behind the scenes, like just switching conversion in three to four might give you this more slightly modern looking feel, but it's not going to change the core of your application just with the base theme. But there are, again, lots of things built in there that it's worth upgrading to four.
Previewing and customizing themes
To preview a theme and actually get into some of the bulk of what these themes can do for you, you can call this new function for BS lib BS underscore theme underscore preview. This will take BS theme as an argument, and you can provide either custom themes that you built yourself or full boot swatch themes, which are essentially open source free themes that other people have created. So you can see with essentially one line of code, we've taken this basic shiny application and themed every component of it. So the fonts are different. This is perpetuated into all of the dropdowns. So the date, the select input, state ranges, the slider bars, all of those will respect the right colors, the right fonts, and other components that you're changing as well as the kind of pop ups or action buttons.
So primary, secondary warnings, danger buttons, accent colors. Again, all of this is changing kind of in bulk and together in a unified interface. So if you want to just use one of these boots, watch themes out of the box, they're going to be very powerful and should be very pretty. A lot of them have been kind of used for front end development for years, but you can also kind of customize wide portions of your app with things like SAS variables or other functions built into Bootstrap.
So again, while I've kind of hit on this a couple of times, I really want to protect people in terms of don't be scared if there's things in here that you haven't seen before. If you want to learn those, you'll be even more powerful, but they're not necessary to use it. So if you did want to dive deeper into SAS, again, there's going to be a presentation right after this about it. And there is some documentation about the SAS R wrapper for R packages that will let you kind of learn how to use those. In short, I'm not going to read this whole thing, but SAS is really a meta language on top of CSS. So if you think of CSS as changing individual components, SAS would kind of provide a more elegant or systematic syntax, letting you change multiple things at one time. This usually leads to more manageable style sheets or styling in general.
So I mentioned earlier that you can create your own theme. So we showed a pre-built boot swatch theme. Now we're using essentially the same argument. So bootstrap theme, but we're kind of mandating what specific components we want to change. So again, this theme may not be the most pretty thing to you. I kind of made it a little bit garish just to pop out. But you can see the background is gray. The foreground is black. So we have a background of gray and then the font will be in black. The primary color is this lovely purple, which goes along with our ladies. And I really do like it, maybe not with this great theme. And then we changed the base font to Francis, which is kind of, again, a very different font. And you can see that the font is perpetuated into each of the different sections.
You might also notice that I didn't have to load externally a different font. I just said, you know, pull in this Google font process and I could change it to anything I want. And it will automatically pull in the style CSS and the Google font, download it and load it in your application. So making it really easy to import Google fonts. And there's hundreds of different ones you could use here, as well as the most common ones used in web dev.
If we switch over to probably a better palette in terms of not one I created randomly, but one that's based off something like Material Dark, you can again see that we've changed all the components at once. So the background's changed, the foreground is still this lovely purple. And then we have a new font, this time Fira Sans, which is one font that I actually really do like. And again, it's perpetuating this into the sliders, into all the different components all at once. And with minimal code, I'm able to create a robustly themed application.
And with minimal code, I'm able to create a robustly themed application.
We'll go over a demo of like what all these different things are. In short, like this screenshot shows you the bulk of it. But you can use Bootstrap Preview with this to show what plots would look like, what tables would look like, notifications, different fonts. And you can actually customize it live inside the Shiny application, which is really cool. And so I will show you that right now in terms of what the preview looks like.
Live demo: theme preview
Let me pull this over here. So if I again, I'm inside RStudio. I'm just going to pull up this code chunk I have here. So Bootstrap Theme Preview. And then I'm providing it with some arguments to the Bootstrap Theme component. So if I run this, it'll load the Shiny application. I'll make that full screen. Give it a second to spin up. And now here is my kind of matrix themed Shiny application.
So again, all the different buttons work and I can change things like the font. So maybe I don't want Fioramano and I want Lato. I can close that out and it will change the fonts. I can go to accent colors and say, well, I don't want the primary color to be green. Let's do F0, F0, F0. We'll make it like a white background or primary color. Now everything's here in white. I can go over to plots and let it load for a second.
My CPU is getting some pressure from the web screen or webcasting. So the plots here are perpetuated and have those themed as well. The tables are themed to match the background notifications, all these different things. So again, as you're kind of playing around with different themes and want to change them, you have this ability to see what all the different components of an application look like without having to write all this different code just to test it. You could obviously like run up your actual production application locally and see how it looks with the theming. But this allows you to change things really quickly.
Using bslib with RMarkdown and Bootswatch
You can also use bslib with R Markdown. Now this is somewhat experimental today in terms of it is, you know, it's been focused on Shiny. I think that's really what we want to talk about today is Shiny. But you can imagine that there are also R Markdown documents that have a Shiny runtime or just R Markdown documents you're generating and you want to theme those as well. So one of the versions of R Markdown that should be merged soon allows for theme arguments. So you can change, again, the background, the foreground, the primary, the base font, very similar to what you're doing with your BS theme argument in Shiny proper.
If you don't want to write out the whole theming component yourself, you can, again, get access to all the boot swatch themes. So something like solar, you can pull that in automatically by name. And while I'm really excited about kind of the future of R Markdown, this is still more experimental in R Markdown, and you'd have to install this GitHub version of R Markdown to test it out. So maybe wait a little bit for R Markdown theming, but Shiny theming is really going to be ready in the next few days, which is very, very exciting.
Again, for using some of the boot swatch themes for Shiny proper, in the past, this was through the Shiny themes package. So you would load like solar or darkly or minty, all these different boot swatch themes, which are listed here in terms of there's a full list of, I think, two or three dozen different themes you could use. This is just going to be loaded with boot swatch and then naming it within whatever fluid component you have. There are some newer themes from boot swatch 4 that weren't available previously. And so we are switching to bootstrap 4. So if you wanted to use an older theme, you could look at older themes, but there's some newer ones as well that are really, really cool.
Awesome. OK, so we've kind of gone over a bunch of different things that are themable. I'm going to turn over to Shannon here in a second to walk through thematic, which is the complimentary package to bslib, but things that are currently themable in the next CRAN release, which, again, should be in the next few days, the Shiny package proper, R Markdown HTML document, as long as you have that specific GitHub version, DT data tables, which are perpetuated through Shiny, and then really any bootstrap compatible HTML and CSS. So you can add to this and add other style sheets that are bootstrap compatible. Some of the other things that are soon to be themable and that the dev team is working on and would love your help in terms of open source contributions or asks or different things that are working on is some of the other HTML based R Markdown output formats. So beyond just HTML document. Additionally, a lot of the HTML widgets have to be perpetuated through. So Plotly will work through GG Plotly, but in terms of Plotly proper, Reactable, which is a package similar to DT for reactive tables, and then some of the other extension packages like Shiny widgets that provide additional widgets that are not in Shiny proper.
One of the things that's fundamentally unthemable via CSS and therefore bootstrap or SAS or bslib is plot output or essentially R based graphics. So Lattice, GG Plot, Base R, those are images. They're not CSS in the classical sense where you can change the components. They're generated by R. So the way to get around that is to use thematic, which actually translates the CSS components into these R plots. So in this case, we'll use thematic to further extend the theme from bslib into things like plotting components. I'm about to be turning it over to Shannon and she's going to be walking through the thematic portions.
Introducing thematic
Yeah, thanks, Tom. So once you've gotten your Shiny application or R Markdown beautifully styled with bslib, one thing that you'll notice is that the plots are not automatically styling the way that Tom just mentioned. And so that's where you're going to want to use thematic because once you've gotten that really nice look to your Shiny application, you're going to want to pass that on to your R plots as well. So that's where thematic comes to the rescue. And what you can do inside of Shiny is use this function thematic underscore Shiny. And you see that we have that bslib theme set inside the Shiny application. And now that setting is being passed on to the plots inside of our Shiny application.
So thematic is giving those R plots the ability to translate, as Tom mentioned, the custom styling of the Shiny application into our styling and give that plot a very cohesive look with the rest of the Shiny application.
So what the thematic package in general does is that it changes the default of the R plots and you can use that thematic underscore Shiny function to turn the customization to the plots on inside Shiny. But you can also use a function called thematic underscore on to turn it on globally. And if you do that, you can also within that function set the specific background, foreground and accent colors that you want for the plots. So you can set the theming of the plot there.
And the other thing that you can do is that you can actually set the font. And we have set the font here to Indie Flower and Indie Flower is actually a Google font. What thematic does that's really cool is that it will automatically download, cache and register with R any Google font that you want to use inside your R plots.
The other thing that thematic can do is in the slide before this, we customized the theme of each of the plots, but we can also thematic will also just grab the auto theming. So it will detect what the HTML container settings are of the plot and it'll apply those to your R plot and what that looks like inside of an RStudio session. So if you've turned thematic underscore on, if you've run that function and you're in RStudio and you have a custom appearance, your RStudio theme set, it's going to auto detect that RStudio theme. So you get this actually really nice look inside your RStudio session where the plot is actually looking, again, cohesive with your RStudio session.
And then similarly, it'll do that theme thing where it will automatically detect your R Markdown settings or your Shiny settings. And so if you have used bslib underscore theme and customized your Shiny application, the auto detection, the auto theming inside thematic is going to be able to apply the settings, the aesthetics that you've set for your Shiny application to your plots.
So why this matters, it's because it's going to help you to create really cohesive, beautiful R Markdown and Shiny applications after you've spent all that time customizing the look of your applications, of your R Markdown report, thematic is going to let you easily apply those settings to your plots to, again, make everything look really nice and cohesive. You can still use traditional theme settings inside ggplot for one-off plots. But what thematic will let you do is apply that to the whole document, the whole application, or to all of the plots that you're making in RStudio if you want to. And with that, I'm going to hand it over to Tom, who's going to show you some thematic in action.
So why this matters, it's because it's going to help you to create really cohesive, beautiful R Markdown and Shiny applications after you've spent all that time customizing the look of your applications, of your R Markdown report, thematic is going to let you easily apply those settings to your plots to, again, make everything look really nice and cohesive.
Live demo: thematic in action
Perfect, thanks so much. Yeah, so as Shannon mentioned, you can use it with R Markdown, you can use it inside Shiny, and then it will also work directly within kind of just an interactive session. So here I'm going to load the thematic and ggplot libraries. I will turn thematic on just as Shannon showed in the slide deck, like we're setting basically a theme for everything in the session, and then I can just create a ggplot. So this is a very simple MT cars ggplot that everyone's looking for, and it will perpetuate what I asked it to here into the plotting panel. In terms of we have that red theme, we have the white and black background.
For me specifically, I'm using the Dracula RStudio theme, so if I didn't want to perpetuate something forward, I could actually just tell it to use whatever is in my environment. So in this case, what's going on in RStudio in the IDE theme? So I can create the plot, call the plot, and again, it will kind of use the pink and the dark purple and the black and the gray that is made up of the RStudio IDE theme I'm using.
For bslib and Shiny, you know, the Bootstrap theme preview we did will also show you a plot that automatically themes the plotting portion because it's calling thematic as well. For R Markdown, you know, we showed that you could do thematic and bslib for those items proper. So here I have a theme component inside in R Markdown doc, so it's an HTML document, and then I'm calling auto for thematic here because it's going to reflect whatever the bslib components are. So if I knit this, it will actually go through. This is, again, just the basic RMD. It's got a base R plot, a ggplot, and then a summary function. We'll let that build in the background, and I'll show you real quick what that goes to, and we'll wrap things up.
So, again, this is perpetuating all the components into the R Markdown. So the font is obviously vastly different. It's using kind of this nice purple as the primary color. The base R plot has been reflected to, you know, also be cohesive with the overall theme and ggplot as well. So, again, this idea of, like, you can theme Shiny, you can theme plots, you can theme R Markdown all by using similar syntax.
Summary
All right, so, yeah, thank you for hanging out with us for the past about 25, 30 minutes. In summary, you can use Bootstrap Lib to theme Shiny and R Markdown. This will upgrade to Bootstrap 4 by default. If you're having apps breaking, you can always downgrade to version equals 3. You can use Boot Swatch or custom design for your own custom themes. You can use Bootstrap underscore theme preview or Bootstrap underscore themer to quickly preview and update themes interactively. There's a full-blown article about theming with bslib, a link here that we'll share in a second. And you can use thematic for easier theming of R plots in conjunction with Bootstrap Lib. So R Markdown, Base R, ggplot, Lattice, all these different components can be perpetuated with the theming through.
I'd really like to thank Carson Siebert for letting us adapt part of his slides. And thank you to Absalon for bringing us in today. They're going to have a lot of wonderful presentations about SAS and more theming and their semantic package, which is a way of getting further away from what traditional Shiny apps look like into really, really custom, really, really cool applications. And I will stop talking and thanks for your time.

