Learn R Programming

XLConnect (version 1.0.7)

addImage-methods: Adding images to a worksheet

Description

Adds an image to a worksheet using a named region.

Usage

# S4 method for workbook
addImage(object, filename, name, originalSize)

Arguments

object

The workbook to use

filename

Name of the image file. Supported are images of the following formats: JPG/JPEG, PNG, WMF, EMF, BMP, PICT.

name

Name of the named region that the image is set to

originalSize

If originalSize = TRUE, the image is inserted in the top left corner of the named region and not scaled. Otherwise, the image is scaled to fit the named region. The default value for originalSize is FALSE.

Author

Martin Studer
Mirai Solutions GmbH https://mirai-solutions.ch

See Also

workbook, createName

Examples

Run this code
if (FALSE) {
## Write an R plot to a specified named region
## This example makes use of the 'Tonga Trench Earthquakes' example

# Load workbook (create if not existing)
wb <- loadWorkbook("earthquake.xlsx", create = TRUE)

# Create a sheet named 'earthquake'
createSheet(wb, name = "earthquake")

# Create a named region called 'earthquake' referring to the sheet
# called 'earthquake' 
createName(wb, name = "earthquake", formula = "earthquake!$B$2")

# Create R plot to a png device
require(lattice)
png(filename = "earthquake.png", width = 800, height = 600)
devAskNewPage(ask = FALSE)

Depth <- equal.count(quakes$depth, number=8, overlap=.1)
xyplot(lat ~ long | Depth, data = quakes)
update(trellis.last.object(),
       strip = strip.custom(strip.names = TRUE, strip.levels = TRUE),
       par.strip.text = list(cex = 0.75),
       aspect = "iso")

dev.off()

# Write image to the named region created above using the image's
# original size; i.e. the image's top left corner will match the
# specified cell's top left corner 
addImage(wb, filename = "earthquake.png", name = "earthquake",
         originalSize = TRUE)

# Save workbook (this actually writes the file to disk)
saveWorkbook(wb)

# clean up 
file.remove("earthquake.xlsx")
file.remove("earthquake.png")
}

Run the code above in your browser using DataLab