Top 7 Packages for Making Beautiful Tables in R (2024)

Learn how to build attractive data tables with R packages

Top 7 Packages for Making Beautiful Tables in R (3)

Developing meaningful visualizations is an essential yet challenging task in data science. Along with the correct choice of tools, a general understanding of the target audience and the goal of the analysis is also expected in any data analysis. In the same context, tables are a powerful and efficient way of organizing and summarizing any available dataset, especially containing categorical variables. Tables are often used in reports along with supporting data visualizations to communicate the results of data analysis effectively. With some relevant narrative text, tables can precisely share the analysis findings for decision-making in an organization. Although Data visualization in R is a vast topic in itself due to the availability of several robust and easy-to-use plotting libraries, the same can be said about tables in R. The CRAN website offers many open-source packages for R users. These packages can not just create tables but also transform the basic tables into beautiful tables that effectively communicate the analysis findings.

In this article, we will discuss seven interesting packages for building colorful tables in R.

Several R packages offer features to create nicely structured tables. Here are a few packages we’ll use to create beautiful tables.

  1. gt (License: MIT)

The gt package offers a different and easy-to-use set of functions that helps us build display tables from tabular data. The gt philosophy states that a comprehensive collection of table parts can be used to create a broad range of functional tables. These are the table body, the table footer, the spanner column labels, the column labels, and the table header. Below is an illustration of the gt package’s architecture:

Top 7 Packages for Making Beautiful Tables in R (4)

The output formats that gt currently supports are HTML, LaTeX, and RTF. You can find more about gt here. For installing the gt package from CRAN, use the following command:

install.packages(“gt”)

For installing the development version of gt from GitHub, use the following commands:

devtools::install_github(“rstudio/gt”)

Next, we will import the gt library package.

library(gt)

Let’s take the mtcars dataset from the pre-installed datasets of R. This dataset was extracted from the Motor Trend US Magazine (1974) and consists of information on the fuel consumption along with 10 attributes of automotive design and performance of 32 cars (1973 and 1974 models). We can print the first few rows of the ‘mtcars’ dataset using the following command:

head(mtcars)
Top 7 Packages for Making Beautiful Tables in R (5)

Next, we create a new dataframe df using the first few columns and rows from the dataset mtcars.

We will call the main function “gt” with our newly created dataframe “df.” The table will appear on your default browser or the Viewer panel if you use RStudio or another R GUI.

df %>%gt()
Top 7 Packages for Making Beautiful Tables in R (6)

The table we created is a simple table, as the formatting properties used are the default properties. The default appearance of the tables can be changed by using the tab_style() function, through which we can target specific cells and apply styles to them.

df %>%gt() %>%
tab_header(title = “mtcars dataset”) %>%
tab_style(
style = list(cell_fill(color = “#b2f7ef”),
cell_text(weight = “bold”)),
locations = cells_body(columns = mpg))%>%
tab_style(
style = list(cell_fill(color = “#ffefb5”),
cell_text(weight = “bold”)),
locations = cells_body(columns = hp))
Top 7 Packages for Making Beautiful Tables in R (7)

2. formattable (License: MIT + file LICENSE):

Formattable data frames are data frames that will be displayed in HTML tables using formatter functions. This package includes techniques to produce data structures with predefined formatting rules, such that the objects maintain the original data but are formatted. The package consists of several standard formattable objects, including percent, comma, currency, accounting, and scientific. You can find more about formattable here.

For installing the formattable package from CRAN, use the following command:

install.packages(“formattable”)

For installing the development version of formattable from GitHub, use the following commands:

devtools::install_github(“renkun-ken/formattable”)

Next, we will import the formattable library package.

library(formattable)

To demonstrate this library, we will use the built-in function color_bar() to compare the magnitude of values in given columns of data.

formattable(df, list(
hp = color_bar(“#e9c46a”),
cyl = color_bar(“#80ed99”),
wt = color_bar(“#48cae4”),
disp = color_bar(“#f28482”)))
Top 7 Packages for Making Beautiful Tables in R (8)

3. kableExtra (License: MIT + file LICENSE)

The kableExtra package is used to extend the basic functionality of knitr::kable tables(). Although knitr::kable() is simple by design, it has many features missing which are usually available in other packages, and kableExtra has filled the gap nicely for knitr::kable(). The best thing about kableExtra is that most of its table capabilities work for both HTML and PDF formats. You can find more about kableExtra here.

For installing the kableExtra package from CRAN, use the following command:

install.packages(“kableExtra”)

For installing the development version of kableExtra from GitHub, use the following commands:

remotes::install_github(“haozhu233/kableExtra”)

Next, we will import the kableExtra library package.

library(kableExtra)

We will call the kbl function with dataframe “df” to view the basic version of the table.

kable(df) %>% kable_styling(latex_options = “striped”)
Top 7 Packages for Making Beautiful Tables in R (9)

To style individual rows and columns, we can use the functions row_spec() and column_spec().

df %>% kbl() %>%
kable_paper() %>% column_spec(2, color = “white”,
background = spec_color(mtcars$drat[1:2],end = 0.7)) %>%
column_spec(5, color = “white”,
background = spec_color(mtcars$drat[1:6], end = 0.7),
popover = paste(“am:”, mtcars$am[1:6]))
Top 7 Packages for Making Beautiful Tables in R (10)

4. dt (License: GPL-3)

dt is an abbreviation of ‘DataTables.’ Data objects in R can be rendered as HTML tables using the JavaScript library ‘DataTables’ (typically via R Markdown or Shiny). You can find more about dt here.

For installing the dt package from CRAN, use the following command:

install.packages(‘DT’)

For installing the development version of dt from GitHub, use the following command:

remotes::install_github(‘rstudio/DT’)

Next, we will import the dt library package.

library(DT)

The DT package’s main feature is its ability to provide filtering, pagination, and sorting to HTML tables. By using this package, we can slice, scroll through, and arrange tables to understand the table contents better.

datatable(
data = mtcars,
caption = “Table”,
filter = “top”
)
Top 7 Packages for Making Beautiful Tables in R (11)

5. flextable (License: GPL-3)

flextable package helps you to create reporting table from a dataframe easily. You can merge cells, add headers, add footers, change formatting, and set how data in cells is displayed. Table content can also contain mixed types of text and image content. Tables can be embedded from R Markdown documents into HTML, PDF, Word, and PowerPoint documents and can be embedded using Package Officer for Microsoft Word or PowerPoint documents. Tables can also be exported as R plots or graphic files, e.g., png, pdf, and jpeg. You can find more about flextable here.

For installing the flextable package from CRAN, use the following command:

install.packages(“flextable”)

For installing the development version of flextable from GitHub, use the following command:

devtools::install_github(“davidgohel/flextable”)

Next, we will import the flextable library package.

library(flextable)

For this library, the main function is flextable. We will call the flextable function to view the basic version of the table as shown below:

flextable(df)
Top 7 Packages for Making Beautiful Tables in R (12)

We will use the set_flextable_defaults function to change the default appearance of the table.

set_flextable_defaults(
font.family = “Arial”, font.size = 10,
border.color = “#e5383b”,
padding = 6,
background.color = “#EFEFEF”)
flextable(head(df)) %>%
bold(part = “header”)
Top 7 Packages for Making Beautiful Tables in R (13)

6. reactable (License: MIT + file LICENSE)

reactable() creates a data table from tabular data with sorting and pagination by default. The data table is an HTML widget that can be used in R Markdown documents and Shiny applications or viewed from an R console. It is based on the React Table library and made with reactR. There are many features of reactable; some of them are given below:

  • It creates a data table with sorting, filtering, and pagination
  • It has built-in column formatting
  • It supports custom rendering via R or JavaScript
  • It works seamlessly within R Markdown documents and the Shiny app

For installing the reactable package from CRAN, use the following command:

install.packages(“reactable”)

For installing the development version of reactable from GitHub, use the following commands:

# install.packages(“devtools”)
devtools::install_github(“glin/reactable”)

Next, we will import the reactable library package.

library(reactable)

We will use the reactable() function to create a data table. The table will be sortable and paginated by default:

reactable(mtcars)
Top 7 Packages for Making Beautiful Tables in R (14)

To change the table’s default appearance, we will use the reactableTheme() function. The global reactable.theme option can also be used if you want to set the default theme for all tables.

library(reactable)
reactable(mtcars)
options(reactable.theme = reactableTheme(
color = “black”,
backgroundColor = “#bde0fe”,
borderColor = “#a3b18a”,
stripedColor = “#a3b18a”,
highlightColor = “#2b2d42”
))
Top 7 Packages for Making Beautiful Tables in R (15)

7. reactablefmtr (License: MIT + file LICENSE)

The reactablefmtr package improves the appearance and formatting of tables created using the reactable R library. The reactablefmtr package includes many conditional formatters that are highly customizable and easy to use.

For installing the reactablefmtr package from CRAN, use the following command:

install.packages(“reactablefmtr”)

For installing the development version of reactablefmtr from GitHub, use the following commands:

remotes::install_github(“kcuilla/reactablefmtr”)

The reactable package in R allows you to create interactive data tables. However, formatting tables inside reactable requires a large amount of code, which might be challenging for many R users and needs to be more scalable. The data_bars() function in the reactablefmtr library makes it much easier to create bar charts.

library(reactablefmtr)
reactable(data,defaultColDef = colDef(
cell = data_bars(data,text_position = “outside-base”)
))
Top 7 Packages for Making Beautiful Tables in R (16)

There are several ways to alter the appearance of data_bars(), including bar alignment, text label location, and the ability to add icons and images to the bars.

library(reactablefmtr)
reactable(data,defaultColDef = colDef(cell = data_bars(df, box_shadow = TRUE, round_edges = TRUE,
text_position = “outside-base”,
fill_color = c(“#e81cff”, “#40c9ff”),
background = “#e5e5e5”,fill_gradient = TRUE)
))
Top 7 Packages for Making Beautiful Tables in R (17)

Conclusion

In this article, we discussed seven powerful R packages to create beautiful tables for a given dataset. There are many more R libraries, and indeed some new ones will also be developed in the future. But this tutorial can be helpful to get started with these packages for anyone looking to create more beautiful and effective tables in R.

You can follow me on: GitHub, Kaggle, Twitter and LinkedIn.

Top 7 Packages for Making Beautiful Tables in R (2024)

FAQs

What is the best package for tables in R? ›

flextable (Gohel and Skintzos 2023) and huxtable (Hugh-Jones 2024): If you are looking for a table package that supports the widest range of output formats, flextable and huxtable are probably the two best choices.

How can I make my table prettier? ›

Use colors and lines to help readers navigate your table. Highlight important cells by applying a subtle background color or group related values by creating thicker lines. Include the source of your data to make your table look more professional and allow readers to analyze the topic more deeply.

What package is data table in R? ›

Data. table is an extension of data. frame package in R. It is widely used for fast aggregation of large datasets, low latency add/update/remove of columns, quicker ordered joins, and a fast file reader.

Is data table better than tidyverse? ›

table and tidyverse . In cases when we are handling very large dataset, data. table would be a good choice since it runs extremely fast. In cases when we are not requiring the speed so much, especially when collaborating with others, we can choose tidyverse since its code is more readable.

Which program is best for tables? ›

Despite its basic data visualization capabilities, Microsoft Excel Online is a powerful tool. It allows users to create, edit, and share spreadsheets, table charts, and other types of charts. The platform offers a wide range of templates and pre-built formulas to make this process as easy as possible.

How do you make a table look elegant? ›

Ensure your tablecloth, porcelain and interior details match each other for a uniform look. Decorate the table with cloth napkins for a luxurious feeling. Use lots of candles on the table for a warm and inviting feeling. Bring elements of nature to the table setting with flowers or plants.

How make a table look fancier? ›

Invest in placemats and coasters to get the designer look with minimal effort. High-quality placemats and coasters will not only protect your table but can add colour and style too. The key here is to invest in high-quality and well-made products and choose items that work well with the material of your dining table.

How do you make an awesome table? ›

Create your first Awesome Table app
  1. Tutorial: Create your first Awesome Table app.
  2. Step 1: Create and set up your data source.
  3. Step 2: Create and set up a Table app in Awesome Table.
  4. Step 3: Customize how data is displayed in your app.
  5. Step 4: Embed your app on your website.

What makes a table more beautiful and attractive? ›

Matching the hues can give your table a huge makeover. You can use neutral shades with earthy tones to give your table warmth. Add fruits in jute baskets to give your table a pop-up look.

How to create visually appealing tables? ›

When deciding how to style and format your table, prioritize readability and remove any visual clutter that may distract the eye.
  1. Choose The Best Row Style. ...
  2. Use Clear Contrast. ...
  3. Add Visual Cues. ...
  4. Align Columns Properly. ...
  5. Use Tabular Numerals. ...
  6. Choose an Appropriate Line Height. ...
  7. Include Enough Padding. ...
  8. Use Subtext.
Oct 1, 2019

What is the dplyr package in R? ›

The dplyr package is a relatively new R package that allows you to do all kinds of analyses quickly and easily. It is especially useful for creating tables of summary statistics across specific groups of data.

Is a data table faster than dplyr? ›

Summary of Fast Data Lookups in R

Dplyr with its filter method will be slow if you search for a single element in a dataset. The same is true for classic data frame filtering with builtin R operators and for regular filtering using data. table .

What package in R uses %>%? ›

18.1. 1 Prerequisites. The pipe, %>% , comes from the magrittr package by Stefan Milton Bache. Packages in the tidyverse load %>% for you automatically, so you don't usually load magrittr explicitly.

What is the best mapping package in R? ›

ggmap. The ggmap package is the most exciting R mapping tool in a long time! You might be able to get better looking maps at some resolutions by using shapefiles and rasters from naturalearthdata.com but ggmap will get you 95% of the way there with only 5% of the work!

How do I add a table in RStudio? ›

In R, these tables can be created using table() along with some of its variations. To use table(), simply add in the variables you want to tabulate separated by a comma.

What is the difference between Dataframes and tables in R? ›

frame in R is similar to the data table which is used to create tabular data but data table provides a lot more features than the data frame so, generally, all prefer the data. table instead of the data. frame.

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Tuan Roob DDS

Last Updated:

Views: 6109

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Tuan Roob DDS

Birthday: 1999-11-20

Address: Suite 592 642 Pfannerstill Island, South Keila, LA 74970-3076

Phone: +9617721773649

Job: Marketing Producer

Hobby: Skydiving, Flag Football, Knitting, Running, Lego building, Hunting, Juggling

Introduction: My name is Tuan Roob DDS, I am a friendly, good, energetic, faithful, fantastic, gentle, enchanting person who loves writing and wants to share my knowledge and understanding with you.