课程: R for Data Science: Lunch Break Lessons

Style guides

- [Instructor] As you learn to program in R, you're going to more and more frequently work with other people. It's gonna be helpful if everyone is using the same punctuation and grammar and style guide for writing R. So the question is, which style should I use? Well, it turns out there are several. There's the R Style by Paul Johnson. There's the Google Style Guide by Google. There's the Advanced R Style Guide from Hadley Wickham. There's the Tidyverse Style Guide, also by Hadley Wickham, and of course, there's the aroma coding style. So these are all different examples of coding styles you can use with R. Now, there is a tool that you can use to help you with that. It's called lintr and let's take a look at that. Lintr is a package, so you'll need to install it. That's easy. And spelled L-I-N-T-R. When the package is installed, you can use the library command to bring it into activity. Using lint with R is easy and it's especially easy with RStudio. Let's bring in all of the panes for RStudio and use lint to check a file called 01_06_lists.R. And I type in lint and then the file name. Now, when I hit return what's going to happen is another window will open up. It's called Markers. It's within the RStudio environment and it lists all of the things that lint thinks should be changed about that source. The first line says, in line four, variables should use under bars rather than dots. Well, we can either go to the source and look for line four. And sure enough, I've used dots instead of underlines, or in RStudio I can double click on the line that tells me the error and RStudio will bring the cursor to that line for me. So here's line five, we have the same problem. We have variables with dots instead of Rs. I double click on that and I'm brought right to line five where I can change the problem. RStudio even does deeper integration that this. If you go to the Tools, Global Options, Code, Diagnostics tab, you'll see that there are several check boxes where you can control the behavior of RStudio as it checks code for you. You'll want to experiment with this and particularly check with the people you work with to see which of these things are important to their coding style.

内容