I have been doing some decision tree plot as seen below
我一直在做一些决策树情节,如下所示
library(party)
irisct <- ctree(Species ~ .,data = iris)
plot(irisct,type="simple")
But now I want to see the base 64 of the image so I can save the graph into JSON.
但现在我想看到图像的基础64,所以我可以将图形保存为JSON。
Or is there another way to send R image into the web?
或者是否有另一种方式将R图像发送到网络?
3 个解决方案
#1
9
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)
#2
6
You could try using knitr
你可以尝试使用knitr
library(knitr)
printImageURI<-function(file){
uri=image_uri(file)
file.remove(file)
cat(sprintf("<img src=\"%s\" />\n", uri))
}
the printImageURI function takes the filename of a file on disk (I use it quite often with PNG files generated by ggplot). It works great with Firefox, Chrome and IE.
printImageURI函数获取磁盘上文件的文件名(我经常使用ggplot生成的PNG文件)。它适用于Firefox,Chrome和IE。
#3
0
If you have the package base64enc
installed, it's much simpler, with equivalent results (assuming you have the image file.png
already on disk):
如果您安装了base64enc软件包,它会更加简单,并且具有相同的结果(假设您已经在磁盘上安装了image file.png):
# Using RCurl:
txt1 <- RCurl::base64Encode(readBin("file.png", "raw", file.info("file.png")[1, "size"]), "txt")
# Using base64encode:
txt2 <- base64enc::base64encode("file.png")
html1 <- sprintf('<html><body><img src="data:image/png;base64,%s"></body></html>', txt1)
html2 <- sprintf('<html><body><img src="data:image/png;base64,%s"></body></html>', txt2)
# This returns TRUE:
identical(html1, html2)
But using knitr::image_uri("file.png")
(see Bert Neef's answer) is even simpler!
但是使用knitr :: image_uri(“file.png”)(参见Bert Neef的回答)更简单!
#1
9
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)
#2
6
You could try using knitr
你可以尝试使用knitr
library(knitr)
printImageURI<-function(file){
uri=image_uri(file)
file.remove(file)
cat(sprintf("<img src=\"%s\" />\n", uri))
}
the printImageURI function takes the filename of a file on disk (I use it quite often with PNG files generated by ggplot). It works great with Firefox, Chrome and IE.
printImageURI函数获取磁盘上文件的文件名(我经常使用ggplot生成的PNG文件)。它适用于Firefox,Chrome和IE。
#3
0
If you have the package base64enc
installed, it's much simpler, with equivalent results (assuming you have the image file.png
already on disk):
如果您安装了base64enc软件包,它会更加简单,并且具有相同的结果(假设您已经在磁盘上安装了image file.png):
# Using RCurl:
txt1 <- RCurl::base64Encode(readBin("file.png", "raw", file.info("file.png")[1, "size"]), "txt")
# Using base64encode:
txt2 <- base64enc::base64encode("file.png")
html1 <- sprintf('<html><body><img src="data:image/png;base64,%s"></body></html>', txt1)
html2 <- sprintf('<html><body><img src="data:image/png;base64,%s"></body></html>', txt2)
# This returns TRUE:
identical(html1, html2)
But using knitr::image_uri("file.png")
(see Bert Neef's answer) is even simpler!
但是使用knitr :: image_uri(“file.png”)(参见Bert Neef的回答)更简单!