Learn how to build an interactive dashboard using the R programming language and the flexdashboard package.
Data visualization is crucial for understanding complex data and making data-driven decisions. One powerful tool for creating interactive dashboards in R is the flexdashboard package. In this blog post, we will walk you through creating your first flexdashboard using R. Whether you’re new to R or an experienced user, this guide will provide you with everything you need to know to create a visually appealing and functional dashboard.
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham and Garrett Grolemund
- R Graphics Cookbook: Practical Recipes for Visualizing Data by Winston Chang
- Efficient Data Manipulation with R: Perform, Explore, and Understand Data with R by Hadley Wickham
- Interactive Web-Based Data Visualization with R, plotly, and shiny by Carson Sievert
- Data Wrangling with R by Bradley C. Boehmke
- Mastering Shiny: Build Interactive Web Applications with R by Hadley Wickham
Next, you need to install the necessary libraries. Run the following code in your R console to install flexdashboard, dplyr, and plotly:
install.packages("flexdashboard") install.packages("dplyr") install.packages("plotly")
Now that you have the required libraries let’s move on to building the flexdashboard.
Creating the Flexdashboard
- Open RStudio, and click on File > New File > R Markdown….
- In the From Template tab, select Flex Dashboard, and click on OK.
- Give your dashboard a title and author, then click on OK.
This will create a new .Rmd file containing a basic flexdashboard template. The file will include YAML metadata at the top, which specifies the title, output format, and other settings for your dashboard.
Customizing the Dashboard
Now, it’s time to customize your dashboard by adding charts, tables, and other visualizations. You can use R’s powerful data manipulation and visualization libraries, like dplyr and plotly.
First, load the necessary libraries at the beginning of your .Rmd file:
library(flexdashboard) library(dplyr) library(plotly)
For this example, let’s assume we have a data frame called sales_data that contains information about a company’s sales performance. To create a bar chart displaying the total sales by region, you can use the following code:
sales_by_region <- sales_data %>% group_by(region) %>% summarize(total_sales = sum(sales)) plot_ly(sales_by_region, x = ~region, y = ~total_sales, type = 'bar', text = ~total_sales, textposition = 'auto')
Next, create a pie chart displaying the sales by product category. Use the following code:
sales_by_category <- sales_data %>% group_by(category) %>% summarize(total_sales = sum(sales)) plot_ly(sales_by_category, labels = ~category, values = ~total_sales, type = 'pie', hoverinfo = 'label+percent', textinfo = 'label+value+percent')
You can continue adding more visualizations and sections to your dashboard as needed. Finally, to render your dashboard, click the Knit button in RStudio.
To further enhance your understanding of R programming and data manipulation, we recommend the following books (as an Amazon Associate, I may earn a small commission from these links):
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data
- Ace the Data Science Interview: 201 Real Interview Questions Asked By FAANG, Tech Startups, & Wall Street
- The Kaggle Book: Data analysis and machine learning for competitive data science
- Practical Statistics for Data Scientists: 50+ Essential Concepts Using R and Python