0

I'm trying to run this code in task manager. It runs successfully in Rstudio, but there is an error when I ran it in Task Manager. Here is the R code.

grDevices::dev.set(1)
library(ggplot2)

pdf(NULL)

options(bitmapType = 'cairo', device = 'pdf')

g <- ggplot()+geom_line(data = data.frame(a = 1:10, b = 21:30), 
                        aes(x = a, y = b))

ggsave('path/graph.pdf',
       g,
       device = 'png')

The error when I ran it in Task Manager looks like this:

Error in (function (file = if (onefile) "Rplots.pdf" else "Rplot%03d.pdf", : cannot open file 'Rplots.pdf' Calls: -> Execution halted

The post below talks about the vanilla options when calling Rscript...but I couldn't figure out what the solution is...

Rscript ggplot - ggsave problem

  • Is it possible you have that file open? I have had ggsave errors like that when I have been viewing the file. Also, why are you using a png device to save a pdf? – Jon Spring Mar 08 '19 at 21:35
  • I never attempted to open it... unless ggsave() opens it in the back end.. I want to save because I'm including that in an email body... this is just part of my email code that causes problem – littleturtle Mar 08 '19 at 21:37

1 Answers1

0

This helped.

grDevices::dev.set(1)
library(ggplot2)

pdf(NULL)

options(bitmapType = 'cairo', device = 'pdf')

g <- ggplot()+geom_line(data = data.frame(a = 1:10, b = 21:30), 
                        aes(x = a, y = b))

ggsave(tf<-tempfile(fileext = ".png"),
       g,
       device = 'png')