R获取开放式绘图设备的类型png或pdf



在R中,我需要检查当前打开的绘图设备类型,例如:

png("test.png"); get_dev_type()
# answer should be "png"
dev.off(); get_dev_type()
# answer should be "error: could not find open plot device"
pdf("test.pdf"); get_dev_type()
# answer should be "pdf"
dev.off()
plot(0); get_dev_type()
# answer should be "x11"

我无法用grDevices::dev.*函数来解决这个问题。

非常感谢您的帮助,
Chris

您可以执行names(dev.cur()):

get_dev_type <- function() names(dev.cur())
png("test.png")
get_dev_type()
#> [1] "png"
dev.off()
#> RStudioGD 
#>         2 
get_dev_type()
#> [1] "RStudioGD"
pdf("test.pdf")
get_dev_type()
#> [1] "pdf"
dev.off()
#> RStudioGD 
#>         2 
plot(0)
get_dev_type()
#> [1] "RStudioGD"

相关内容

最新更新