doc.filename = "addImage_example.docx"
# set default font size to 10
options( "ReporteRs-fontsize" = 10 )
doc = docx( )
# files 'logo.jpg' and 'logosm.jpg' only exist in R for Windows
img.file1 = file.path( Sys.getenv("R_HOME"), "doc", "html", "logo.jpg" )
img.file2 = file.path( Sys.getenv("R_HOME"), "doc", "html", "logosm.jpg" )
if( file.exists( img.file1 ) && file.exists( img.file2 ) ){
doc = addTitle( doc, "Add images with defaut PPI (72)", level = 1)
doc = addTitle( doc, "Image 1", level = 2)
doc = addImage(doc, img.file1 )
doc = addTitle( doc, "Image 2", level = 2)
doc = addImage(doc, img.file2 )
doc = addTitle( doc, "Add images with their respective PPI", level = 1)
doc = addTitle( doc, "Image 1", level = 2)
doc = addImage(doc, img.file1, ppi = 300 )
doc = addTitle( doc, "Image 2", level = 2)
doc = addImage(doc, img.file2, ppi = 96)
doc = addTitle( doc, "Add images with width and height", level = 1)
doc = addImage(doc, img.file1, width = 4, height = 2 )
}
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 )
}
# Write the object
writeDoc( doc, file = doc.filename )
Run the code above in your browser using DataLab