Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem creating dynamic file name in R

Tags:

r

I'm working on a script in R that processes some data and writes an output file. I'd like that output file to be named in a way that reflects the input file, and I'd like something about the file to be unique so older files aren't overwritten.

So I thought to use a timestamp. But this isn't working the way I'd hoped, and I'd like to understand what's happening and how to do this correctly.

This is how I'm trying to name the file (file_base is the name of the input file):

now<-format(Sys.time(), "%b%d%H%M%S")
outputfile<-cat(file_base, "-",now,"-output.txt", sep="")

The output of this pair of functions looks great. But executing 'outputfile' subsequently results in 'NULL' as output.

What's happening here and how can I create an output filename with the properties that I'd like?

like image 458
J Miller Avatar asked Aug 13 '10 17:08

J Miller


People also ask

How do I get the filename in R?

To list all files in a directory in R programming language we use list. files(). This function produces a list containing the names of files in the named directory. It returns a character vector containing the names of the files in the specified directories.

What is a dynamic file name?

A dynamic file name uses an expression to generate the file name. You can use a dynamic file name to create a new target file every time the. mapping. task runs. For example, the following expression creates a file called "OrdersOut_<system_timestamp_with_second_precision>.csv" each time the.

How do I put multiple files in different names in R?

If you are creating multiple datasets in R and wish to write them out under different names, you can do so by looping through your data and using the gsub command to generate enumerated filenames.


1 Answers

You're confusing cat and paste. You want:

outputfile <- paste(file_base, "-",now,"-output.txt", sep="")
like image 187
Joshua Ulrich Avatar answered Oct 14 '22 06:10

Joshua Ulrich