14.4 How to write good code/workflow!
- Q: What is your experience with looking at data analysis code you have written 2 years earlier?
- Comment your code
- Data sources, data manipulations, describe steps of analysis
- commenting increases readability
- commenting increases reproducability
- you can’t comment too much
- Comments in R: #
- Use meaningful names!
- Don’t be too clever!
- Structure you code properly
- Headlines
- Indentation (https://google-styleguide.googlecode.com/svn/trunk/Rguide.xml)
- Write robust syntax (e.g. relative paths)
- Follow the Google Style Guide for R https://google-styleguide.googlecode.com/svn/trunk/Rguide.xml
- More rules
- Code must be usable on machines with different paths to the project
- Path is set once and afterwards, relative paths are used
- There is a folder structure separating data and figures (and code)
- Code must be usable on machines with different paths to the project
For larger projects you might consider tools that keep track of versions, branches etc. like Git (http://www.github.com among others offers free git repositories). github can easily be used from within RStudio.
- More…
- See tips by Jonathan Nagler
- Try the formatR package: Online example
tidy_dir()
: Formats all R files in directory