如何将 R plot 或 ggplot2 转换为 base64 字符串?

How to convert R plot or ggplot2 to base64 string?

假设我有这段代码(其中 x 和 y 是占位符):

  df <- data.frame(x = c(1,2,3,4,5), y = c(2,3,4,5,6))
  plot <- ggplot(df, aes(x, y)) + geom_point()

有什么办法可以将绘图转换成base64字符串吗?我将通过管道工将其与其他数据一起发回,以显示在网页上。

如果需要,我愿意使用不同的绘图函数或库。

保存 ggplot:

ggsave("myggplot.png", plot)

然后使用base64enc包如下:

base64enc::dataURI(file = "myggplot.png", mime = "image/png")