A blog by Rob J Hyndman 

Twitter Gplus RSS

Organization and R

Published on 6 September 2007

Many R users seem to get them­selves in a bit of a mess with R files and work­spaces scat­tered across dif­fer­ent direc­to­ries. The R files them­selves also get messy and hard to fol­low. So here is some advice on keep­ing orga­nized with R:

  • Try to keep code strictly indented based on the code struc­ture such as loops, if state­ments, etc. Every left brace { should be fol­lowed by an extra level of inden­ta­tion which con­tin­ues until the match­ing right brace }. You should be able to quickly iden­tify what lines are part of a loop, or are con­di­tioned by an if state­ment, sim­ply by the lev­els of indentation.
  • Com­ment copi­ously. You need to be able to fig­ure out what your code does in a year’s time.
  • Have a sin­gle direc­tory for each project. Within that, keep an R work­space, an R file con­tain­ing the func­tions you’ve writ­ten, and one or more R files con­tain­ing the code to read in the data, apply the func­tions to the data, plot some graphs, etc.
  • Don’t have mul­ti­ple ver­sions of essen­tially the same code. If you are doing sim­i­lar things to what you’ve done before, write a func­tion to do it and call it when required.
  • Have a main.R file which does all the analy­sis for the paper, chap­ter or report. It may sim­ply con­sist of source lines such as
  • source(“functions.R”)
    source(“readdata.R”)
    source(“fitmodel.R”)

    Then the whole project can be run by sourc­ing the main file. If you find an error in your data, or you get updated or revised data, it is then a sim­ple mat­ter of run­ning  main.R and all the graphs and analy­sis will be re-​​created.

  • Every graph and table to go into your writ­ten doc­u­ment should be cre­ated via code. Use savepdf() or saveeps() in the monash pack­age for graphs, and xtable() in the xtable pack­age for LaTeX tables. (For more com­pli­cated LaTeX tables, latex() in the Hmisc pack­age is also useful.)

Related Posts:


 
Tags:
No Comments  comments