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:

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(here("static/files/melanoma.RDS"))
flextable(data) %>% theme_vanilla() %>% autofit() 

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 <- flextable(data = head(data)) %>% 
  theme_booktabs() %>% 
  set_header_labels( n = "#", Mean = "\u03D1", SD = "\u03C3") %>% 
  color(i = ~ n < 4, color = "wheat") %>% 
  autofit() 
ft

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(ft, location = ph_location_type(type="body") ) %>% 
  # add a slide then the ggplot
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with(dml(code = print(gg)), location = ph_location_type(type="body")) %>% 
  # add a slide then the microsoft chart
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with(scatter_ms, location = ph_location_type(type="body")) %>% 
  # generate pptx
  print(target = here("static/files/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…↩︎


Suivez nous:  -  Sites recommandés: R-bloggers R weekly Twitter #rstats Jobs for R-users