Adding Comments to CSV Files

[This article was first published on Quintuitive » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
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 ) ) 

To leave a comment for the author, please follow the link and comment on their blog: Quintuitive » R. 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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)