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


try

while (!is.null(dev.list()))  dev.off()

I had this situation and resolved it by running the following 2 or 3 times:

dev.set(dev.next())

The console should eventually spit out:

quartz_off_screen 
                3

Once you see this, the plots will render.

P.S. you can probably use dev.set(dev.prev()) in the same way.


In my case the reason for the error was also quite silly. In case anyone runs into the same issue...

dev.off

will result in:

function (which = dev.cur()) 
{
    if (which == 1) 
        stop("cannot shut down device 1 (the null device)")
    .External(C_devoff, as.integer(which))
    dev.cur()
}
<bytecode: 0x0000000028e62bd8>
<environment: namespace:grDevices>

Do not forget the function brackets:

dev.off()

Tags:

R