Patrick Maia
Patrick Maia

Reputation: 13

Save downloaded images in one PDF in R

I'm downloading a few .pngs of a website, saving them in a separate folder and now I'm trying to join them in a single pdf, each image on a different page.

Everything I've found uses a different language but I would love to do everything using R, is it possible?

Upvotes: 0

Views: 253

Answers (1)

csgroen
csgroen

Reputation: 2551

This should do the trick:

#-- Load libraries
library(png)
library(grid)

#-- Parameters
nFiles <- 2
file_name <- "test"

#-- Open pdf
pdf(file = "test.pdf")

#-- Read the files & plot
for (i in 1:nFiles)
{
    img <- readPNG(paste(file_name, i, ".png", sep = ""))
    grid.raster(img)

    if (i < nFiles) plot.new()
}
#-- Close pdf
dev.off()

Upvotes: 1

Related Questions