Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
By Steph Locke
We polled our consultants for their tips on how to be more effective at writing R code; and here are the top 10!
10. Don’t pre-emptively tidy
Spending time on formatting as you go along gives you a cause to procrastinate plus it’s hard to make things consistent.
Instead of worrying about it, let a linter like formatR do it for you and then do any minor tweaks required.
9. Use an IDE
You might be hardcore if you code directly in the R GUI or in Notepad but Interactive Development Environments (IDEs) like Rstudio make it much easier to write code quickly. The code-completion alone will save lots of time.
8. Learn your hotkeys
There’s a never-ending list of hotkeys for your OS, and for IDEs like Rstudio. The amount of time you can save by never taking your fingers off the keyboard really mounts up so `Ctrl+S` this tip to your memory.
7. Plan twice, code second
Making sure you understand thoroughly what you’re trying to achieve will make coding it much easier and quicker. Doing things like writing your comments first can really help (and give you a taste of your progress).
# Step 1: Get data
# Step 2: Clean up data issues
# 2a: Remove excess rows
# 2b: Correct case issues in columns x,y,z
# Step 3: Apply glm function
# Step 4: Output validation components
6. Don’t be afraid to google
Building everything from first principles is reinventing the wheel. You may learn a lot but you take longer and can’t guarantee you’ve done it the most sensible way.
See if there’s a canonical solution, some common pitfalls, or simply some useful things to consider.
5. Don’t Repeat Yourself (DRY)
You’ve probably heard this before, but with the ease that R can make functions, split repetitious code into it’s own functions.
Check out the *apply functions as well to help avoid C&P.
4. Use the right tool for the job
Don’t let R become your proverbial hammer. Evaluate the needs of a project and use the appropriate language – a little bit of learning now can save a lot of pain later!
3. Write to facilitate tests
You should be testing your code, and to make that as quick as possible it has to be easy so when you write code, incorporate components like verbose outputs and plenty of validation. Avoid building functions that have “side effects” and putting things into the global environment.
2. Document as you go
Documentation rarely happens if left to the end – so as you’re writing your code, take the time to pen the vignettes and the roxygen headers as you go along.
1. Source control
Be the bigger Git!
Using source control (Git or SVN) helps you maintain your versions of code, and allows you to develop in a team more effectively.
Conclusion
We think that code that’s tracked, well documented, and easily tested is super valuable. Taking the time to do these things will set you apart from the pack and “Future You” will love you for it.
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.