ReporteRs (version 0.8.5)

addImage: Add an external image into a document object

Description

Add an external image into a document object

Usage

addImage(doc, filename, ...)

## S3 method for class 'docx': addImage(doc, filename, bookmark, par.properties = parProperties(text.align = "center", padding = 5), width, height, ...)

## S3 method for class 'bsdoc': addImage(doc, filename, width, height, par.properties = parProperties(text.align = "center", padding = 5), ...)

## S3 method for class 'pptx': addImage(doc, filename, offx, offy, width, height, ...)

Arguments

doc
document object
filename
"character" value, complete filename of the external image
...
further arguments passed to other methods
bookmark
a character value ; id of the Word bookmark to replace by the image. optional. if missing, image is added at the end of the document. See bookmark.
par.properties
paragraph formatting properties of the paragraph that contains images. An object of class parProperties. It has no effect if doc is a pptx object.
width
image width in inches
height
image height in inches
offx
optional, x position of the shape (top left position of the bounding box) in inches. See details.
offy
optional, y position of the shape (top left position of the bounding box) in inches See details.

Value

  • a document object

Details

Arguments width and height can be defined with functions png::readPNG, jpeg::readJPEG or bmp::read.bmp.

When document object is a pptx, width and height are not mandatory. By default, image is added to the next free 'content' shape of the current slide. See slide.layouts.pptx to view the slide layout.

If arguments offx and offy are missing, position is defined as the position of the next available shape of the slide. This dimensions can be defined in the layout of the PowerPoint template used to create the pptx object.

See Also

docx, pptx, bsdoc

Examples

Run this code
# Image example for MS Word -------
doc.filename = "ex_add_image.docx"

options( "ReporteRs-fontsize" = 10 )
doc <- docx( )
img.file <- file.path( Sys.getenv("R_HOME"), "doc", "html", "logo.jpg" )

if( file.exists( img.file ) && requireNamespace("jpeg", quietly = TRUE) ){
  dims <- attr( jpeg::readJPEG(img.file), "dim" )
  doc <- addTitle( doc, "Add images with width and height", level = 1)
  doc <- addImage(doc, img.file, width = dims[2]/72, height = dims[1]/72)
}
if( exists("win.metafile") ){
	win.metafile(filename = "image.wmf", width = 5, height = 5 )
	barplot( 1:6, col = 2:7)
	dev.off()
	
	doc =addImage(doc, "image.wmf", width = 5, height = 5 )
}

writeDoc( doc, file = doc.filename )

# Image example for bsdoc -------
doc.filename = "ex_add_image/example.html"

options( "ReporteRs-fontsize" = 11 )
doc = bsdoc( )
img.file <- file.path( Sys.getenv("R_HOME"), "doc", "html", "logo.jpg" )

if( file.exists( img.file ) && requireNamespace("jpeg", quietly = TRUE) ){
  dims <- attr( jpeg::readJPEG(img.file), "dim" )
  doc <- addTitle( doc, "Add images with width and height", level = 1)
  doc <- addImage(doc, img.file, width = dims[2]/72, height = dims[1]/72)
}

writeDoc( doc, file = doc.filename )

# Image example for MS PowerPoint -------
doc.filename = "ex_add_image.pptx"

options( "ReporteRs-fontsize" = 24 )
doc = pptx( title = "title" )
img.file = file.path( Sys.getenv("R_HOME"), "doc", "html", "logo.jpg" )

if( file.exists( img.file ) && requireNamespace("jpeg", quietly = TRUE) ){

  dims <- attr( jpeg::readJPEG(img.file), "dim" )
  doc <- addSlide( doc, "Title and Content" )
  doc <- addTitle( doc, "Add images" )
  doc <- addImage(doc, img.file, width = dims[2]/72, height = dims[1]/72)
}
if( exists("win.metafile") ){
	win.metafile(filename = "image.wmf", width = 5, height = 5 )
	barplot( 1:6, col = 2:7)
	dev.off()

	doc = addSlide( doc, "Title and Content")
	doc = addImage(doc, "image.wmf", width = 5, height = 5 )
}

writeDoc( doc, file = doc.filename )

Run the code above in your browser using DataCamp Workspace