remykarem
remykarem

Reputation: 2490

Batch File Produces Blank PNG Images

I am running some plots on R. When I run the source code from RStudio, I get the output images as expected. However, when I run the source code from a .bat file, the output images are blank.

runPlots.R

dev.copy(png, "image.png")
dev.off()

runPlots.bat

@echo off
title Run plots
"C:\Program Files\R\R-3.4.3\bin\x64\Rscript.exe" "D:\...\runPlots.R"

Is there something that I am missing here? Greatly appreciate your help!

Upvotes: 2

Views: 1003

Answers (1)

Stéphane Laurent
Stéphane Laurent

Reputation: 84599

Do like this:

png("image.png")
plot.igraph(....)
dev.off()

And this works.

Explanation:

When you do

plot.igraph(......) 
dev.copy(png, "image.png")
dev.off()

then dev.copy copies the image displayed by the graphical window of RStudio. That does not work in batch mode.

Other possibility:

Thanks to @Eumenedies's comment. You can open a graphical window with the command windows() on Windows and x11() on Linux. Then this code works when it is ran from a batch file:

windows()
plot(.......)
dev.copy(png, "image.png")
dev.off()

Upvotes: 3

Related Questions