Often, all you need in a table is data as text and numbers. But sometimes, you’d like to visualize results in each row, too. That’s especially true when each row of data is a trend over time.

You can do that inside a new table column with mini inline graphs called sparklines. You might be familiar with them in Excel, but you can create them in interactive HTML tables, too—with the sparkline package and four basic steps:

[ Get Sharon Machlis’s R tips in our how-to video series. | Read the InfoWorld tutorials: Learn to crunch big data with R. • How to reshape data in R. • R data manipulation tricks at your fingertips • Beginner’s guide to R. | Stay up to date on analytics and big data with the InfoWorld Big Data Report newsletter. ]

  1. Add a column in the data frame that has sparkline data and formatting.
  2. Add a snippet of JavaScript to the table options. That’s the same code all the time, so you can save it once and reuse it.
  3. This one is very easy: Add escape = FALSE as a datatable() argument so HTML displays as HTML and not as the actual code.
  4. This is also very easy: Pipe the results to a function that adds necessary dependencies so the table will display sparklines.

1. Add a column with sparkline data and formatting

Before adding sparklines to a table, you need a table. Here’s code to generate a table from a data frame called prices, including adding search filters and formatting one of the columns as percents:

To read this article in full, please click here