如何将R ggplot图形存储为html代码段 [英] How to store r ggplot graph as html code snippet
问题描述
我正在通过使用ggplotly()
和htmltools函数(例如h3()
和html()
)创建各种对象来创建html文档.然后,我将它们作为列表提交给htmltools::save_html()
以创建一个html文件.
I am creating an html document by creating various objects with ggplotly()
and htmltools functions like h3()
and html()
. Then I submit them as a list to htmltools::save_html()
to create an html file.
我想直接将ggplot图表添加为图像,而不是附加所有杂乱无章的钟声.最后,我将创建一个自包含的html文件(无依赖项),而内容丰富的工作会使该文件过大.
I would like to add ggplot charts directly as images, rather than attaching all the plotly bells and whistles. In the end, I will create a self-contained html file (no dependencies), and the plotly stuff would make that file excessively large.
是否存在一些将ggplot对象转换为html类型对象的函数?还是我必须将ggplot保存为.png文件,然后将.png文件读入我添加到save_html()函数列表中的某个对象中?
Is there some function that converts a ggplot object into some html-type object? Or do I have to save the ggplot as a .png file, then read the .png file into some object that I add to the list in the save_html() function?
我的R代码看起来像这样:
My R code looks something like this:
library("tidyverse")
library("plotly")
library("htmltools")
HTMLOut <- "c:/Users/MrMagoo/My.html")
df <- data.frame(x=1:25, y=c(1:25*1:25))
g7 <- ggplot(df,aes(x=x, y=y)) + geom_point()
p7 <- ggplotly(g7) # I would like to use something other than ggplotly here. Just capturing the ggplot as an image would be fine.
# create other objects to add to the html file
t7 <- h2(id="graph7", "Title for graph #7")
d7 <- p("description of graph 7")
save_html(list(t7, p7, d7), HTMLOut)
# of course, the real code has many more objects in that list – more graphs, text, tables, etc.
我想用只显示g7的东西替换plotly对象(p7),而不会在save_html函数中引起错误.
I would like to replace the plotly object (p7) with something that just presents g7 in a way that would not cause an error in the save_html function.
我曾经希望找到一个可以直接对ggplot对象进行 Base64编码的函数,但是看来我首先需要将ggplot对象输出为.png文件(或SVG,下方的Teng L),然后对其进行base64编码.我希望有一种更直接的方法,但是我最终可能会这样做,例如 https://stackoverflow.com/a/33410766/3799203 ,以
I had hoped to find a function that could directly Base64 encode a ggplot object, but it seems that I first need to output the 'ggplot' object as a .png file (or SVG, per Teng L, below), then base64-encode it. I was hoping there was a more direct way, but I may end up doing that, as in https://stackoverflow.com/a/33410766/3799203 , ending it with
g7img <- "<img src=\"data:image/png;base64,(base64encode string)\""
g7img <- htmltools::html(g7img)
推荐答案
我最终生成了一个临时图像文件,然后在我称为encodeGraphic()
的函数中进行了base64编码(从LukeA的帖子):
I ended up generating a temparory image file, then base64 encoding it, within a function I called encodeGraphic()
(borrowing code from LukeA's post):
library(ggplot2)
library(RCurl)
library(htmltools)
encodeGraphic <- function(g) {
png(tf1 <- tempfile(fileext = ".png")) # Get an unused filename in the session's temporary directory, and open that file for .png structured output.
print(g) # Output a graphic to the file
dev.off() # Close the file.
txt <- RCurl::base64Encode(readBin(tf1, "raw", file.info(tf1)[1, "size"]), "txt") # Convert the graphic image to a base 64 encoded string.
myImage <- htmltools::HTML(sprintf('<img src="data:image/png;base64,%s">', txt)) # Save the image as a markdown-friendly html object.
return(myImage)
}
HTMLOut <- "~/TEST.html" # Say where to save the html file.
g <- ggplot(mtcars, aes(x=gear,y=mpg,group=factor(am),color=factor(am))) + geom_line() # Create some ggplot graph object
hg <- encodeGraphic(g) # run the function that base64 encodes the graph
forHTML <- list(h1("My header"), p("Lead-in text about the graph"), hg)
save_html(forHTML, HTMLOut) # output it to the html file.
这篇关于如何将R ggplot图形存储为html代码段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!