| Title: | R Graphics Devices for 'Office' Vector Graphics Output |
|---|---|
| Description: | Vector Graphics devices for 'Microsoft PowerPoint' and 'Microsoft Excel'. Functions extending package 'officer' are provided to embed 'DrawingML' graphics into 'Microsoft PowerPoint' presentations and 'Microsoft Excel' workbooks. |
| Authors: | David Gohel [aut, cre], ArData [cph], Bob Rudis [ctb] (the javascript code used by function set_attr), Francois Brunetti [ctb] (clipping algorithms) |
| Maintainer: | David Gohel <[email protected]> |
| License: | GPL-3 |
| Version: | 0.4.2 |
| Built: | 2026-05-25 17:29:12 UTC |
| Source: | https://github.com/davidgohel/rvg |
A simple wrapper to mark
the plot instructions as Vector Graphics instructions. It produces
an object of class 'dml' with a corresponding method ph_with.
The function enables usage of any R plot with argument code and with
ggplot objects with argument ggobj.
Note: the output is a vector graphic, not a native Microsoft Office chart. The underlying data is not embedded in the document.
dml( code, ggobj = NULL, bg = "white", fonts = list(), pointsize = 12, editable = TRUE, ... )dml( code, ggobj = NULL, bg = "white", fonts = list(), pointsize = 12, editable = TRUE, ... )
code |
plotting instructions |
ggobj |
ggplot object to print. argument code will be ignored if this argument is supplied. |
bg, fonts, pointsize, editable
|
Parameters stored and
later passed to |
... |
unused arguments |
When dealing with a ggplot object, the bg parameter sets the
device background but the ggplot theme's plot.background will
typically draw over it. To control the background appearance,
set it in the theme instead:
theme( panel.background = element_rect(fill = "#EFEFEF"), plot.background = element_rect(fill = "wheat"))
The DrawingML format does not support the following R graphics features:
Patterns and gradients: grid patterns (GridPattern),
linear and radial gradients used as fill are not translated into
DrawingML. ggplot2 will emit a warning when these are used.
Rich text (ggtext/gridtext): packages such as ggtext render
HTML/Markdown into composite grid grobs. The rvg device can only
capture low-level primitives (text strings, lines, polygons), so
the rich formatting (inline images, coloured spans, HTML markup)
is lost. Use standard ggplot2 text functions or apply formatting
after export in PowerPoint.
Plotmath expressions: R's graphics engine decomposes
expression() into individual text primitives before the device
sees them. Compound expressions (e.g. expression("a" * "b"),
superscripts, subscripts) are rendered as separate text boxes,
causing vertical misalignment.
Clipping paths, masks, compositing and transformations: these R >= 4.1 features have no DrawingML equivalent and are silently ignored.
anyplot <- dml(code = barplot(1:5, col = 2:6), bg = "wheat") library(officer) doc <- read_pptx() doc <- add_slide(doc, "Title and Content", "Office Theme") doc <- ph_with(doc, anyplot, location = ph_location_fullsize()) fileout <- tempfile(fileext = ".pptx") # fileout <- "vg.pptx" print(doc, target = fileout)anyplot <- dml(code = barplot(1:5, col = 2:6), bg = "wheat") library(officer) doc <- read_pptx() doc <- add_slide(doc, "Title and Content", "Office Theme") doc <- ph_with(doc, anyplot, location = ph_location_fullsize()) fileout <- tempfile(fileext = ".pptx") # fileout <- "vg.pptx" print(doc, target = fileout)
Graphics devices for Microsoft PowerPoint DrawingML format.
dml_pptx( file = "Rplots.dml", width = 6, height = 6, offx = 1, offy = 1, bg = "white", fonts = list(), pointsize = 12, editable = TRUE, id = 1L, last_rel_id = 1L, raster_prefix = "raster_", standalone = TRUE )dml_pptx( file = "Rplots.dml", width = 6, height = 6, offx = 1, offy = 1, bg = "white", fonts = list(), pointsize = 12, editable = TRUE, id = 1L, last_rel_id = 1L, raster_prefix = "raster_", standalone = TRUE )
file |
the file where output will appear. |
height, width
|
Height and width in inches. |
offx, offy
|
top and left origin of the plot |
bg |
Default background color for the plot (defaults to "white"). |
fonts |
Named list of font names to be aliased with
fonts installed on your system. If unspecified, the R default
families Important: fonts are referenced by name in the DrawingML output
and are not embedded. The reader of the PowerPoint file must also
have the same fonts installed, otherwise Office will substitute them.
For maximum portability, prefer fonts bundled with Microsoft Office
such as Fonts must be installed on your system; use
Example: |
pointsize |
default point size. |
editable |
should vector graphics elements (points, text, etc.) be editable. |
id |
specifies a unique identifier (integer) within the slide that will contain the DrawingML instructions. |
last_rel_id |
specifies the last unique identifier (integer) within relationship file that will be used to reference embedded raster images if any. |
raster_prefix |
string value used as prefix for png files produced when raster objects are printed on the graphical device. |
standalone |
produce a standalone drawingml file? If |
dml_pptx(file = tempfile()) plot(1:11, (-5:5)^2, type = "b", main = "Simple Example") dev.off()dml_pptx(file = tempfile()) plot(1:11, (-5:5)^2, type = "b", main = "Simple Example") dev.off()
Graphics devices for Microsoft Excel DrawingML format.
dml_xlsx( file = "Rplots.dml", width = 6, height = 6, offx = 1, offy = 1, bg = "white", fonts = list(), pointsize = 12, editable = TRUE, id = 1L, last_rel_id = 1L, standalone = TRUE )dml_xlsx( file = "Rplots.dml", width = 6, height = 6, offx = 1, offy = 1, bg = "white", fonts = list(), pointsize = 12, editable = TRUE, id = 1L, last_rel_id = 1L, standalone = TRUE )
file |
the file where output will appear. |
height, width
|
Height and width in inches. |
offx, offy
|
top and left origin of the plot |
bg |
Default background color for the plot (defaults to "white"). |
fonts |
Named list of font names to be aliased with
fonts installed on your system. If unspecified, the R default
families Important: fonts are referenced by name in the DrawingML output
and are not embedded. The reader of the Excel file must also
have the same fonts installed, otherwise Office will substitute them.
For maximum portability, prefer fonts bundled with Microsoft Office
such as Fonts must be installed on your system; use
Example: |
pointsize |
default point size. |
editable |
should vector graphics elements (points, text, etc.) be editable. |
id |
specifies a unique identifier (integer) within the sheet that will contain the DrawingML instructions. |
last_rel_id |
specifies the last unique identifier (integer) within relationship file that will be used to reference embedded raster images if any. |
standalone |
produce a standalone drawingml file? If |
dml_xlsx(file = tempfile()) plot(1:11, (-5:5)^2, type = "b", main = "Simple Example") dev.off()dml_xlsx(file = tempfile()) plot(1:11, (-5:5)^2, type = "b", main = "Simple Example") dev.off()
Produces a vector graphics output from R plot instructions
stored in a dml object and adds the result in an rpptx
object produced by read_pptx.
## S3 method for class 'dml' ph_with(x, value, location, ...)## S3 method for class 'dml' ph_with(x, value, location, ...)
x |
a pptx device |
value |
|
location |
a location for a placeholder. |
... |
Arguments to be passed to methods |
anyplot <- dml(code = barplot(1:5, col = 2:6), bg = "wheat") library(officer) doc <- read_pptx() doc <- add_slide(doc, "Title and Content", "Office Theme") doc <- ph_with(doc, anyplot, location = ph_location_fullsize()) fileout <- tempfile(fileext = ".pptx") print(doc, target = fileout)anyplot <- dml(code = barplot(1:5, col = 2:6), bg = "wheat") library(officer) doc <- read_pptx() doc <- add_slide(doc, "Title and Content", "Office Theme") doc <- ph_with(doc, anyplot, location = ph_location_fullsize()) fileout <- tempfile(fileext = ".pptx") print(doc, target = fileout)
Add a dml() object to a sheet in an xlsx workbook
created with officer::read_xlsx(). The plot is rendered as
editable vector graphics (DrawingML).
## S3 method for class 'dml' sheet_add_drawing( x, value, sheet, left = 1, top = 1, width = 6, height = 6, ... )## S3 method for class 'dml' sheet_add_drawing( x, value, sheet, left = 1, top = 1, width = 6, height = 6, ... )
x |
an rxlsx object (created by |
value |
a |
sheet |
sheet name (must already exist) |
left, top
|
left and top origin of the plot on the sheet in inches |
width, height
|
width and height in inches |
... |
additional arguments passed to |
the rxlsx object (invisibly)
library(officer) library(rvg) x <- read_xlsx() x <- sheet_add_drawing(x, value = dml(code = barplot(1:5, col = 2:6)), sheet = "Feuil1", left = 1, top = 2, width = 6, height = 6 ) print(x, target = tempfile(fileext = ".xlsx"))library(officer) library(rvg) x <- read_xlsx() x <- sheet_add_drawing(x, value = dml(code = barplot(1:5, col = 2:6)), sheet = "Feuil1", left = 1, top = 2, width = 6, height = 6 ) print(x, target = tempfile(fileext = ".xlsx"))
Deprecated, use officer::sheet_add_drawing() with a dml() object instead.
xl_add_vg(x, sheet, code, left, top, width, height, ...)xl_add_vg(x, sheet, code, left, top, width, height, ...)
x |
an |
sheet |
sheet label/name |
code |
plot instructions |
left, top
|
left and top origin of the plot on the sheet in inches. |
height, width
|
Height and width in inches. |
... |
arguments passed on to |
library(officer) my_ws <- read_xlsx() my_ws <- sheet_add_drawing(my_ws, value = dml(code = barplot(1:5, col = 2:6)), sheet = "Feuil1", width = 6, height = 6, left = 1, top = 2 ) fileout <- tempfile(fileext = ".xlsx") print(my_ws, target = fileout)library(officer) my_ws <- read_xlsx() my_ws <- sheet_add_drawing(my_ws, value = dml(code = barplot(1:5, col = 2:6)), sheet = "Feuil1", width = 6, height = 6, left = 1, top = 2 ) fileout <- tempfile(fileext = ".xlsx") print(my_ws, target = fileout)