R畫圖的時(shí)候出現(xiàn)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()
在運(yùn)行上面R代碼畫餅圖的時(shí)候會在命令輸出窗口里面顯示:
null device
1
雖然圖片是輸出了,但是這個報(bào)錯不知道是什么問題導(dǎo)致的。
解決辦法
把之前的dev.off()改為下面這句話
while (!is.null(dev.list())) dev.off()
這句話判斷了當(dāng)前圖片輸出設(shè)備是否有可用的,如果沒有那也就不執(zhí)行dev.off()這句話了,因?yàn)樵谥暗竭@句代碼的時(shí)候已經(jīng)輸出了圖片了,這里就不需要這句話了。
方法來源
- Stackoverflow - Error in dev.off() : cannot shut down device 1 (the null device)