18

I am new to R programming and I got hit with this error when I tried to run the code. This simply creates a pie chart with some data. Can anyone explain to me why I am getting this error and what it means?

1) I am running windows 2) Version 3.4.0 3) I am using RGui desktop 4) R version 3.4.0

# 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()
Andrew
  • 245
  • 1
  • 2
  • 12
  • 1.) what operating system are you using (Windows, Mac OS X, Linux)? 2.) what version number of RStudio are you running (open RStudio, go to Help > About RStudio)? 3.) Are you using RStudio Desktop or Server version (do you run it from an installed application or from a web browser)? 4.) what version of R do you have installed (enter the command `version` and `sessionInfo()`into the R console)? – M-- Jun 02 '17 at 19:43
  • 1
    Yes only once. I ran the whole script each time and that's what appeared – Andrew Jun 02 '17 at 20:47
  • What do you get when you do `dev.cur()` after `pie(...)`? And have you tried to close R and open it again and then run the code? Does the error persist? –  Jun 02 '17 at 21:49
  • @user7843120 I get png 2 after I run dev.cur(). I have also tried closing R and opening it again, same problem – Andrew Jun 05 '17 at 15:37
  • 1
    I also have this issue with code which previously worked (RStudio on Mac). I've updated R, and updated Java. The code works fine in R itself, so this is obviously an R-studio issue. – Nick Riches Jun 22 '17 at 15:33

6 Answers6

24

try

while (!is.null(dev.list()))  dev.off()
Farid Khafizov
  • 1,062
  • 12
  • 8
7

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.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
awags1
  • 304
  • 2
  • 8
5

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

Andrew
  • 245
  • 1
  • 2
  • 12
4

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()
ThomDietrich
  • 79
  • 1
  • 7
4

When I had this issue, I was attempting to run dev_off() as well with an attempted file being created from the Rchunk I was dealing with. To fix it, I highlighted the entire chunk and tried to rerun it. It didn't say that above so I wanted to add as a possible help to the issue if needed.

  • 4
    Adding to this: If you run RStudio, using this method by running the print commands in a chunk in an RMarkdown-File does not work (all the time), I copied the print commands to the console and it worked flawlessly. – gilberg Feb 12 '20 at 18:54
  • 1
    This was the answer. Reiterating; You literally have to copy/paste these lines to the console. I was even trying commenting the lines in R Code, and then marking the code line by line and pressing command-Enter (osx) and it didn't work. – maze Jan 12 '23 at 22:05
0

I had this error and the only issue what how I named the file I was saving. I used a "_" instead of "."