[R] Save pdf to a relative directory?

David Winsemius dwinsemius at comcast.net
Thu May 6 22:48:38 CEST 2010


On May 6, 2010, at 3:06 PM, someone wrote:

>
> hi there
> im having a R script in which i produce soe plots that are saved to  
> a pdf
> specified by an absolute path...
> Is there a way to specify a relative path instead?
>
> 	pdfPlot("/Users/XXX/Desktop/R_script/plots/plot1", 8, 6, function(){
> 				plot_plot1(data)
> 		})

?getwd

Perhaps:
pdfPlot <- function(file, width, height, data=c(1,1), fun=plot,  
png=FALSE){
	# plots are saved to a PDF, parameters are listed
	# output file location, width, height, function to be plotted
	# e.g. pdfPlot("C:/..", 6, 5, function(){ ... })
	pdf(paste(file, ".pdf", sep=""), width=width, height=height)
	fun(data)
	dev.off()
	if (png){
		png(paste(file, ".png", sep=""), width=width*100, height=height*100)
		fun(data)
		dev.off()
	}
}
print(pdfPlot(file=paste(getwd(), "plot2", sep="/"), 8, 6,  
data=c(1,1), fun=plot))

>
> this is what I am doing at the moment but this will obviously just  
> work on
> my machine.
>
> pdfPlot <- function(file, width, height, fun, png=FALSE){
> 	# plots are saved to a PDF, parameters are listed
> 	# output file location, width, height, function to be plotted
> 	# e.g. pdfPlot("C:/..", 6, 5, function(){ ... })
> 	pdf(paste(file, ".pdf", sep=""), width=width, height=height)
> 	fun()
> 	dev.off()
> 	if (png){
> 		png(paste(file, ".png", sep=""), width=width*100, height=height*100)
> 		fun()
> 		dev.off()
> 	}
> }
>
> this is the plotting function...
>
> could anyone help me out and let me know how i could save the pdf to
> "workingdirectory"/plots/?
>
> -- 
> View this message in context: http://r.789695.n4.nabble.com/Save-pdf-to-a-relative-directory-tp2133206p2133206.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list