Skip to content

Error in RStudioGD() : Shadow graphics device error: r error 4 (R code execution error) #2214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ArRstudio opened this issue Feb 16, 2018 · 38 comments · Fixed by #6722
Closed
Assignees
Labels

Comments

@ArRstudio
Copy link

System details

RStudio Edition : <!-- Desktop or Server -->
RStudio Version : Version 1.0.136
OS Version      : ubuntu
R Version       : 3.3.3

Steps to reproduce the problem

plot(cars)
Error in RStudioGD() : Shadow graphics device error: r error 4 (R code execution error) In addition: Warning message: In grDevices:::png("/tmp/RtmpEdEj7l/642098b8481c4db7820081c92f4117e7.png", : unable to open connection to X11 display ''

 

| >

Describe the problem in detail

i am trying to run plot(cars) on rstudio application ,receiving this below error

plot(cars)
Error in RStudioGD() : Shadow graphics device error: r error 4 (R code execution error) In addition: Warning message: In grDevices:::png("/tmp/RtmpEdEj7l/642098b8481c4db7820081c92f4117e7.png", : unable to open connection to X11 display ''

 

| >

Describe the behavior you expected

@ronblum
Copy link
Contributor

ronblum commented Feb 19, 2018

@ArRstudio Are you able to run plot(cars) successfully in R alone, outside of RStudio? Also, you might consider updating to the latest version of RStudio, 1.1.423, which is available for download at https://www.rstudio.com/products/rstudio/download/

@ronblum ronblum added the info needed Additional information requested—reprex, steps, open question, etc. label Feb 19, 2018
@ronblum
Copy link
Contributor

ronblum commented Feb 20, 2018

@ArRstudio It looks like this is being handled by our professional Support Team (issue 25469) so I'm am going to close it in this queue.

@Andreas-Bio
Copy link

Same issue here:


> plot(mtcars)
Error in RStudioGD() : 
  Shadow graphics device error: r error 4 (R code execution error)
In addition: Warning messages:
1: In grDevices:::png("C:/Users/gf1954/AppData/Local/Temp/Rtmp02Ep26/1033921b18d7481796b9759f3bc1e62b.png",  :
  kann Datei 'C:/Users/gf1954/AppData/Local/Temp/Rtmp02Ep26/1033921b18d7481796b9759f3bc1e62b.png' nicht zum Schreiben öffnen
2: In grDevices:::png("C:/Users/gf1954/AppData/Local/Temp/Rtmp02Ep26/1033921b18d7481796b9759f3bc1e62b.png",  :
  opening device failed

Another RStudio instance works but this instance fails.

@Rietaros
Copy link

Rietaros commented May 7, 2019

I use RStudio Version 1.1.463 – © 2009-2018 RStudio, Windows 10

have the same issue
Error in RStudioGD() : Shadow graphics device error: r error 4 (R code execution error) In addition: Warning messages: 1: In grDevices:::png("C:/Users/AFITRI~1/AppData/Local/Temp/RtmpQBGjX0/01f79ba1aad74fd3b5c52d0423bda7af.png", : unable to open file 'C:/Users/AFITRI~1/AppData/Local/Temp/RtmpQBGjX0/01f79ba1aad74fd3b5c52d0423bda7af.png' for writing 2: In grDevices:::png("C:/Users/AFITRI~1/AppData/Local/Temp/RtmpQBGjX0/01f79ba1aad74fd3b5c52d0423bda7af.png", : opening device failed

@kevinushey
Copy link
Contributor

The error most likely implies that RStudio / your user account does not have write access to your Temp folder for some reason. Unfortunately figuring out why this is the case could be very challenging.

@werner-rammer
Copy link

I experienced the same error; thanks for @kevinushey for hinting at missing write access!
I am on Windows, and for some reason the temp directory RStudio was trying to write into did not exist anymore (my RStudio is already open for quite a long time).

The problem was gone and plotting back to normal when I recreated the temp folder (in my case the c:/users/xxx/AppData/Local/Temp/Rtmp0kHPQi folder).

@10515524
Copy link

@werner-rammer
I recreated the temp folder and it worked. Thank you!

@ytrancer
Copy link

The last folder in the path did not exist. Re-creating the folder has solved the issue for me.

@kevinushey
Copy link
Contributor

If I understand correctly, this issue is occurring because something is deleting the R temporary directory in your session? AFAIK this shouldn't happen normally, but we could try to recover by auto-recreating the directory as needed...

@ytrancer
Copy link

Sometimes I will start a project and, when I have a break, I will just close the laptop lid. Maybe temp files are erased as Windows goes into standby.

@werner-rammer
Copy link

@kevinushey : yes, re-creating the folder if missing would solve the issue. It does not always happen when Windows goes into standby, and my guess is that it is related to Windows updates (but really just a guess).

By the way: QGis has a very similiar issue (saving of a project fails when the temp directory is gone).

@hafez-ahmad
Copy link

Error in RStudioGD() :
Shadow graphics device error: r error 4 (R code execution error)
In addition: Warning messages:
1: In grDevices:::png("C:/Users/hafez/AppData/Local/Temp/RtmpEHFUSu/58029964ce8b4531b26d8dc1007a08cb.png", :
unable to open file 'C:/Users/hafez/AppData/Local/Temp/RtmpEHFUSu/58029964ce8b4531b26d8dc1007a08cb.png' for writing
2: In grDevices:::png("C:/Users/hafez/AppData/Local/Temp/RtmpEHFUSu/58029964ce8b4531b26d8dc1007a08cb.png", :
opening device failed

@onpatrick
Copy link

thank you! it worked

@igorfarah
Copy link

Error in RStudioGD() :
Shadow graphics device error: r error 4 (R code execution error)
In addition: Warning messages:
1: In grDevices:::png("C:/Users/hafez/AppData/Local/Temp/RtmpEHFUSu/58029964ce8b4531b26d8dc1007a08cb.png", :
unable to open file 'C:/Users/hafez/AppData/Local/Temp/RtmpEHFUSu/58029964ce8b4531b26d8dc1007a08cb.png' for writing
2: In grDevices:::png("C:/Users/hafez/AppData/Local/Temp/RtmpEHFUSu/58029964ce8b4531b26d8dc1007a08cb.png", :
opening device failed

You just need to create a folder on Temp called "RtmpEHFUSu" and it'll work.

@ronblum
Copy link
Contributor

ronblum commented Jul 27, 2020

The fix for this, #6722, should be in the 1.4 Wax Begonia dailies. Note, though, that the dailies are meant for testing purposes only.

@ronblum ronblum added bug and removed verified labels Jul 27, 2020
@DawnEve
Copy link

DawnEve commented Dec 30, 2020

the same error on CentOS

$ lsb_release -a
LSB Version:	:core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID:	CentOS
Description:	CentOS Linux release 7.5.1804 (Core) 
Release:	7.5.1804
Codename:	Core

> plot(cars)
Error in RStudioGD() : 
  Shadow graphics device error: r error 4 (R code execution error)
In addition: Warning message:
In grDevices:::png("/tmp/RtmpDYD5MP/63fb007b34db4b42b4230b8c578df9a8.png",  :
  unable to open connection to X11 display ''

But the /tmp/ dir exists, the file is empty:

$ ls -l /tmp/RtmpDYD5MP/
total 0
-rw-r--r--. 1 wangjl user 0 Dec 30 15:06 63fb007b34db4b42b4230b8c578df9a8.png

And output to pdf files works fine:

> pdf('1.pdf', width=4, height=4)
> plot(cars)
> dev.off()
null device 
          1 

$ ls -l /home/wangjl/tmp/pdfs/
total 20
-rw-r--r--. 1 wangjl user  6948 Dec 30 15:07 1.pdf
> version
               _                           
platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          4                           
minor          0.3                         
year           2020                        
month          10                          
day            10                          
svn rev        79318                       
language       R                           
version.string R version 4.0.3 (2020-10-10)
nickname       Bunny-Wunnies Freak Out    


RStudio Server
Version 1.3.1093

@OfekShilon
Copy link

Still getting bitten by this in 2021, RStudio 1.4.1103 - with successful access to tmp dir:

> plot(1:100,sin(1:100))
Error in RStudioGD() : 
  Shadow graphics device error: r error 4 (R code execution error)
In addition: Warning message:
In grDevices:::png("/tmp/RtmpUBSVJV/87eb593bd19f4d58977ff56c6c73f42e.png",  :
  unable to open connection to X11 display ''

@kevinushey
Copy link
Contributor

Is R compiled with support for Cairo? What is the output of capabilities()? What is the output of Sys.getenv("DISPLAY")?

@OfekShilon
Copy link

OfekShilon commented Apr 11, 2021

@kevinushey They seem Ok:

> capabilities()
       jpeg         png        tiff       tcltk         X11        aqua    http/ftp     sockets      libxml        fifo      cledit       iconv 
       TRUE        TRUE        TRUE        TRUE       FALSE       FALSE        TRUE        TRUE        TRUE        TRUE        TRUE        TRUE 
        NLS     profmem       cairo         ICU long.double     libcurl 
       TRUE       FALSE        TRUE       FALSE        TRUE        TRUE 
> Sys.getenv("DISPLAY")
[1] ":0"

I can also plot successfully from naked R (on the same machine and user).
I can also touch files successfully on the tmp folder used.

@enbrown
Copy link

enbrown commented Apr 16, 2021

Under the Tools menu is the Global Options... command that lets you set the graphics backend (under the General section, Graphics tab). Changing the graphics device backend to "Cairo" made this error go away for me.

@OfekShilon
Copy link

OfekShilon commented Apr 18, 2021

@enbrown Thanks! It just reproduced again, and your advice solved it.

@OfekShilon
Copy link

OfekShilon commented Apr 18, 2021

@kevinushey I believe I have a consistent repro for this:
(1) Clone data.table,
(2) Build it via RStudio over r-server (in the Build pane, "More"/"Clean and rebuild"
(3) As part of the build the R session is restarted. In this R session I type plot(1:10,1:10) and see -

Error in RStudioGD() :
Shadow graphics device error: r error 4 (R code execution error)
In addition: Warning message:
In grDevices:::png("/tmp/Rtmprnne46/53ec1414d2334c4d860d0015571cbca0.png", :
unable to open connection to X11 display ''

Merely opening "Tools/global options/General/Graphics" and clicking 'Ok' (Backend Cairo was already selected) solves it.

@kevinushey
Copy link
Contributor

@OfekShilon I wasn't able to reproduce locally, but it might be useful to see how png is being invoked. Try running:

trace(grDevices:::png, quote({
  print(ls.str())
  print(Sys.getenv("DISPLAY"))
}))

and then try to reproduce your issue after plotting.

@OfekShilon
Copy link

@kevinushey Here are the tracing results:

> plot(1:10,1:10)
Tracing grDevices:::png("/tmp/RtmpJdkjin/35e149a37fba4fa6b01759c5bf1ba7bd.png",  .... on entry 
antialias : <missing>
bg :  chr "white"
filename :  chr "/tmp/RtmpJdkjin/35e149a37fba4fa6b01759c5bf1ba7bd.png"
height :  num 440
pointsize :  num 12
res :  num 96
type :  chr [1:4] "cairo" "cairo-png" "Xlib" "quartz"
units :  chr "px"
width :  num 803
[1] ":0"
Error in RStudioGD() : 
  Shadow graphics device error: r error 4 (R code execution error)
In addition: Warning message:
In grDevices:::png("/tmp/RtmpJdkjin/35e149a37fba4fa6b01759c5bf1ba7bd.png",  :
  unable to open connection to X11 display ''

It still reproduces consistently for me. If there's more stuff you want to check I'll be happy to try.

@kevinushey
Copy link
Contributor

Weird... does it also reproduce if you use a plain old call to e.g. png(tempfile())?

@OfekShilon
Copy link

@kevinushey yep:

> png(tempfile())
Error in .External2(C_X11, paste0("png::", filename), g$width, g$height,  : 
  unable to start device PNG
In addition: Warning message:
In png(tempfile()) : unable to open connection to X11 display ''

@kevinushey
Copy link
Contributor

Then whatever happens seems to be independent of RStudio. I'd use the R debugger to step through grDevices:::png and see why it's trying to create an X11 window. (This would seem to imply either that type is not "cairo" or that R believes capabilities("cairo") is FALSE)

@OfekShilon
Copy link

@kevinushey Indeed, on a 'good' machine type was cairo and on a 'bad' one it was xlib. type is set to getOption("bitmapType").
In naked R from the terminal on both machines, getOption("bitmapType") returns xlib.
Is RStudio supposed to set it to cairo? Where? Perhaps this fails in some flow?

@OfekShilon
Copy link

@kevinushey Another bit:
On the 'bad' machine (where png(tempfile()) fails and getOption("bitmapType")=="xlib", currently plot succeeds. Breaking in grDevices::png, it seems that type is passed as an argument, not by the option, and is set to cairo. I have no visibility into who passes it and what is its internal logic.

@honghh2018
Copy link

honghh2018 commented Jun 8, 2021

Hi @ALL,
I got the same issue, when i change the R path from where i install through source code compiled by editting the rstudio configure '/etc/rstudio/rserver.conf' rsession-which-r=/usr/local/R/bin/R .
The error showing below:
Error in RStudioGD() :
Shadow graphics device error: r error 4 (R code execution error)
In addition: There were 50 or more warnings (use warnings() to see the first 50)

type = "cairo" is unavailable. trying "Xlib"
50: In grDevices:::png("/tmp/Rtmp4yZP1J/359f32a28ee244cc900a15944d9bf3b9.png", ... :
type = "cairo" is unavailable. trying "Xlib"

I change the access of /tmp file with 777, but it doesn't work.
In another situation, it would work when i restore the default R path /usr/bin/R.
Any advice would be appreciated.
hanhuihong
The rstudo version was latest: rstudio-server-rhel-1.4.1717-x86_64.rpm insatall on CENTOS 7.7 platform.

@ronblum ronblum removed the info needed Additional information requested—reprex, steps, open question, etc. label Jun 8, 2021
@ronblum ronblum added this to the 1.6 (Q4 2021) milestone Jun 8, 2021
@ronblum ronblum reopened this Jun 8, 2021
@valerie-rstudio
Copy link

QA Note

I am currently unable to reproduce this issue.

@mikebessuille mikebessuille removed this from the 1.6 (Q4 2021) milestone Jun 28, 2021
@mikebessuille
Copy link
Contributor

This has not been reproed internally, and Kevin believes this is a problem in R, not RStudio. Closing this issue.

@qingjian1991
Copy link

Under the Tools menu is the Global Options... command that lets you set the graphics backend (under the General section, Graphics tab). Changing the graphics device backend to "Cairo" made this error go away for me.

Thanks, this works for me

@tanjialing
Copy link

The fix for this, #6722, should be in the 1.4 Wax Begonia dailies. Note, though, that the dailies are meant for testing purposes only.

thank you very much!it worked!

@alexandramarcos1896
Copy link

Rtmp0kHPQi

It works for me. Thank u

@MJimitater
Copy link

@werner-rammer How did you re-create the tmp folder please? Delete it and re-run command that lets it re-create it?

@werner-rammer
Copy link

@werner-rammer How did you re-create the tmp folder please? Delete it and re-run command that lets it re-create it?

Well, the error message refers to a directory which does not exist anymore. Go the the folder, create a directory with the name given by the error message, and run the R code again. This worked for me on Windows.

@rwitkowski3
Copy link

Under the Tools menu is the Global Options... command that lets you set the graphics backend (under the General section, Graphics tab). Changing the graphics device backend to "Cairo" made this error go away for me.

This worked!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.