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?
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.
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.
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.
You're confusing cat
and paste
. You want:
outputfile <- paste(file_base, "-",now,"-output.txt", sep="")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With