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.

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.

How to format a nice table in R? ›

The {reactablefmtr} package simplifies and enhances the styling and formatting of tables built with the {reactable} R package. The {reactablefmtr} package provides many conditional formatters that are highly customizable and easy to use.

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.

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.

Is a data table faster than dplyr? ›

dplyr shows great memory efficiency in summarizing, while data. table is generally the fastest approach.

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.

Which R package is used for data visualization? ›

1. Plotly. Plotly is an R package library for all your graphics needs, and it is open-source and free to use. Using plotly, developers can create remarkably beautiful and interactive visualizations.

How do you make a table of data look good? ›

Table Style
  1. Choose The Best Row Style. Row style helps users scan, read, and parse through data. ...
  2. Use Clear Contrast. Establish hierarchy by adding contrast to your table. ...
  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

How do you format a table to look nice? ›

Change the table style options
  1. To add special formatting to the first row in a table, select Design > Header Row.
  2. To add special formatting to the last row in a table, select Design > Total Row.
  3. To alternate row or column colors and make tables easier to read, select Design > Banded Rows or Design > Banded Columns.

How do you set a nice table? ›

To the left of the plate, place the fork on the napkin. On the right of the plate, place the knife closest to the plate and then the spoon. Directly above the knife, place the water glass. To the right and slightly above the water glass, place the wine glass or a glass for another beverage.

What makes a table more beautiful and attractive? ›

Use Linens. Adding linens instantly elevates your table setting. Consider a tablecloth, cloth napkins, and/or a table runner. Linens are an extra touch that won't go unnoticed by your guests and are a great foundation for setting the perfect table.

How do you make a table look aesthetic? ›

Pick colours and things you like for aesthetic study table decoration. Use colourful pens, and stylish notebooks, and add cute decorations. A small plant can bring freshness, and keeping things organised with trays or containers makes it look neat.

What is the table function package in R? ›

Table function (table())in R performs a tabulation of categorical variable and gives its frequency as output. It is further useful to create conditional frequency table and Proportinal frequency table. This recipe demonstrates how to use table() function to create the following two tables: Frequency table.

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!

Can you create tables in R? ›

Tables are often essential for organzing and summarizing your data, especially with categorical variables. When creating a table in R, it considers your table as a specifc type of object (called “table”) which is very similar to a data frame.

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
Article information

Author: Pres. Lawanda Wiegand

Last Updated:

Views: 5701

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Pres. Lawanda Wiegand

Birthday: 1993-01-10

Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893

Phone: +6806610432415

Job: Dynamic Manufacturing Assistant

Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting

Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.