# 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 DataLab