Unable to generate PDF with neural network graph - r

I'm trying to create a hard-copy image of a neural network graph and it keeps failing. If I try to create a PNG, nothing is generated, and if I try to generate a PDF I get a small file output that refuses to open with "file may be damaged" errors. If I just let it display in a graphics window, the image comes up fine.
I'm using 2.15.1 on OS X (10.7.4), built by Macports. The code I'm working with at the moment:
library(ALL)
library(neuralnet)
data(ALL)
ALL.pdat <- pData(ALL)
bt <- factor(substring(ALL.pdat$BT,1,1))
all.sds <- apply(exprs(ALL),1,sd)
top.10.sds <- rank(all.sds)>length(all.sds)-10
exprs.top.10 <- as.data.frame(t(exprs(ALL)[top.10.sds,]))
nn.data <- cbind(exprs.top.10, as.numeric(bt))
## Gene names start with a number, and that causes problems when trying to set up the
## formula for neuralnet.
col.names <- paste("g", colnames(nn.data), sep = '')
col.names[11] <- "bt"
colnames(nn.data) <- col.names
my.nn <- neuralnet(bt ~ g36108_at + g36638_at + g37006_at + g38096_f_at + g38319_at + g38355_at + g38514_at + g38585_at + g39318_at + g41214_at, nn.data, hidden = 10, threshold = 0.01)
summary(my.nn)
pdf("./nn-all.pdf")
plot.nn(my.nn)
dev.off()
png("./nn-all.png")
plot.nn(my.nn)
dev.off()
I've even rebooted the machine to make sure that all the memory is cleared up, and that didn't help any.

Simple reproducible example:
pdf("test.pdf")
set.seed(42)
plot(runif(20),rnorm(20))
png("test.png")
set.seed(42)
plot(runif(20),rnorm(20))
dev.off()
If I try to open the PDF with Adobe Reader on my German Windows 7, I get a nice informative error message telling me that the file cannot be opened because the file is in use by another application. This can be fixed easily:
pdf("test.pdf")
set.seed(42)
plot(runif(20),rnorm(20))
dev.off() #make sure to close the graphics device
png("test.png")
set.seed(42)
plot(runif(20),rnorm(20))
dev.off()
Edit:
The problem is plot.nn. Until the package gets patched, you need to redefine plot.nn manually as shown in this answer.

Related

Problem with getting an image of a graph made in ggplot2 [duplicate]

In R, I use function savePlot to save graphs into image files. But my colleague can only open .jpgs and .gifs (probably because he's on vacation, reading emails on his mobile phone). I hate to create jpegs because especially the boxplots looks very ugly (whiskers blurred etc.). But the savePlot function only supports the following types:
type = c("wmf", "emf", "png", "jpg", "jpeg", "bmp",
"tif", "tiff", "ps", "eps", "pdf")
How can I save plot in GIF in R?
EDIT: if possible, ideal solution should work without installing ImageMagick (so that the R script is easily portable).
R doesn't have a native GIF graphics driver, mostly (completely?) due to the patent-encumbrances of the GIF format: see http://tolstoy.newcastle.edu.au/R/help/05/02/12809.html .
There is a function in the caTools package (write.gif()) but it is specifically designed for writing images. If you wanted to use it you have to do something hacky to convert your plot to an image first (e.g. save as PNG and then read it back into R as an image). For example:
png("myPlot.png")
plot(rnorm(1000),rnorm(1000))
dev.off()
library(png)
P1 <- readPNG("myPlot.png")
library(caTools)
write.gif(P1,"myPlot.gif")
showGIF <- function(fn) system(paste("display",fn))
showGIF("myPlot.gif")
unlink("myPlot.gif") ## clean up
?write.gif() has a lot of stuff about color indexing that I didn't read but that might be important for more complex graphs ...
The animation package has a saveGIF() function to save GIFs, but (1) it is designed for saving multi-frame animations (not general graphics), and (2) it does it by calling ImageMagick.
It's easier just to construct that function yourself.
install ImageMagick (http://imagemagick.org)
save as a PNG, then use ImageMagick to convert.
For example:
png("myPlot.png")
plot(rnorm(1000),rnorm(1000))
dev.off()
system("convert myPlot.png myPlot.gif")
unlink("myPlot.png") ## clean up
showGIF("myPlot.gif")
unlink("myPlot.gif") ## clean up
Of course you can either of these in a function if you want to use them regularly.
UPDATE: I spent a while longer on this, to try to get a pure-R solution, but don't yet have a working solution. Suggestions or edits welcome ...
## needs ImageMagick: just for testing ...
showGIF <- function(fn) system(paste("display",fn))
The main function:
saveGIF <- function(fn,verbose=FALSE,debug=FALSE) {
require(png)
require(caTools)
tmpfn <- tempfile()
on.exit(unlink(tmpfn))
savePlot(tmpfn,type="png")
P1 <- readPNG(tmpfn)
dd <- dim(P1)
P1 <- aperm(P1,c(3,1,2),resize=TRUE) ## P1[,1,15]
dim(P1) <- c(dd[3],prod(dd[1:2]))
P1 <- t(P1)
if (verbose) cat("finding unique colours ...\n")
P1u <- unique(P1)
rgbMat <- function(x) {
rgb(x[,1],x[,2],x[,3])
}
if (verbose) cat("creating colour index ...\n")
pp <- paste(P1[,1],P1[,2],P1[,3],sep=".")
## make sure factor is correctly ordered
ind <- as.numeric(factor(pp,levels=unique(pp)))
if (verbose) cat("finding colour palette ...\n")
if (nrow(P1u)>256) {
if (verbose) cat("kmeans clustering ...\n")
kk <- kmeans(P1u,centers=256)
ind <- kk$cluster[ind]
pal <- rgbMat(kk$centers)
} else {
pal <- rgbMat(P1u)
}
## test:
if (debug) {
dev.new()
par(mar=rep(0,4))
image(t(matrix(ind-1,nrow=dd[1])),col=pal,axes=FALSE,ann=FALSE)
}
if (verbose) cat("writing GIF ...\n")
indmat <- matrix(ind-1,nrow=dd[1])
storage.mode(indmat) <- "integer"
write.gif(indmat,fn,col=as.list(pal),scale="never")
}
X11.options(antialias="none")
image(matrix(1:64,nrow=8),col=rainbow(10))
saveGIF("tmp.gif",verbose=TRUE,debug=TRUE)
showGIF("tmp.gif")

Task Schedular Giving Error while scheduling R Script for saving PDF

I am scheduling R script which contains ggsave for saving pdf.
my code is running but on the line of ggsave("plot.pdf), it is skipping code. But instead of saving pdf if i use png format then it is fine. but only for pdf it is giving problem.
Below is my sample code.
library(ggplot2)
library(data.table)
a <- data.frame(a = c(1:5))
p <- ggplot(data.frame(x = 1:5, y = 1:5), aes(x, y)) + geom_point()
fwrite(a,"abc1.csv")
ggsave("plot.pdf")
Does ggsave(p, "plot.pdf", device = "pdf") work? You may not have been specifying the plot to be saved or perhaps it doesn't know to export as pdf from only the file path that you gave?
EDIT: It should be ggsave("plot.pdf", p, device = "pdf") so that the arguments are in the correct order.

Use of recordPlot() and replayPlot() in Parallel in R to save plot in the same PDF

I would like to plot data in parallel using foreach in R but I didn't find any way to get all my plots in the same pdf file. I thought of using recordPlot to save my plots in a list and then print them in a pdf device but it doesn't work.
I have the following error :
Error in replayPlot(x) : loading snapshot from a different session
I tried as well with ggplot but this is to slow with my large dataset.
Here is a piece of code showing my problem :
# Creating a dataframe : df
df=as.data.frame(matrix(nrow=1, ncol=10))
df=apply(df, 2, function(x) runif(100))
# Plotting function
par.plot=function(dat){
plot(dat)
p=recordPlot()
return(p)}
#Applying the function in parallel
library("parallel")
library("foreach")
library("doParallel")
cl <- makeCluster(detectCores())
registerDoParallel(cl, cores = detectCores())
plot.lst = foreach(i = 1:nrow(df)) %dopar% {
par.plot(df[i,])
}
# Trying to get 1st plot
plot.lst[[1]]
Error in replayPlot(x) : loading snapshot from a different session
Replacing %dopar% by %do% is working when I try to get my plots, because they seems to have been generated in the same environment.
I know I can call a pdf device inside the loop to generate a file for each iteration, but I would like to know if there is a way to get one file for all my plots at the output of my function.
Or do you know an easy way to merge my pdf files afterwards ?
Thanks for your help.
Charles
In my opinion your question can be devided into two distinctive parts:
1. Using the replayPlot function in th%dopar% without getting the weird error
2. Somehow getting 1 file at the end
The first question is easy to answer. The reason you get this error is that the R somehow remembers where (in OS level) the plots has been generated. You can get the same effect by using Rstudio server and trying to replay some of the recorded plots after couple of hours of closing the browser tab. In brief, the issue is that R remembers the PID of the process that generated the plot (Don't know why though!):
# generate a plot
plot(iris[, 1:2]
# record the plot
myplot <- recordPlot()
# check the PID
attr(x = myplot, which = "pid")
the good thing is you can overwrite this by assigning your current PID:
attr(x = myplot, which = "pid") <- Sys.getpid()
so you should only change the last line of your code to the following:
pdf(file = "plot.lst.pdf"))
graphics.off()
lapply(plot.lst, function(x){
attr(x = x, which = "pid") <- Sys.getpid()
replayPlot(x)})
graphics.off()
The part above entirely solves your problem, but in case you are interested in merging PDF files, follow this discussion:
Merging existing PDF files using R

How to plot and save it inside a R function in R in mac

My question is that I want to save a plot inside an overall big function to summarize result. However, when I do not put my plotting command in the big function, it works great, but when I run the big function, then I my plot can not be opened because they are damaged. Is there any options I can achieve my goal? Neither of the two options I provide below works.
Thanks guys!:)
I am using a mac machine with yosemite system
Here is my code:
library(lattice)
poissonICARMCMCPost = function(overallRes, preProcessData,path){
####Posterior part#########################################
### get the posterior information from the posterior samples
result = overallRes$result
resultSubset = overallRes$resultSubset
quartz()
acfplot(resultSubset)
dev.copy2pdf(file = paste(path, "acfplot", ".pdf", sep=""))
dev.off()
}
I have also tried
poissonICARMCMCPost = function(overallRes, preProcessData,path){
####Posterior part#########################################
### get the posterior information from the posterior samples
result = overallRes$result
resultSubset = overallRes$resultSubset
pdf(paste(path, "acfplot", ".pdf", sep=""))
acfplot(resultSubset)
dev.off()
}
Thanks for MrFlick's great comment, I find a solution that works for now:
poissonICARMCMCPost = function(overallRes, preProcessData,path){
####Posterior part#########################################
### get the posterior information from the posterior samples
result = overallRes$result
resultSubset = overallRes$resultSubset
quartz()
print(acfplot(resultSubset))
dev.copy2pdf(file = paste(path, "acfplot", ".pdf", sep=""))
dev.off()
}

Exporting rgl.snapshot and rgl.postscript fails

I am currently using the rgl package for some data representation.
Here's my command
mypath("directory")
png(file=mypath, res=600, width=10.5, height= 10.5,units="in",bg = "transparent")
require(rgl)
set.seed(1)
df <- data.frame(replicate(4,sample(1:200,1000,rep=TRUE)))
colnames(df) <- c("var1","var2","var3","var4")
plot3d(x=df$var1, y=df$var2, z=df$var3, col=as.numeric(df$var4), size=0.5, type='s',xlab="var1",ylab="var2",zlab="var3")
rgl.snapshot(mypath)
The command above works and produces a tiny image, which I wasn't able to make bigger, or increase its resolution (to 600).
I have also tried to export a pdf using:
rgl.postscript(mypath, fmt="pdf")
but when I execute the command R goes into a "not responding" state.
Can somebody please show me how to properly export the file? I would prefer the have the PNG with the resolution 600 dpi.
Cheers,
A solution can be to set the size of the window using open3d() :
require(rgl)
set.seed(1)
df <- data.frame(replicate(4,sample(1:200,1000,rep=TRUE)))
colnames(df) <- c("var1","var2","var3","var4")
open3d(windowRect=c(100,100,700,700))
plot3d(x=df$var1, y=df$var2, z=df$var3, col=as.numeric(df$var4), size=0.5, type='s',xlab="var1",ylab="var2",zlab="var3")
rgl.snapshot(<path to png file>)
May be someone need. I used the following combination for persp3Drgl:
persp3Drgl(...)
par3d( windowRect=c( 0,0,100,100 ) )
snapshot3d( file.path(plotDir, "3D.png"), top = TRUE )
Without top = TRUE it got failed.

Resources