问题描述
我早些时候here问过这个问题,但它被标记为重复并关闭。不幸的是,我指向的答案不起作用。...
所以,再次:
我可以制作像这样的eCharts4r量规
library(echarts4r)
library(magrittr)
CA_gauge <- e_charts() %>%
e_gauge(4.1,"INCIDENCE",min=0,max=20,axisLine = list(
linestyle = list(
color=list(
c(1.5/20,"green"),c(3/20,"yellow"),c(1,"red")
)
))) %>%
e_title("CA")
print(CA_gauge)
但是我还没有找到一种将输出保存到文件中的好方法,以便以后可以在gt
表中使用它。我能找到的最好的方法是将“ saveAsImage”添加到输出中
e_charts() %>%
e_gauge(4.1,axisLine = list(
linestyle = list(
color=list(
c(1.5/20,"red")
)
))) %>%
e_toolBox_feature(feature = c("saveAsImage"))
但是我真正想做的只是将图像(显然没有动画)以代码形式保存到tiff / jpg / png图像文件中。我大约有十几个工作要做,所以我不想手动保存它们。
我尝试使用标准设备,例如
tiff("CA_gauge.tif",sep=""),res=600,compression = "lzw",height=5,width=15,units="in")
print(CA_gauge)
dev.off()
但是那没有做到。建议使用htmlwidgets
和webshot
来关闭原始线程时指向的answer。在该示例之后,我想到了
CA_gauge <- e_charts() %>%
e_gauge(round(CA_data[[nrow(CA_data),10]],1),"CA \nINCIDENCE",animation = FALSE,axisLine = list(
linestyle = list(
color=list(
c(1.5/20,"red")
)
)))
htmlwidgets::saveWidget(widget = CA_gauge,file = "~/plot.html")
setwd("~")
webshot::webshot(url = "plot.html",file = "plot.png")
Plot.html
在浏览器中看起来像这样
这一切都很好,但是plot.png
是一个5kb .png文件,据我所知是空白的。当您尝试打开它时,它会显示为白色屏幕。 / p>
有什么建议吗?
解决方法
我在使用 webshot
时遇到了同样的问题,但 webshot2
似乎有效。
webshot2 来自 rstudio 的 github
library(echarts4r)
library(magrittr)
library(webshot2)
library(htmlwidgets)
CA_gauge <- e_charts() %>%
e_gauge(4.1,"INCIDENCE",min=0,max=20,axisLine = list(
linestyle = list(
color=list(
c(1.5/20,"green"),c(3/20,"yellow"),c(1,"red")
)
))) %>%
e_title("CA")
htmlwidgets::saveWidget(widget = CA_gauge,file = "~/ca_gauge.html")
# I had to set this for webshot2 to work on Ubuntu 20.04 LTS
# Sys.setenv(CHROMOTE_CHROME = '/snap/bin/chromium')
# use 'Sys.which("chromium")' to get your path.
webshot('ca_gauge.html',file = 'ca_gauge.png')
文件浏览器中的文件:
输出.png: