我有一个数据集,其中包含 ID 列和要下载的 URL 列,每个 ID 有 2 个 URL, 我想下载每两个 URL 并将它们转换为一个以其 ID 命名的 PDF 文件。 我想要每个 ID 一份 PDF。
# demo dataset
df <- data.frame(ID = c(1,1,2,2,3,3),
Full_Path = c("https://test1.png","https://test1.png",
"https://test1.png","https://test1.png",
"https://test1.png","https://test1.png"))
for (url in df$Full_Path) {
download.file(url, destfile = basename(url), method="curl", extra="-k", mode="wb")
fl = list.files(full.names = T, pattern = '.png')
img = image_read(fl) # read from vector of paths
img2 = image_append(img, stack = TRUE) # places pics above one another
image_write(img2, format="pdf", file.path(dir,paste('IMG',today(),'.pdf')))
}
unlink(fl)
但是在这个循环之后全部打印在一个 PDF 中,我想为每个 ID 打印一个 pdf
我不确定下面的代码是否能回答问题。
输出文件名的名称中包含数字。
df <- data.frame(ID = c(1,1,2,2,3,3),
Full_Path = c("https://test1.png","https://test1.png",
"https://test1.png","https://test1.png",
"https://test1.png","https://test1.png"))
for(i in seq_len(nrow(df))) {
url <- df$Full_Path[i]
fl <- basename(url)
download.file(url, destfile = fl, method="curl", extra="-k", mode="wb")
img <- image_read(fl) # read from vector of paths
img2 <- image_append(img, stack = TRUE) # places pics above one another
# sprintf's format string outputs a string
# with the formats replaced by
# %02d - an 2 digits integer padded with zeros
# %s - a string, in this case the system date
outfile <- sprintf('IMG_%02d_%s.pdf', i, today())
outfile <- file.path(dir, outfile)
image_write(img2, format = "pdf", outfile)
}
unlink(fl)