The package flextable is existing since mid 2016 and I did not made any communication about it; obviously if I wrote it, I’d like it to be used by R users! That post is an attempt to fix that.

The flextable package makes it simpler to create tables for reporting purposes. The goal of the package is to provide a set of functions that can be used to design and format tabular reporting.

It’s been originally written in order to provide an alternative to ReporteRs::FlexTable objects that are not supported by package officer.

What the flextable package does

It lets customize tables, their formats and their contents. Formatting can be done on cells, paragraphs and text. It also lets you to insert headers and footer rows with eventually merged cells.

Common operations can be made with simple functions, i.e. use bold() to make a table selection bold, use align() to set alignment, merge_at() to merge contiguous cells, etc. Conditional formatting is possible with each of these functions.

flextable bold set_header_labels border display merge_h rotate padding style align italic add_header body_add_flextable fontsize bg ph_with_flextable color autofit merge_at width void merge_v knit_print.flextable set_header_df

Supported outputs

These objects can be rendered with R markdown documents. There is a knitr::knit_print method for HTML output and for Word output (thanks to Maxim Nazarov). Note that you will need pandoc >= 2.0.0 if you want to use Word output.

These objects can also be used with package officer and can be rendered into Word and PowerPoint documents (and Excel when I will find time and motivation for it).

xtable

A work started to support outputs from package xtable. It make statistical reporting easier for those used to work with xtable.

For now, only xtable objects are supported but xtableList will also be supported later.

library(xtable)
library(flextable)
temp.ts <- ts(cumsum(1 + round(rnorm(100), 0)),
  start = c(1954, 7), frequency = 12)
ft <- xtable_to_flextable(x = xtable(temp.ts, digits = 0),
  NA.string = "-")
ft

Preview

the function print() makes it easier to view the flextable inside all supported format.

By default, the print method display the flextable in an HTML page, if you are running R with RStudio, the flextable is displayed in the Viewer as an HTML table.

This can be change with argument preview. The document will be opened with the application associated to the file extension (Word or PowerPoint). This will not work on a Web server, i.e. RStudio Server.

library(flextable)

ft <- flextable(head(iris))
ft <- theme_vanilla(ft)
ft <- width(ft, width = 1)
ft
print(ft, preview = "log")
#> a flextable object.
#> col_keys: `Sepal.Length`, `Sepal.Width`, `Petal.Length`, `Petal.Width`, `Species` 
#> header has 1 row(s) 
#> body has 6 row(s) 
#> original dataset sample: 
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa
#> 5          5.0         3.6          1.4         0.2  setosa
print(ft, preview = "docx")
print(ft, preview = "pptx")

Detailed example

Let’s first create a data.frame example from a sample of mtcars. We will use it as data source for our reporting table.

library(tibble)
library(magrittr)

dataset <- mtcars %>% 
  rownames_to_column(var = "car_name") %>% 
  head() %T>%
  print()
#>            car_name  mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> 1         Mazda RX4 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> 2     Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> 3        Datsun 710 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> 4    Hornet 4 Drive 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> 5 Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> 6           Valiant 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Now let’s use flextable API:

myft <- flextable( dataset, 
  col_keys = c("car_name", "am", "carb", "gear", "blank", "mpg", "drat" )) %>% 
  theme_vanilla() %>% 
  width(j = c("am", "carb", "gear", "mpg", "drat" ), width = 1) %>% 
  width(j = 1, width = 2)
myft

We’ve just created a simple table. Let’s customize printed values.

# Change labels
myft <- myft %>% 
  set_header_labels(
    car_name = "Car name", am = "Transmission", carb = "# carburetors", 
    gear = "# forward gears", mpg = "Miles/(US) gallon", drat = "Rear axle ratio") 
myft <- myft %>% 
  add_header(mpg = "Some measures") %>% 
  merge_at(i = 1, j = c("mpg", "drat"), part = "header") %>% 
  set_formatter( 
    am = function(x) ifelse( x < 1, "automatic", "manual"),
    carb = function(x) sprintf("%.0f", x),
    gear = function(x) sprintf("%.0f", x)
    ) 
myft

And now let’s add some formattings.

myft <- myft %>% 
  bold(i = 3, bold = TRUE) %>% bold(j = 3, bold = TRUE) %>% 
  color(j = 3, color = "#4790b5") %>% color(i = 3, color = "#d24625") %>% 
  align(j = 3:6, align = "center", part = "all") %>% 
  empty_blanks()
myft

Finally, adjust widths and heights:

myft <- autofit(myft)
myft

With package officer

The following code is producing a PowerPoint document containing the flextable.

library(officer)
read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with(myft, ph_location_type(type="body")) %>% 
  print(target = "../../static/files/flextable_example_01.pptx")

Download file flextable_example_01.pptx - view with office web viewer

The following code is producing a Word document containing the flextable.

library(officer)
read_docx() %>% 
  body_add_flextable(value = myft) %>% 
  print(target = "../../static/files/flextable_example_01.docx")

Download file flextable_example_01.docx - view with office web viewer


Follow us:  -  Recommanded sites: R-bloggers R weekly Twitter #rstats Jobs for R-users