Error in Dev.Off(): Cannot Shut Down Device 1 (The Null Device)

Error in dev.off() : cannot shut down device 1 (the null device)

Turns out that the only problem with this was with where the file was trying to save to. I altered the code to save to a different directory and it worked fine

Function that saves to files produces an error

Change your code like so

f1<-function(x){
write.table(as.matrix(summary(x)),file="dane.txt",sep=";",row.names=T)

#I want here to make two plots.
png(filename="hist.png")
hist(x)
dev.off()

dev.cur()
png(filename="density.png")
plot(density(x))
dev.off()
}

You only missed the () of dev.off()

How to plot in RStudio and not have a new window pop up (R Graphics: Device (ACTIVE)?

For others who like me may still encounter this issue:

This is probably caused by an R update to 3.3.2 and is fixed by installing a newer version of RStudio. In my case 1.0.136 did the trick.

Suppressing null device output with R in batch mode

One of the nice things about R is that you can view the source of many functions:

> dev.off
function (which = dev.cur())
{
if (which == 1)
stop("cannot shut down device 1 (the null device)")
.Internal(dev.off(as.integer(which)))
dev.cur()
}
<environment: namespace:grDevices>

So it calls .Internal(dev.off(...)) and then returns dev.cur(), which I suppose would be useful if you have several devices open so you know which one became active. You could use .Internal(dev.off(as.integer(dev.cur()))) in your script, or even patch dev.off to only return the value of dev.cur() if it is something else than the null device, and send the patch to the maintainers of R.

Also, graphics.off() calls dev.off() for all devices and doesn't return anything.



Related Topics



Leave a reply



Submit