Working with R – RStudio

RStudio is an Integrated Development Environment (IDE) for R it helps you:

  • write code - makes suggestions
  • view the output of your code, including plots
  • find errors
  • manage files
  • View documentation

RStudio logo

[source]

RStudio used to be the name of a company that is now called Posit.

RStudio

First it is important to be familiar with the layout. When you first open RStudio, you will see 3 panes.

When RStudio is first opened you will see 3 panes

Hidden Pane

To save a copy of your code. You must open a file first - this will open a 4th pane. These files include Scripts or what are called R Markdown files.

Hidden Pane

You will see a popup that you can just say “OK” to for now.

Hidden Pane

Nice! now we have a place to save code! This is where we will mostly be working.

Working with R in R Studio - 2 major panes:

  1. The Source/Editor:
    • Static copy of what you did (reproducibility)
    • Top by default
    • saves your code
  2. The R Console:
    • Calculator
    • Try things out interactively, then add to your editor
    • Bottom by default
    • doesn’t save your code

RStudio

Super useful “cheatsheet”: LINK

RStudio

R Markdown files look different from scripts

It will look like this with text in it.

RStudio layout

Scripts will just be empty

Scripts and R Markdown

Although people will use scripts often, and they are good for more programmatic purposes, we generally don’t recommend them for Public Health Researchers.

For data analyses, R Markdown files are generally superior because they allow you to check your code and write more info about your code.

Workspace/Environment

Workspace/Environment

  • Tells you what objects are in R
  • What exists in memory/what is loaded?/what did I read in?

History

  • Shows previous commands. Good to look at for debugging, but don’t rely on it.
    Instead use RMarkdown!
  • Also type the “up” key in the Console to scroll through previous commands

Lower right pane

  • Files - shows the files on your computer of the directory you are working in
  • Help - shows help of R commands
  • Plots - pictures and figures

RStudio Layout

If RStudio doesn’t look the way you want (or like our RStudio), then:

Click on the pane button, which looks like a waffle with 4 indentations. Scroll down to “Pane Layout”.

Pane button.

Default Layout

The Pane Layout menu allows you to modify the layout of RStudio. This shows the default settings with Source on the top left, Console on the bottom left, Environment, History, Connections, and Build on the top right, and Files, Plots, Packages, Help, VCS (version control) on the bottom right.

Let’s take a look at R Studio ourselves!

R Markdown file

R Markdown files (.Rmd) help generate reports that include your code and output.

  1. Helps you describe your code
  2. Allows you to check the output
  3. Can create many different file types

Code chunks

Within R Markdown files are code “chunks”.

This is where you can type R code and run it!

Run code in a chunk

Clicking the run (play) button runs the code in the chunk.

Ctrl + Enter on Windows or Command + Enter on Mac in your script evaluates that line of code

Running a chunk executes the code

  • generally see a preview of the output of the code just below the chunk
  • see the code in the console

If you get annoyed by code previews in Markdown files…

See the Help page of the website. You can adjust this and change your RStudio settings:

Tools > Global Options > R Markdown

Knit file to html

Running all chunks - this will create a report from the R Markdown document! Note that it can’t use anything not included in the file, it can’t use objects in your environment that you were modifying interactively.

knit

Nice report!

This generates a nice report that you can share with others who can open in any browser.

knit

Create Chunks

To create a new R code chunk:

  • Use the insert code chunk button at the top of RStudio.

  • Select R (default) as the language:

Create Chunks

If you like keyboard shortcuts:

  • Windows & Linux use Ctrl+Alt+I
  • Mac use Command+Option+I

I is for insert.

Run previous chunks button

You can run all chunks above a specific chunk using this button:

Errors

R studio can help you find issues in your code. Note that sometimes the error occurs earlier than RStudio thinks.

Recap of where code goes

  • you can test code in the console

knit

  • you can save code in a chunk in the editor (Markdown file)

knit

Getting help from the preview

When you type in a function name, a pop up will preview documentation to help you. It also helps you remember the name of the function if you don’t remember all of it!

preview a function

preview a function

Get help with the help pane

help_pane

Getting Help with ?

If you know the name of a package or function:

Type ?package_name or ?function_name in the console to get information about packages and functions.

For example: ?readr or ?read_csv.

getting help

Double Question Mark

If you haven’t loaded a package yet into R than you may get a response that there is no documentation.

Typing in ??package_name can show you packages that you haven’t loaded yet.

help_pane

Gut Check

Why are R Markdown files so useful?

  1. They let you test your code
  2. They let you view the output of your code
  3. They let you generate cool reports
  4. All of the above

Gut Check

Where does code go typically in an Rmd file?

help_pane

Gut Check

Which button do you click to run the code in a current chunk?

help_pane

Lab: Getting started

To do this lab we need to:

  • Download the file at the link above by clicking on it or go to the website schedule page

  • Find the downloaded file on your computer

  • Open the file in RStudio (double clicking the file name typically works, otherwise right click)

  • Might need to restart RStudio

These videos can help if you aren’t sure where your downloads are:

If you have a PC: https://youtu.be/we6vwB7DsNU

If you have a Mac: https://www.youtube.com/watch?v=Ao9e0cDzMrE

You can find these on the resource page of the class website.

Summary

  • RStudio makes working in R easier
  • the Editor (top) is for static code like scripts or R Markdown documents
  • The console is for testing code (bottom) - best to save your code though!
  • R markdown documents are really helpful for lots of reasons!
  • R code goes within what is called a chunk (the gray box with a green play button)
  • Code chunks can be modified so that they show differently in reports

🏠 Class Website. 💻 Lab. 📃 Posit Cheatsheet. 📃 Day 1 Cheatsheet.

The End

Image by Gerd Altmann from Pixabay

Extra Slides

Chunk settings

Chunk settings

You can specify if a chunk will be seen in the report or not.

Sometimes you want to hide your code

Rainbow Parentheses

Tools –> Global Options –> Code –> Display –> Use rainbow parentheses

This can help you see your code more easily.

Press enter to save this setting and get out of this menu.