Data science never sleeps. Stay on top of the latest trends, tools, and techniques with weekly tips designed to make data scientists better at their jobs.
- [Barton] Welcome to Data Science Tips, a Weekly Series brought to you by LinkedIn and lynda.com that has hands-on demonstrations and guidance for people who are working in Data Science. Brought to you by your host, Barton Poulson, author and advocate for Data Science. We hope you like what you see and we invite you to share these tips with others you might think might like them. Share on your Twitter, Facebook, LinkedIn, Slacker, however else you like using #datasciencetips. And check out our other LinkedIn lynda.com courses on Data Science and business analytics right here.
So thanks and let's get into this week's LinkedIn Data Science Tip. Organizing code in our RStudio is a great way to make your work in Data Science more effective and more efficient. Let's take a look at this script and see how it works. I'm going to make things here in RStudio a little easier by zooming in on the source pane. The key to all of this is using headers in RStudio. Headers allow folding and they allow you to use a document outline for navigation.
To make a header, you start with a pound sign and end with four or more pound signs, equal signs, or dashes. I prefer personally to use pound signs for all three levels for compatibility with Rmarkup. And I use, one, two or three leading pound signs to indicate levels along with changes in capitalization. Once you have your headers in place, you can open up the document outline by clicking on this button over here. Or by going to Document Outline in the menu or using the keyboard shortcut.
When the document outlines open you can navigate through your code by simply clicking on the various headers. Or you can use the Jump To menu right here at the bottom of the window. There's several different kinds of folding that you can do, that's collapsing and expanding code. You can fold braced regions in formulas, code chunks within R Sweave or R Markdown, text sections between headers within R Markdown and we're gonna do something similar here. Code sections and really any arbitrarily selected area of code.
In order to fold sections, there are a few methods available for folding sections. The easiest is to come over here into the margin and simply click on this arrow. And that collapses it and then you get this rectangle that indicates that something's been collapsed. You can either click on the arrow again or you can click on the rectangle and get it back. You can also go to the edit menu to folding and collapse either a particular section or the entire document. I want to show you a sample function with nested statements here.
This just shows you how the bracketed regions were. I can click on each and collapse it or I can bring it back a bit at a time by clicking back on to these. This is the basic idea of using section headers as a way of making your code a little cleaner and a lot easier for you to navigate through it as you're doing your Data Science work. In making more efficient and more effective.