Learn R Programming

officer R package

Make corporate reporting with minimum hassle

The officer package lets R users manipulate Word (.docx) and PowerPoint (*.pptx) documents. In short, one can add images, tables and text into documents from R. An initial document can be provided; contents, styles and properties of the original document will then be available. It also supports the writing of ‘RTF’ documents.

Ressources

The help pages are in a bookdown located at:

https://ardata-fr.github.io/officeverse/

Manuals are available at:

https://davidgohel.github.io/officer/.

Word documents

The read_docx() function will read an initial Word document (an empty one by default) and lets you modify its content later.

The package provides functions to add R outputs into a Word document:

  • images: produce your plot in png or emf files and add them into the document, as a whole paragraph or inside a paragraph.
  • tables: add data.frames as tables, format is defined by the associated Word table style.
  • text: add text as paragraphs or inside an existing paragraph, format is defined by the associated Word paragraph and text styles.
  • field codes: add Word field codes inside paragraphs. Field codes is an old feature of MS Word to create calculated elements such as tables of contents, automatic numbering and hyperlinks.

File generation is performed with the print function.

import Word document in a data.frame

The function docx_summary() reads and imports content of a Word document into a data.frame. The function handles paragraphs, tables and section breaks. The function docx_comments() reads comments of a Word document and organise the results into a data.frame.

PowerPoint documents

The function read_pptx() will read an initial PowerPoint document (an empty one by default) and let you modify its content later.

The package provides functions to add R outputs into existing or new PowerPoint slides:

  • images: produce your plot in png or emf files and add them in a slide.
  • tables: add data.frames as tables, format is defined by the associated PowerPoint table style.
  • text: add text as paragraphs or inside an existing paragraph, format is defined in the corresponding layout of the slide.

In a PowerPoint document, one can set a slide as selected and reach a particular shape (and remove it or add text).

File generation is performed with the print() function.

import PowerPoint document in a data.frame

The pptx_summary() function reads and imports content of a PowerPoint document into a data.frame. The function handles paragraphs, tables and images.

Extensions

Tables and package flextable

The package flextable brings a full API to produce nice tables and use them with packages officer and rmarkdown.

Vector graphics with package rvg

The package rvg brings an API to produce nice vector graphics that can be embedded in PowerPoint documents or Excel workbooks with officer.

Native office charts with package mschart

The package mschart combined with officer can produce native office charts in PowerPoint and Word documents.

Advance Word documents with R Markdown with package officedown

The package officedown facilitates the formatting of Microsoft Word documents produced by R Markdown documents.

Installation

You can get the development version from GitHub:

devtools::install_github("davidgohel/officer")

Or the latest version on CRAN:

install.packages("officer")

Getting help

If you have questions about how to use the package, visit Stack Overflow’s officer tag and post your question there. I usually read them and answer when possible.

Contributing to the package

Code of Conduct

Anyone getting involved in this package agrees to our Code of Conduct.

Bug reports

When you file a bug report, please spend some time making it easy for me to follow and reproduce. The more time you spend on making the bug report coherent, the more time I can dedicate to investigate the bug as opposed to the bug report.

Contributing to the package development

A great way to start is to contribute an example or improve the documentation.

If you want to submit a Pull Request to integrate functions of yours, please provide:

  • the new function(s) with code and roxygen tags (with examples)
  • a new section in the appropriate vignette that describes how to use the new function
  • add corresponding tests in directory tests/testthat.

By using rhub (run rhub::check_for_cran()), you will see if everything is ok. When submitted, the PR will be evaluated automatically on travis and appveyor and you will be able to see if something broke.

Copy Link

Version

Install

install.packages('officer')

Monthly Downloads

82,600

Version

0.6.7

License

MIT + file LICENSE

Maintainer

Last Published

October 9th, 2024

Functions in officer (0.6.7)

docx_body_relationship

Body xml document
block_pour_docx

External Word document placeholder
body_add_toc

Add table of content in a 'Word' document
docx_body_xml

Body xml document
block_section

Section for 'Word'
body_add_docx

Add an external docx in a 'Word' document
body_end_section_landscape

Add landscape section
body_add_fpar

Add fpar in a 'Word' document
body_replace_all_text

Replace text anywhere in the document
body_end_section_continuous

Add continuous section
fp_border

Border properties object
body_comment

Add comment in a 'Word' document
body_end_block_section

Add any section
fp_cell

Cell formatting properties
fp_par

Paragraph formatting properties
fp_tab

Tabulation mark properties object
docx_bookmarks

List Word bookmarks
body_replace_gg_at_bkm

Add plots at bookmark location in a 'Word' document
hyperlink_ftext

Formatted chunk of text with hyperlink
docx_comments

Get comments in a Word document as a data.frame
image_to_base64

Images to base64
change_styles

Replace styles in a 'Word' Document
notes_location_label

Location of a named placeholder for notes
notes_location_type

Location of a placeholder for notes
body_add_break

Add a page break in a 'Word' document
block_list

List of blocks
body_add

Add content into a Word document
annotate_base

Placeholder parameters annotation
color_scheme

Color scheme of a PowerPoint file
length.rdocx

Number of blocks inside an rdocx object
length.rpptx

Number of slides
as.matrix.rpptx

PowerPoint table to matrix
pack_folder

compress a folder
opts_current_table

Table options in a 'knitr' context
ph_location_type

Location of a placeholder based on a type
body_remove

Remove an element in a 'Word' document
body_end_section_portrait

Add portrait section
docx_reference_img

add images into an rdocx object
empty_content

Empty block for 'PowerPoint'
docx_set_character_style

Add character style in a Word document
docx_summary

Get Word content in a data.frame
layout_dedupe_ph_labels

Detect and handle duplicate placeholder labels
ph_remove

Remove a shape
officer

Manipulate Microsoft Word and PowerPoint Documents with 'officer'
ph_location

Location for a placeholder from scratch
layout_properties

Slide layout properties
officer-defunct

Defunct Functions in Package officer
ph_location_right

Location of a right body element
ph_hyperlink

Hyperlink a placeholder
read_docx

Create a 'Word' document object
prop_table

Table properties
body_add_caption

Add Word caption in a 'Word' document
docx_current_block_xml

xml element on which cursor is
cursor_begin

Set cursor in a 'Word' document
docx_dim

'Word' page layout
body_end_section_columns_landscape

Add a landscape multi columns section
doc_properties

Read document properties
body_end_section_columns

Add multi columns section
body_add_xml

Add an xml string as document element
run_bookmark

Bookmark for 'Word'
body_add_blocks

Add a list of blocks into a 'Word' document
run_columnbreak

Column break for 'Word'
ftext

Formatted chunk of text
officer_url_encode

officer url encoder
fortify_location

Eval a location on the current slide
fpar

Formatted paragraph
external_img

External image
body_bookmark

Add bookmark in a 'Word' document
body_set_default_section

Define Default Section
body_replace_text_at_bkm

Replace text at a bookmark location
page_mar

Page margins object
on_slide

Change current slide
read_xlsx

Create an 'Excel' document object
read_pptx

Create a 'PowerPoint' document object
docx_set_paragraph_style

Add or replace paragraph style in a Word document
str_encode_to_rtf

Encode UTF8 string to RTF
sp_lineend

Line end properties
remove_slide

Remove a slide
ph_location_template

Location for a placeholder based on a template
ph_location_fullsize

Location of a full size element
get_reference_value

Get the document being used as a template
get_layout

Layout selection helper
rtf_add

Add content into an RTF document
run_wordtext

Word chunk of text with a style
plot_instr

Wrap plot instructions for png plotting in Powerpoint or Word
plot_layout_properties

Slide layout properties plot
fp_text

Text formatting properties
run_autonum

Auto number
run_pagebreak

Page break for 'Word'
ph_location_id

Location of a placeholder based on its id
layout_summary

Presentation layouts summary
fp_tabs

Tabs properties object
docx_show_chunk

Show underlying text tag structure
layout_rename_ph_labels

Change ph labels in a layout
rtf_doc

Create an RTF document object
slide_summary

Slide content in a data.frame
media_extract

Extract media from a document object
sanitize_images

Remove unused media from a document
move_slide

Move a slide
set_doc_properties

Set document properties
run_reference

Cross reference
run_tab

Tab for 'Word'
table_stylenames

Paragraph styles for columns
set_notes

Set notes for current slide
run_word_field

'Word' computed field
ph_location_label

Location of a named placeholder
sp_line

Line properties
ph_location_left

Location of a left body element
ph_slidelink

Slide link to a placeholder
page_size

Page size object
shortcuts

shortcuts for formatting properties
slide_size

Slides width and height
print.rtf

Write an 'RTF' document to a file
run_footnote

Footnote for 'Word'
print.rpptx

Write a 'PowerPoint' file.
prop_section

Section properties
run_footnoteref

Word footnote reference
ph_with

Add objects on the current slide
run_linebreak

Page break for 'Word'
pptx_summary

PowerPoint content in a data.frame
run_comment

Comment for 'Word'
section_columns

Section columns
set_autonum_bookmark

Update bookmark of an autonumber run
shape_properties_tags

pptx tags for visual and non visual properties
sheet_select

Select sheet
styles_info

Read 'Word' styles
table_colwidths

Column widths of a table
table_conditional_formatting

Table conditional formatting
unpack_folder

Extract files from a zip file
table_width

Preferred width for a table
unordered_list

Unordered list
to_wml

Convert officer objects to WordprocessingML
to_rtf

Convert officer objects to RTF
to_pml

Convert officer objects to PresentationML
to_html

Convert officer objects to HTML
table_layout

Algorithm for table layout
uuid_generate

generates unique identifiers
wml_link_images

transform an xml string with images references
add_slide

Add a slide
block_table

Table block
add_sheet

Add a sheet
block_caption

Caption block
block_toc

Table of content for 'Word'
body_add_gg

Add a 'ggplot' in a 'Word' document
body_add_par

Add paragraphs of text in a 'Word' document
body_add_plot

Add plot in a 'Word' document
body_add_table

Add table in a 'Word' document
body_add_img

Add an image in a 'Word' document