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
officeris the reboot of
ReporteRs. It’s better, faster and does only rely on R.2
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/files/melanoma.RDS") regulartable(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 <- regulartable(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_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/files/melanoma.pptx") %>% invisible()
The documentation of the packages can be found here:
I will work later on:
- Fix issues if any.
- Add a minimal support for Excel files with flextables, vector graphics.
- Add pages headers and footers support for Word files.
I will write more about these in future posts.↩
ReporteRsexists 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
rJavadependency 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…↩