R画图的时候出现null device
问题
# Create data for the graph.
x <- c(21, 62, 10, 53)
labels <- c("London", "New York", "Singapore", "Mumbai")
# Give the chart file a name.
png(file = "city_title_colours.jpg")
# Plot the chart with title and rainbow color pallet.
pie(x, labels, main = "City pie chart", col = rainbow(length(x)))
# Save the file.
dev.off()
在运行上面R
代码画饼图的时候会在命令输出窗口里面显示:
null device
1
虽然图片是输出了,但是这个报错不知道是什么问题导致的。
解决办法
把之前的dev.off()
改为下面这句话
while (!is.null(dev.list())) dev.off()
这句话判断了当前图片输出设备是否有可用的,如果没有那也就不执行dev.off()
这句话了,因为在之前到这句代码的时候已经输出了图片了,这里就不需要这句话了。
方法来源
- Stackoverflow - Error in dev.off() : cannot shut down device 1 (the null device)