2.1 Rstudio workflow
As we’ve seen, RStudio has both a scripting pane to write code, and a console pane to run code. Of course, you can write code directly into the console, but it is not a good practice. You will tend to get sloppy, lose the “story”, and generally have less reproducible code.
Writing the program (“story”) is just more reliable if you write into the script file and the send it to the console to run. Sending it to the console can be acheieved with a keyboard shortcut, Ctrl-Enter (or Cmd-Enter on a Mac). This is something that will be second nature while coding in RStudio.
When you write code, be sure to comment your code liberally. In R, any line or any phrase starting with #
is considered a comment and is ignored by the program. This allows you to comment your code, explain your ideas to yourself and generally make your code more readable. To further this goal, write your code in differnt lines, and indent, to make it more readable; R ignores white space in your file.
Why bother doing this? Basically because the most likely next person to see your code is going to be you in 6 months, and you don’t want to be scratching your head wondering what you were doing earlier (been there, done that, don’t like it). You certainly can’t phone your earlier self, so the best strategy is to write comments for your future self to minimize future grief.