我正在使用 R 和 magick 将信息图表创建为 PDF,并希望在生成的 PDF 输出中插入可点击的超链接。
这可以在 magick 内完成吗?
还有另一个选项可以让我重新加载 PDF 并在其上插入超链接并重新保存。
(大多数搜索结果似乎最终都指向我需要创建一个 Markdown 文档,这似乎不是一个很好的处理方式)
演示代码:
library(magick)
url<-"https://atlas-content-cdn.pixelsquid.com/assets_v2/205/2054680838884300340/jpeg-600/G03.jpg"
#Load Graphic
pb<-image_read(url)
#Place text at at top corner
car<-image_draw(pb) %>%
image_annotate(paste("I want this to be a clickable hyperlink to:",url),location=geometry_point(10,10), color = "red", size = 10)
#Save PDF
image_write(car,"Car.pdf",format="pdf")
我不确定 R 中的
magick
包是否支持将可点击的超链接直接嵌入到图像或 PDF 中。 rmarkdown
可能是要走的路。
library(rmarkdown)
library(magick)
# Loading the image
url <- "https://atlas-content-cdn.pixelsquid.com/assets_v2/205/2054680838884300340/jpeg-600/G03.jpg"
image <- image_read(url)
image_path <- "hyperlink_image.png"
image_write(image, path = image_path)
# Markdown file
rmd_content <- "
---
title: 'Hyperlink in PDF'
output: pdf_document
---
![Car Image]({image_path})
[Click here to go to the link](https://atlas-content-cdn.pixelsquid.com/assets_v2/205/2054680838884300340/jpeg-600/G03.jpg)
"
rmd_content <- gsub("\\{image_path\\}", image_path, rmd_content)
rmd_file <- "hyperlink_rmd_file.Rmd"
writeLines(rmd_content, con = rmd_file)
# Render PDF
render(input = rmd_file, output_format = "pdf_document", output_file = "output.pdf")