开发者

problem creating dynamic file name in 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 'ou开发者_开发百科tputfile' 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?


You're confusing cat and paste. You want:

outputfile <- paste(file_base, "-",now,"-output.txt", sep="")


You can also use the function sprintf(), it's a wrapper for the C function. example:

filepath <- file.path(outdir, sprintf("abcdefg_%s.rda", name))


You could also use the separator argument of paste:

outputfile <- paste(file_base,now,"output.txt", sep="-")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜