saving plots results in R code execution error

Hello,
I've been struggling with RStudio lateley. Whenever I want to save a plot (even the simplest ones) as a jpg, I get an "R code execution error".
Given that it worked before my PC had to restart for an update, I already tried shutting down and restarting my PC, as well as terminating and restarting R Sessions, I deleted R and installed it again, nothing helped and I don't know why.

I created a simple example with:

x<-c(1,2,3,4)
y<-c(2,4,6,8)

plot(x~y)

And the plot shows without Problems. But when I click "Export"->"Save As Image", without changing anythig I get:

Clicking "Save" just results in a notification that says: "R code execution error"

Please, if anyone knows how to fix this, help me. I'm going crazy

What happens if you try this?

x<-c(1,2,3,4)
y<-c(2,4,6,8)

png("pplot1.jpeg")
plot(x~y)
dev.off()

After typing: "png("pplot1.jpeg")"
I get:
"Error in png("pplot1.jpeg") : unable to start png() device
In addition: Warning messages:
1: In png("pplot1.jpeg") : unable to open file 'pplot1.jpeg' for writing
2: In png("pplot1.jpeg") : opening device failed"

What is your operating system, the version of RStudio and R that you are using?

Please run the command sessionInfo() and post the output here.

Edit
Try completely closing everything on your computer and reboot.

sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252
[2] LC_CTYPE=German_Germany.1252
[3] LC_MONETARY=German_Germany.1252
[4] LC_NUMERIC=C
[5] LC_TIME=German_Germany.1252

attached base packages:
[1] stats graphics grDevices utils datasets
[6] methods base

other attached packages:
[1] readxl_1.3.1

loaded via a namespace (and not attached):
[1] Rcpp_1.0.5 fansi_0.4.1 crayon_1.3.4
[4] assertthat_0.2.1 cellranger_1.1.0 lifecycle_0.2.0
[7] magrittr_1.5 pillar_1.4.6 rlang_0.4.7
[10] cli_2.0.2 rstudioapi_0.11 vctrs_0.3.2
[13] ellipsis_0.3.1 tools_4.0.2 glue_1.4.1
[16] compiler_4.0.2 pkgconfig_2.0.3 tibble_3.0.3

Hopefully you have seen my edit and have rebooted the computer. This often cures strange things.

If a reboot does not help we need to try and see if the problem is in RStudio or R.

Shut down RStudio and load R on its own. There should be an Icon on the desktop. Try running

x<-c(1,2,3,4)
y<-c(2,4,6,8)

png("pplot1.jpeg")
plot(x~y)
dev.off()

there. If you get an errer there, I am not sure what to suggest other than a new installation of R. I have not used Windows in at least 10 years so I cannot suggest any easier.
Best of luck

Unable to open file for writing sounds like you don't have permission to write to the directory. What happens if you supply a full path to a location where you know you have write permissions?

I'm not sure where RStudio thinks ~ is on a Windows machine, nor what the directory it is considering the current working directory for writing the file. Trying to rule out file permissions as the culprit by specifying a path that is known writable.

1 Like

So far the error occurs regardless of the directory, I tried saving in my documents file, in desktop...

1 Like

rebooting the Computer didn't work, but typing it in R did (meaning I have a "pplot1.jpeg" file), so the problem must be in RStudio, right? How do I go on?

Thank you so much by the way

I really have no idea. I use RStudio all the time but I am not much use at troubleshooting it.

Ah, wait a minute grosscol makes a very good point that I missed

It looks like a permissions problem. I never use the Export function and may have missed the blindingly obvious answer.

When you export check the directory you are writing to. The Directory button is in the upper left corner just below the Image Format. text. If necessary change it to a directory you know you have permission to write to and see what happens.

It seems to me RStudio has no permission to write to at all. I tried different directories, even just "C:/" and the plot that saved from R is saved in a directory that RStudio can't write to. Do you know where I can see and change RStudio's permissions?

Edit: The problem startet happening right after Windows needed an update, so I think this is likely

I have seen reports about all sorts of Windows 10 permissions problems but I have been using Linux for years so I cannot help .

Perhaps another reader here can or you may need to try a Windows forum.

Best wishes.

1 Like

You definitely helped diminishing the problem. Thank you :slight_smile:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.