It’s time to announce the release of officer version 0.1.6. It allows to generate Microsoft documents from R; supported formats are Word and PowerPoint. It does come with other new packages1:

  • flextable: produces sophisticated tables.
  • rvg: produces vector graphics for PowerPoints
  • mschart: produces native Microsoft charts

Package officer is the reboot of ReporteRs. It’s better, faster and does only rely on R.2

Example

Let’s have a demo. I have made a video for impatients:

First load packages:

library(officer)
library(flextable)
library(rvg)
library(mschart)
library(magrittr)
library(ggplot2)

We will use the following dataset:

Download file data.RDS

data <- readRDS("../../static/misc/melanoma.RDS")
regulartable(data) %>% theme_vanilla() %>% autofit() 

Status

Gender

Ulceration

n

Mean

SD

Alive

Female

Absent

68

1.693

2.004

Alive

Female

Present

23

2.972

2.593

Alive

Male

Absent

24

1.468

1.719

Alive

Male

Present

19

4.319

2.423

Melanoma

Female

Absent

8

2.139

1.184

Melanoma

Female

Present

20

4.724

4.128

Melanoma

Male

Absent

8

3.266

4.681

Melanoma

Male

Present

21

5.143

2.862

Non-melanoma

Female

Absent

3

1.667

1.141

Non-melanoma

Female

Present

4

3.302

3.713

Non-melanoma

Male

Absent

4

2.420

2.499

Non-melanoma

Male

Present

3

8.053

4.019

First let’s create a ggplot object:

gg <- ggplot(data, aes(x = Ulceration , y = Mean, colour = Gender, size = n)) + 
  geom_point() + 
  facet_wrap(~Status ) + 
  theme_minimal()
gg

Then a summary table:

ft <- regulartable(data = head(data)) %>% 
  theme_booktabs() %>% 
  set_header_labels( n = "#", Mean = "\u03D1", SD = "\u03C3") %>% 
  color(i = ~ n < 4, color = "wheat") %>% 
  autofit() 
ft

Status

Gender

Ulceration

#

ϑ

σ

Alive

Female

Absent

68

1.693

2.004

Alive

Female

Present

23

2.972

2.593

Alive

Male

Absent

24

1.468

1.719

Alive

Male

Present

19

4.319

2.423

Melanoma

Female

Absent

8

2.139

1.184

Melanoma

Female

Present

20

4.724

4.128

A Microsoft chart:

scatter_ms <- ms_scatterchart(data = data, x = "Mean", y = "n", group = "Status")

And gather them in a new PowerPoint document:

read_pptx() %>% 
  # add a slide then the flextable
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with_flextable(ft, type="body" ) %>% 
  # add a slide then the ggplot
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with_vg(code = print(gg), type="body", bg = "transparent") %>% 
  # add a slide then the microsoft chart
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with_chart(chart = scatter_ms, type="body") %>% 
  # generate pptx
  print(target = "../../static/misc/melanoma.pptx") %>% 
  invisible()

Download file melanoma.pptx - view with office web viewer

Documentation

The documentation of the packages can be found here:

What’s next

I will work later on:

  1. Fix issues if any.
  2. Add a minimal support for Excel files with flextables, vector graphics.
  3. Add pages headers and footers support for Word files.

  1. I will write more about these in future posts.

  2. ReporteRs exists since beginning of 2014. In the beginning, everything was under control, I was able to help or debug when necessary. But then the package became more popular and support started to be painful; because of rJava dependency and other reasons. The best solution was to rewrite the package so that I could do something better without breaking existing codes of users that choosed ReporteRs…