Adding Comments to CSV Files
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Various of my R scripts produce csv files as output. For instance, I run a lengthy SVM back test, the end result is a csv file containing the indicator with some additional information. The problem is that over time one loses track what exactly the file contained and what parameters were used to produce it.
I have considered various solutions. First, I do store some information in the file name. However, this has limited applications. Next I considered using file system attributes (getfattr, setfattr, etc). Although an exciting feature, it doesn’t seem to integrate easily with git.
The solution I ended up using is to add comments to the csv file. The implementation is surprisingly easy. R’s read.table supports comments starting ‘#’ by default. I only needed to create a decent function to write the comments:
writeIndicator = function( indicator, fileName, comments=c() ) { require( quantmod, quietly=TRUE ) # First write the comments append = FALSE for( comment in comments ) { line = paste( sep="", "# ", comment ) write( line, file=fileName, append=append ) append = TRUE } # Write the series, but suppress warnings to avoid the following: # Warning message: # In write.table(dx, file = file, row.names = row.names, col.names = col.names, : # appending column names to file suppressWarnings( write.zoo( indicator, quote=F, row.names=F, sep=",", file=fileName, append=append ) ) }
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.