R & RStudio Quick Start Guide

Yocoda
9 min readJul 18, 2023

R & RStudio: What is the difference?

First time users of R are often confused as to the difference between R and RStudio. RStudio is an IDE, essentially an added platform to R: it takes the R software and adds to it a very user-friendly graphical interface. Thus, when one uses RStudio, they are still using the full version of R while also getting the benefit of greater functionality and usability due to an improved user interface. As a result, when using R, one should always use RStudio; working with R itself is very cumbersome. Additionally, you aren't limited to only writing in .r files, the editor allows a variety of different languages and file types.

Software summary 👀

  • R and RStudio are not two different versions of the same thing. One can’t be substituted for the other.
  • In fact, they work together. R is a programming language for statistical calculation. And RStudio is an Integrated Development Environment (IDE) that helps you develop programs in R.
  • You can use R without using RStudio, but you can’t use RStudio without using R, so R comes first.

Requirements ☝️

Since RStudio is an add-on to R, you must first download and install R. On your computer, you will see R and RStudio as separate installed programs. When using R for data analysis, you will always open and work in RStudio; you must leave R installed on the computer for RStudio to work, even though you will likely never open the R console itself.

Getting Started with RStudio 🐣

To install R & RStudio on your local computer follow these steps:

(it would be best to read through all the steps before you begin downloading)

Step 1: Download the R installer

  • R for Windows: Click Here
  • Click the link on the top of the page that looks like this⬇️ ⬇️ :
  • R for Mac: Click Here
  • Pick the latest version
  • R for Linux: Click Here

​Install R by opening the installer and following the steps. It’s best to leave all the configuration settings as default and simply press next. Once R is finished, there is no need to launch the terminal unless you want to, you can proceed to Step 2.

Step 2: Download the RStudio Installer

Installing RStudio: Click Here

  • Verify you have already installed R and that you can launch the R application
  • Download the RStudio Desktop installer from THIS LINK
  • Scroll down until you see the download section as the image below. ⬇️⬇️⬇️ Then select the correct download for your OS:
  • ​Install the RStudio IDE by opening the installer and following the steps
  • ⚠️ The instructions for the installer will eventually ask you where R itself has when been installed. Generally it defaults to the correct path on your system for R, though you may have to find where you have installed R and type the path into the RStudio installer manually.⚠️
  • That’s it! You can now run RStudio from your local computer.
  • Once you have everything installed, go ahead and launch the RStudio IDE
  • Which should look like this ⬇️⬇️⬇️

Getting Started 💻

  • R is used both for software development and data analysis. We will operate somewhere between these two tasks. Our goal will be to analyze data 📊 , but we will also perform programming exercises that help illustrate certain concepts.

Our main goal in the labs will follow this flow:

  1. Understand the basic tasks & data
  2. Review the Code Setup and run the sample code
  3. Complete the coding problem
  4. Analyze and explain what the results actually mean.

RStudio IDE 🎰:

  • The RStudio interface is simple. You type R code into the bottom line of the RStudio console pane and then click Enter to run it. The code you type is called a command, because it will command your computer to do something for you. The line you type it into is called the command line.

Console🧮:

  • The large window on the left side is the Console.
  • You can think of this as the “calculator” for RStudio. This is were all of the input, calculations and output are contained. In fact, if you were to run R and not RStudio, this Console is the only window you would see; RStudio adds all of the other interface components that you see.

When you type a command at the prompt and hit Enter, your computer executes the command and shows you the results. Then RStudio displays a fresh prompt for your next command. For example, if you type 1 + 1 and hit Enter, RStudio will display:

> 1 + 1
[1] 2
>

Once you get the hang of the command line, you can easily do anything in R that you would do with a calculator. For example, you could do some basic arithmetic:

2 * 3   
## 6
4 - 1   
## 3
6 / (4 - 1)   
## 2

I’ve left out the >’s and [1]’s. This will make the code easier to copy and paste if you want to put it in your own console.

RMD Files 📄

Included in the Lab assignment page on canvas are a set of .Rmd files which were used to create all of the content contained in them. Usually, R code scripts are saved as .R files. However, you will notice that all of these are saved as .RMD files. These are called R Markdown files.

R Markdown provides an unified authoring framework for data science, combining your code, its results, and your prose commentary. R Markdown documents are fully reproducible and support dozens of output formats, like PDFs, Word files, slideshows, and more.

Create your first R Markdown

  1. To create a new R Markdown file, in R Studio select File>New File>R Markdown
  2. Then, you will see a window pop-up titled “New R Markdown”. Here, you specify the type of file you wish to create… default output is HTML, and this is recommended.
  3. Finally, select Ok to create your new R Markdown file. You will see it appear as a tab in your R Studio session, similar to when a new script is created.

Understanding the R Markdown editor 📐

When creating a new HTML-type R Markdown document, you should see the following window in your R Studio session:

Here is where you edit and create the content of your R Markdown. A R Markdown has three main components: the Prelude, the Chunk, and the Non-chunk (in-line comment), which are labeled in the above image for reference.

Prelude:

In the Prelude, you specify settings and headers for your R Markdown. Some headers you can specify include the title, author, and date. The main setting you may alter is the output setting, which determines which file types are used when creating your document. You can specify multiple file types, or leave it as the single type you chose when creating your Markdown (HTML in the above example).

Chunk:

A chunk is a specially marked part of your Markdown document where you place R code to have run. You will generally have multiple chunks throughout your Markdown which represent specific components of your analysis.

You can quickly insert chunks into your file with

  • The keyboard shortcut Ctrl + Alt + I (OS X: Cmd + Option + I)
  • The Green +C Add Chunk command in the editor toolbar:
  • or by typing the chunk delimiters ```{r} and ```.

When you create your Markdown file and turn it into a document, these chunks are run in order and any output from them is shown in the document. A chunk is marked using {r name} to start, as in the following example:

You can see that the chunk is shaded in gray and adds in a few icons on the upper right of this shaded space.

A chunk has a few components.

  1. First, you must name your chunk, which allows you to easily refer to what the code in the chunk does and to facilitate the organization of your document. In the above example, “name” was used as the chunk’s name. ⚠️ Note that each chunk must have a different name or else you will receive an error when the R Markdown file is compiled. ⚠️
  2. You can also specify a number of options for your chunk inside {r …}, where each option is placed in … and separated by a comma. Note that the chunk’s name is technically an option, so you must separate it from the other options using a comma. Some common options include:
  3. echo=TRUE or FALSE

If TRUE is selected, the actual code in the chunk appears in your document along with the results it produced. If FALSE is selected, the code does not appear and only the output does

  1. warning=TRUE or FALSE

If TRUE is selected (is by default), all outputted warnings from the code are included in the document. If FALSE is selected, these wanrings are not included.

  1. include=TRUE or FALSE

If TRUE is selected (is by default), the output from the code is included in the document. If FALSE is selected, the code is still run but the output is not included in the document. This can be useful if you use certain calculations from this chunk in later chunks and the actual output from this chunk is not of interest.

Non-Chunk (in-line comment)

The white space outside of your Prelude is where you place the non-R code components of your document. This space largely works like a Word document; you can place text, images, tables, etc. inside of it. This combination of R code, its results, and the Word document-like capabilities is what allows you to create a comprehensive report for your analysis within a single file. You can also format the text which appears in this space.

Knit 📋 :

With your R Markdown file open in R studio, you create the corresponding document using the Knit button at the top. This will compile your R Markdown into the document file types that you specified in the Prelude. You will see the progress of the compilation as well as any errors or other messages created during the compilation process in the lower left-hand window of R Studio, in the R Markdown tab. Using Knit will be the best way of understand the capabilities of R Markdown, as well as how to use them.

Here are some important resources when you are ready to jump into Lab 1 📚📑📙

  • “Appendix A” in the book Probability: With Applications and R
  • An Introduction to R
  • Cheat Sheets below ⬇️ (double click to view & download)

✅ When you’re ready

--

--

Yocoda

I am a full-time Student studying Computer Science at SDSU. Writing mostly for myself, sharing some of my projects with you. Feel free to reach out to me!