保存绘图而不显示它 [英] Save plot without showing it at all
问题描述
我做了一点 ggplot
hack为了能够很容易地将图形复制到 powerpoint
,它将图形复制到剪贴板,但是人们可以看到设备窗口快速打开和关闭,这有点尴尬,我可以避免这种情况?
我正在使用 windows
和 rstudio
。
$ b
可复制的代码:
库(ggplot)
`-gg` < - 函数(e1,e2){
assertthat :: assert_that(is.numeric(e2),
length e2)<= 2)
if(identical(e2,0))return(invisible(NULL))
W <-8
H <-4.5
dev .new(width = W * head(e2,1),height = H * tail(e2,1),noRStudioGD = TRUE)
print(e1)
savePlot(clipboard,type = wmf)
dev.off()
e1
}
ggplot(data.frame(x = 1:10,y = 1:10),aes (x,y))+ geom_point() - 1 - 0
我的代码和选择的解决方案在处理半透明性时遇到了问题。大多数情况下都可以,但例外情况会很烦人。
也许一个通用解决方案的路径是使用 tempfile
保存它,然后通过适当的R函数或使用命令行将其读入剪贴板 system
(可能会以隐式方式打开该文件并复制)。
这适用于Windows:使用 win.metafile()
设备。如果你没有给文件名,它会保存到剪贴板。所以你的函数应该是
library(ggplot2)
`-gg`< - 函数(e1,e2){
assertthat :: assert_that(is.numeric(e2),
length(e2)<= 2)
if(identical(e2,0))return不可见(NULL))
W <-8
H <-4.5
win.metafile(width = W * head(e2,1),height = H * tail(e2, 1))
print(e1)
dev.off()
e1
}
ggplot(data.frame(x = 1:10, y = 1:10),aes(x,y))+ geom_point() - 1 - 0
Is it possible to save a plot without displaying it at all ?
I made a little ggplot
hack to be able to copy graphs to powerpoint
easily, it copies the plot to the clipboard, but one can see the device window open and close fast, it's a bit awkward, can I avoid this ?
I'm using windows
and rstudio
.
reproducible code:
library(ggplot)
`-.gg` <- function(e1,e2){
assertthat::assert_that(is.numeric(e2),
length(e2)<= 2)
if(identical(e2,0)) return(invisible(NULL))
W <- 8
H <- 4.5
dev.new(width=W * head(e2,1), height=H * tail(e2,1),noRStudioGD =TRUE)
print(e1)
savePlot("clipboard", type="wmf")
dev.off()
e1
}
ggplot(data.frame(x=1:10,y=1:10),aes(x,y)) + geom_point() - 1 - 0
Edit:
My code, and chosen solution, have issues dealing with semi-transparency.It's ok most of the time, but exceptions will be annoying.
Maybe a path to a general solution would be to save it with tempfile
then read it into the clipboard, either through an appropriate R function, or with command line using system
(maybe something that would open the file invisibly and copy).
This works on Windows: use the win.metafile()
device. If you give no filename, it saves to the clipboard. So your function should be
library(ggplot2)
`-.gg` <- function(e1,e2){
assertthat::assert_that(is.numeric(e2),
length(e2)<= 2)
if(identical(e2,0)) return(invisible(NULL))
W <- 8
H <- 4.5
win.metafile(width=W * head(e2,1), height=H * tail(e2,1))
print(e1)
dev.off()
e1
}
ggplot(data.frame(x=1:10,y=1:10),aes(x,y)) + geom_point() - 1 - 0
这篇关于保存绘图而不显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!