R/NAME_comb2pngs.R

Defines functions comb2pngs

Documented in comb2pngs

#' A function to plot 2 png images as one in a knitr chunk
#'
#' \code{comb2pngs} combines to png images into one, and then plots the combined image.
#'
#' The code of this function was inspired by code from Ben Bolker at \url{https://stackoverflow.com/questions/25415365/insert-side-by-side-png-images-using-knitr}
#'
#' @param imgs vector of image file names
#' @param bottom_text caption text appearing below the figure
#' @return plots images in knitr chunk
#' @importFrom grid rasterGrob
#' @importFrom grDevices as.raster
#' @importFrom png readPNG
#' @importFrom gridExtra grid.arrange


#' @export
comb2pngs <- function(imgs, bottom_text = NULL){
  img1 <-  grid::rasterGrob(grDevices::as.raster(png::readPNG(imgs[1])),
                            interpolate = FALSE)
  img2 <-  grid::rasterGrob(grDevices::as.raster(png::readPNG(imgs[2])),
                            interpolate = FALSE)
  gridExtra::grid.arrange(img1, img2, ncol = 2, bottom = bottom_text)
}
sebastiansauer/prada documentation built on Jan. 17, 2024, 11:05 a.m.