RStudio: Basics and Tips

When working in RStudio there are some workflow basics and tips, when done correctly, that will make data science projects easier to do and follow in the long run. In this post, I will be covering how to assign objects, a handy print short cut, how to name variables and calling functions in RStudio. I will also be covering naming code chunks and adding comments when working working with R Markdown files.

Assigning Objects

When you create a new variable or data frame, or whatever object it may be, you always create a new object with “<-”. For example:

What’s handy is that you can easily make another assignment to an object name:

Print Short Cut

You may notice that I had to type “x” in order to print what value x is equal too. A short cut to printing an objects value is to surround the object assignment in parenthesis. See below:

Naming Objects

Along with assigning objects, you will also be naming your objects. Object names must begin with a letter and can only contain letters and numbers. The only special characters that can be used are “_”and “.”.

Assigning unique and descriptive names to all objects is also important and requires using multiple words to name an object. This ensures that both you and your readers can differentiate between all of your objects. When creating such long names, you will need a method to distinguish each word in the name.

For example:


Calling Functions

R comes with built-in functions that you can call with by their function name. For example: function_name(argument1 = variable1, argument2 = variable2)

Let’s try out the mean() function:

There are few things you’ll notice when working with functions in RStudio. First, any time you open a parenthesis, RStudio will automatically close it for you. RStudio will do the same with quotation marks.

In the console, RStudio tries to autofill code so that you do not make mistakes. In the event that you enter code incorrectly, RStudio will show you a “+” on the next line if it thinks that you are missing code. At this point, you can add the rest of code, or hit ESC to start over.

Naming your Code Chunks in R Markdown

Every R Markdown file you work with involves entering your code into a code chunk. These code chunks begin with “{r}” and ends with ““`”as you’ve probably seen through this post.

The chunk header, {r}, is typically used to add arguments that tell RStudio whether or not to include the chunk’s code and results in the R Markdown file. This chunk header can also be used to name the code chunk. While naming a code chunk is not required, providing unique names will make your code easier to follow by telling readers what each code chunk contains. For example:

Adding Comments to Code

While we are on the topic of providing clear code for our readers, nothing makes your code clearer than including comments. Comments can easily be added to code with a “#”. Adding comments to describe what each step of code is makes it easier for readers to understand and follow all of your code. Additionally, these comments are completely ignored by RStudio when running code and generating results. See the example below:

Whether you are an RStudio Pro or just a beginner, I hope these workflow basics and tips were either a refresher or a guide for the next time you are working in RStudio!