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 PowerPointsmschart
: produces native Microsoft charts
Package
officer
is the reboot ofReporteRs
. 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"))
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(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:
- officer: https://davidgohel.github.io/officer
- flextable: https://davidgohel.github.io/flextable
- mschart: https://ardata-fr.github.io/mschart
What’s next
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.↩︎
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 ofrJava
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…↩︎
Follow us: - Recommanded sites: R-bloggers R weekly Twitter #rstats Jobs for R-users