3

I'm creating a script to cluster my data in a server. I need to save the text output and the images as well. The text output works just fine but when I try to use the png() + plot() + dev.off() thing to save the plots, no image is created.

[ADDED FOR CLARIFICATION]

What I need to do is to SAVE the plot (i.e. generate an image file) in running mode. If I run the code step by step, the file is created.

I already tried to change the image format to PDF and JPG using the corresponding functions but I'm still getting no images as output when running the code as script. When stepping, it works great.

Since it takes a little while for R to render the image when I'm running step by step, I tried to add Sys.sleep(2) in between commands (code below) but nothing changed.

I think the problem might be related to the package that I'm using and the type of object it generates (library(NMF)). I looked at the documentation to see if there was something about the way the plot() function works with the type of object that the clustering algorithm generates but the text is vague:

"The result (of estim.r <- nmf(esGolub, 2:6, nrun=10, seed=123456) for example) is a S3 object of class NMF.rank, that contains a data.frame with the quality measures in column, and the values of r in row. It also contains a list of the consensus matrix for each value of r".

"All the measures can be plotted at once with the method plot (Figure 1), and the function consensusmap generates heatmaps of the consensus matrix for each value of the rank".

There is another type of image that can be generated after the clustering runs: the consensusmap. This one works on both cases (stepping and running).

The script is pretty short. Here it is:

library(NMF)
data = read.csv('R.csv', header=TRUE, sep=";")
res1 <- nmf(data, rank=2:5, nrun=1, "brunet", "random")

# this always works
capture.output(summary(res1) ,file = "summary.txt", append = TRUE)

# this always works too
png(filename = 'consensus.png', width = 1366, height = 768, units = 'px')
consensusmap(res1)
dev.off()

# this does not work on 'running mode', only 'stepping mode'
png(filename = 'metrics.png', width = 1366, height = 768, units = 'px')
# added hoping it would fix the issue. It didn't
Sys.sleep(2)
plot(res1)
# added hoping it would fix the issue. It didn't
Sys.sleep(2)
dev.off()

The summary.txt file is generated, the consensus.png too. The metrics.png is not. What's going on here??

TheDuckman
  • 31
  • 4
  • 2
    Can you try print(plot(res1))? – Roland Apr 10 '19 at 15:19
  • @Roland is right. The `plot` method in `NMF` uses `ggplot()` to do the plotting, and it only does the plotting when the result is printed. I think you could get the same result using `source("script.R", echo = TRUE)`, because that does auto-printing. – user2554330 Apr 10 '19 at 16:31
  • @Roland I think maybe I wasn't clear on one point: I won't be running this code on my machine. It'll run on a server so I don't have access to a GUI. I need to automatically save the plot. Plotting and then saving is not an option. – TheDuckman Apr 15 '19 at 13:00
  • I think maybe you are not understanding my point. I believe you are running into a [FAQ regarding grid (and thus ggplot2) graphics output](https://stackoverflow.com/q/26643852/1412059) as described by user2554330. – Roland Apr 15 '19 at 14:01
  • Ok, NOW I get it. and it worked! The solution mentioned by @user2554330 did the trick. Thanks a lot, guys! – TheDuckman Apr 22 '19 at 12:57

0 Answers0