将 R 图像转换为 Base 64 [英] Convert R image to Base 64

查看:19
本文介绍了将 R 图像转换为 Base 64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查看图片的 base64 编码,因此我可以将绘图保存为 JSON 文件的一部分或嵌入到 HTML 页面中.

I want to see find the base64 encoding of an image, so I can save a plot as part of a JSON file or embedded into an HTML page.

library(party)
irisct <- ctree(Species ~ ., data = iris)
plot(irisct, type="simple")

还有其他方法可以在网络上共享 R 图像吗?

Are there other ways to share an R image over the web?

推荐答案

我不知道你到底想完成什么,但这里有一个例子:

I don't know exactly what you want to accomplish, but here is an example:

# save example plot to file
png(tf1 <- tempfile(fileext = ".png")); plot(0); dev.off()

# Base64-encode file
library(RCurl)
txt <- base64Encode(readBin(tf1, "raw", file.info(tf1)[1, "size"]), "txt")

# Create inline image, save & open html file in browser 
html <- sprintf('<html><body><img src="data:image/png;base64,%s"></body></html>', txt)
cat(html, file = tf2 <- tempfile(fileext = ".html"))
browseURL(tf2)

这篇关于将 R 图像转换为 Base 64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆