Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call plot() from an R script and get the graph in the output file?

Tags:

graph

plot

r

I have created an R script that reads certain data from a file, calls the summary() method and then the plot() method.

But when I try to run the R script where the commands below are written, in the output file I get the summary, but not the graph.

When I run the following instructions in R manually, everything works perfectly, and I get both the summary and the graph.

Is there a way to get the graph in the output file?

m0<-read.csv(file="Myfile", head=FALSE, sep",")
var_m0<-c(m0$ V3)
summary(var_m0)
plot(var_m0)

Thanks!

like image 721
FranXh Avatar asked Aug 01 '12 19:08

FranXh


1 Answers

You need to tell R what kind of output you want and where you want it to go. Take a look at ?png for a fairly comprehensive list. And don't forget dev.off() after your plot() call!

m0 <- read.csv(file="Myfile", head=FALSE, sep",")
var_m0 <- c(m0$ V3)
summary(var_m0)

png('plot.png')
plot(var_m0)
dev.off()

If you specifically want the graph in the same output file as the rest of the code, you can look at knitr and sweave.

like image 166
Justin Avatar answered Oct 24 '22 09:10

Justin