jeudi 23 avril 2015

Using R to writing a function to export a PDF graph, but PDFs are corrupted

I have written this function to export graphs in R. When I run this function the pdf files are created, but they are corrupted and won't open (or when I made it export png file the files are empty). When I run the individual lines of code, not as part of the function they work.

Help!! I think it must be something to do with making the tables or graphs in the function, (do I have to do something like return the graphs/data frames?) but I don't know what!

    graphFunction <- function(fileName){

pdfTitle <- paste(fileName, "_graph", ".pdf", sep = "")

File <- read.csv(fileName, quote = "")
ByPopAge<- ddply(File, .(age), summarise, # need to discount trials where no feeding obs and eve
        NCols = length(!is.na(colony_ID)),
        TotNumInd = sum(num.indvs)
)

pdf(pdfTitle, width =10, height =10)

ggplot(data = ByPopAge, aes(x = age, y = TotNumInd)) + geom_line() + geom_point()   
ggplot(data = ByPopAge, aes(x = age, y = NCols)) + geom_line() + geom_point()

dev.off() }

Thanks!

Aucun commentaire:

Enregistrer un commentaire