Main style things to pay attention to for this class
Important note: I won’t ever grade you on any of this! If you submit something like
filter(mpg,cty>10,class=="compact")
, I might recommend adding spaces, but it won’t affect your grade or points or anything.
Spacing
See the “Spacing” section in the tidyverse style guide.
Put spaces after commas (like in regular English):
# Good
filter(mpg, cty > 10)
# Bad
filter(mpg , cty > 10)
filter(mpg ,cty > 10)
filter(mpg,cty > 10)
Put spaces around operators like +
, -
, >
, =
, etc.:
# Good
filter(mpg, cty > 10)
# Bad
filter(mpg, cty>10)
filter(mpg, cty> 10)
filter(mpg, cty >10)
Don’t put spaces around parentheses that are parts of functions:
# Good
filter(mpg, cty > 10)
# Bad
filter (mpg, cty > 10)
filter ( mpg, cty > 10)
filter( mpg, cty > 10 )
Long lines
See the “Long lines” section in the tidyverse style guide.
It’s generally good practice to not have really long lines of code. A good suggestion is to keep lines at a maximum of 80 characters. Instead of counting characters by hand (ew), in RStudio go to “Tools” > “Global Options” > “Code” > “Display” and check the box for “Show margin”. You should now see a really thin line indicating 80 characters. Again, you can go beyond this—that’s fine. It’s just good practice to avoid going too far past it.
You can add line breaks inside longer lines of code. Line breaks should come after commas, and things like function arguments should align within the function:
# Good
filter(mpg, cty > 10, class == "compact")
# Good
filter(mpg, cty > 10,
class == "compact")
# Good
filter(mpg,
cty > 10,
class == "compact")
# Bad
filter(mpg, cty > 10, class %in% c("compact", "pickup", "midsize", "subcompact", "suv", "2seater", "minivan"))
# Good
filter(mpg,
cty > 10,
class %in% c("compact", "pickup", "midsize", "subcompact",
"suv", "2seater", "minivan"))
Pipes (%>%
) and ggplot layers (+
)
Put each layer of a ggplot plot on separate lines, with the +
at the end of the line, indented with two spaces:
# Good
ggplot(mpg, aes(x = cty, y = hwy, color = class)) +
geom_point() +
geom_smooth() +
theme_bw()
# Bad
ggplot(mpg, aes(x = cty, y = hwy, color = class)) +
geom_point() + geom_smooth() +
theme_bw()
# Super bad
ggplot(mpg, aes(x = cty, y = hwy, color = class)) + geom_point() + geom_smooth() + theme_bw()
# Super bad and won't even work
ggplot(mpg, aes(x = cty, y = hwy, color = class))
+ geom_point()
+ geom_smooth()
+ theme_bw()
Put each step in a dplyr pipeline on separate lines, with the %>%
at the end of the line, indented with two spaces:
# Good
mpg %>%
filter(cty > 10) %>%
group_by(class) %>%
summarize(avg_hwy = mean(hwy))
# Bad
mpg %>% filter(cty > 10) %>% group_by(class) %>%
summarize(avg_hwy = mean(hwy))
# Super bad
mpg %>% filter(cty > 10) %>% group_by(class) %>% summarize(avg_hwy = mean(hwy))
# Super bad and won't even work
mpg %>%
filter(cty > 10)
%>% group_by(class)
%>% summarize(avg_hwy = mean(hwy))
Comments
Comments should start with a comment symbol and a single space:
#
If the comment is really short (and won’t cause you to go over 80 characters in the line), you can include it in the same line as the code, separated by at least two spaces (it works with one space, but using a couple can enhance readability):
You can add extra spaces to get inline comments to align, if you want:
If the comment is really long, you can break it into multiple lines. RStudio can do this for you if you go to “Code” > “Reflow comment”
Though, if you’re dealing with comments that are that long, consider putting the text in R Markdown instead and having it be actual prose.