[R] Generating unique filenames.

Ben Tupper btupper at bigelow.org
Wed Mar 6 15:33:37 CET 2013


Hello,

On Mar 6, 2013, at 9:20 AM, Sahana Srinivasan wrote:

> Hi,
> I am trying to create unique filenames for my output text file. The idea is
> that I would like to append a string to ".zsc.txt" so that all my files are
> uniquely named but with a similar format. I have tried adding the string
> variable to ".zsc.txt" while creating the output file name, i.e.
> write.table function, is what I have tried using :
> 
> write.table(x, file=str,".",".zsc.txt"....);
> 
> but it isn't working.
> 

I think you are looking for the paste() or the newish paste0() function to assemble the parts of your filename.  In the example below I make a unique name out of a timestamp and a path.  You might have a different unique name to use instead of timestamp.  Note, use file.path() to build up filename that includes a path description.

path <- "/my/own/path"
appendage <- ".zsc.txt"
string <- format(Sys.time(), format = "%Y-%j-%H%M%S")
outputFile <- file.path(path, paste0(string, appendage))
write.table(x, file = outputFile) 

Cheers,
Ben






> 
> 
> Would appreciate everyone's input on the matter. Thanks :)
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org



More information about the R-help mailing list