Introduction
This article describes how to go from a table...
...to an R table using the formattable package which includes line charts from the sparklines package.
Requirements
A table with multiple rows and a time period range in the columns.
Method
1. Right click on your table in the Report tree and select Reference name.
2. Copy the name.
3. Right click in the Report tree and select Add R Output.
4. In the object inspector go to Properties > R CODE.
5. Reference the formattable and sparkline R libraries and define the table using the name from step 2. In this example we are first adding the row headers of prevalence.tab as a new column and then removing these row headers:
library(formattable)
library(sparkline)
prevalence <- cbind("Indicator Name" = rownames(prevalence.tab), as.data.frame(prevalence.tab))
rownames(prevalence) <- c()
6. We now replace the third column with line charts based on all the original date columns (2 to 7) and specify which columns to keep via their index number. Finally, the table is saved as a formattable HTML widget with a column of line charts covering the entire date range in the middle.
prevalence[3] = apply(prevalence[, 2:7], 1, FUN = function(x) as.character(htmltools::as.tags(sparkline(as.numeric(x), type = "line"))))
names(prevalence)[3] = "  "
new.prevalance = prevalence[, c(1, 2, 3, 7)]
final.table = as.htmlwidget(formattable(new.prevalance,
align = c("l",rep("r", NCOL(prevalence) - 1)),
list(`Indicator Name` = formatter("span", style = ~ style(color = "grey", font.weight = "bold")))))
final.table$dependencies <- c(final.table$dependencies, htmlwidgets:::widget_dependencies("sparkline", "sparkline"))
final.table
Next
How to Add Statistical Significance to a Table Using the Formattable R Package