2 min read

Script writing style

Style is important when writing script. I generally follow the tidyverse style guide and cannot recommend strongly enough that you do, too. I’ll admit that I’m definitely not perfect and have adopted some shortcuts the style guide says to avoid. However I do my best to not let those habits creep into script for class.

Two main points for now:

Firstly, realize that \({\bf\textsf{R}}\) ignores line breaks as long as they are preceded by a comma dividing arguments, as in vars(...), fun. I use a lot of line breaks. It makes my script files long, but so much easier to read. We tend to use smaller laptops with smaller screens and R studio’s panes further reduce the width of our coding window. Having to scroll far to the right just to see what’s in a line of code makes reading and debugging script difficult.

Secondly, when using many line breaks, I use indents to help keep track of which arguments go together. Here are two examples:

# Good:
  new_df <- 
    old_df %>%
      Function1(SubFunction(
                  SubFunArg1, 
                  SubFunArg2), 
                FunctionArg1,
                FunctionArg2) %>%
      Function2(Arg1, 
                Arg2)
# Bad:
  new_df <- 
      old_df %>% Function1(SubFunction(SubFunArg1, SubFunArg2), FunctionArg1,
      FunctionArg2) %>%
  Function2(Arg1, 
    Arg2)

Note that it is acceptable to gasp! reverse the assigner and put it at the end of your pipe:

old_df %>%
  Function1 %>%
  Function2 -> new_df

This really plays well with the image of pouring data down the pipe, but resist using it. It makes it very difficult to go back through the script to find where an object was created. Treat the new_df, with everything below it and indented in, as a heading that makes it easy to see what pipe created that object.

This convention makes it really easy to see where an object came from and what data went into it:

new_df <-
  old_df %>%