How to Use CSS to Style Your R Shiny Dashboards
Updated: February 16, 2022.
How many times have you opened a website just to find yourself immediately closing it due to terrible visuals? Too many. As a developer, it’s your responsibility to make your apps easy to look at. In this article, you’ll learn how to use CSS to style your R Shiny dashboards.
CSS is a powerful tool for enhancing your R Shiny apps, and it’s also pretty easy to learn. The hardest part is coming up with amazing UI designs, so that’s where you’ll spend most of your time.
Are you a CSS power user? Read this article to learn how to use SASS in your R Shiny apps.
Table of contents:
- What is CSS and Why Should You Care as an R Shiny Developer?
- Why use CSS? Before and After Comparison
- How to use CSS in R Shiny?
Want to learn more about CSS in Shiny and you prefer video? You’re in luck:
What is CSS and Why Should You Care as an R Shiny Developer?
CSS stands for Cascading Style Sheets. It describes how HTML elements should be displayed on a website or a web application. Every CSS document (either in a separate
.css file or embedded in the head element of an HTML document) is a series of instructions – or statements.
Every CSS statement consists of two parts – selectors for the elements, and their respective set of rules. The part outside of the bracket indicates which elements will be affected, and the part inside of the bracket enumerates the rules that will be applied.
The concept is best summarized in the following image:
The biggest benefit of using CSS is that you can write a set of rules once and apply it to multiple HTML documents. For example, in Image 1 you’ve seen an element
.vestColor. You can add a class attribute
.vestColor to any HTML tag in any HTML document and the same styles will apply, as long as you’ve added a link to that CSS document.
Easy, right? Right.
Frontend developers aren’t the only IT professionals who should care about CSS. Every R Shiny developer should know the basics of CSS/SCSS too. Nobody wants to use an unstyled dashboard – it’s a terrible experience for your users. Make sure to use CSS wisely to highlight the elements you want, structure the application, and also make it mobile-friendly.
Why use CSS? Before and After Comparison
You now know what CSS is and why it’s important in R Shiny dashboards and apps. But what about real-world examples? We’ll show you the difference between a dashboard with and without CSS stylings. Here’s a dashboard of choice – our Shiny Enterprise Demo Dashboard – first without CSS:
The charts look okay, but only because they’re styled and configured in a separate R file, and not in CSS. The other elements, such as dropdowns, text, and icons look horrible. Adding stylings helps drastically:
We ensure you – it’s the same dashboard. Clearly, CSS allows us to build much more visually appealing products. CSS is as old as HTML itself but it still remains highly useful, introduces order into the development process, and saves time.
How to use CSS in R Shiny?
The question now remains – How can I add CSS to my R Shiny dashboards? There are currently three ways you can add CSS to your Shiny apps:
- Add stylings directly to HTML tags
- Add CSS to HTML header
- Add stylesheets to the
The last option is preferred – You should always add CSS files into a www directory when building R Shiny apps. However, the two remaining methods can be useful in certain situations and it can’t hurt to know them, both for personal projects and when reviewing the work of other developers.
1. Add stylings directly to HTML tags
This approach works but there are two downsides. First, it’s easy to lose track of CSS styles in large projects with a lot of code. Second, you can’t reuse them for different objects. If you plan to use the element more than once, you’ll have to repeat the styling.
2. Add CSS to HTML header
A slight improvement over the first approach, but not the optimal solution. You’re defining styles outside of the actual HTML elements. However, since you are still doing it in the code and not using a separate file, you cannot use cache and optimize the app. Therefore, you are still missing some of the options that make CSS particularly useful.
3. Add stylesheets to the www directory
Using a separate file for the style makes the code cleaner. It provides a separate place for all the rules, and also facilitates code reuse and caching. Using multiple files allows us to add structure to the styling process.
Long story short – you should be proficient in CSS if you want to develop production-ready R Shiny applications and dashboards. This article wasn’t a go-to guide on CSS, but rather a simple rundown on how to use CSS in R Shiny. There are three ways to do it, but the last one is preferred by R professionals.
What’s your preferred way to write CSS in R Shiny apps? Plain CSS or SASS? Let us know in the comment section below.