RStudio - R

Note

In this tutorial, we will provide a brief introduction to RStudio using the JupyterHub.

  1. Open RStudio in the JupyterHub
  2. Basic navigation around RStudio: the 4 main panels and menus
  3. The help panel
  4. Create a RStudio project
  5. Installing packages
  6. Uploading and downloading files
  7. Creating files and creating files with templates
  8. Command line (terminal/shell) in RStudio

Open RStudio

You can open RStudio from your computer or in the JupyterHub. We are using RStudio version 2024.04.2 Build 764 on the JupyterHub. Go to Help > About RStudio to check your version. You need to have a relatively updated version (late 2023 at least).

If using the JupyterHub

  1. Login the NMFS Openscapes JupyterHub

  2. Click on the RStudio button when the Launcher appears Jupyterhub Launcher

  3. Look for the browser tab with the RStudio icon.

Basic Navigation

RStudio Panels

Create an RStudio project

  1. Open RStudio
  2. In the file panel, click on the Home icon to make sure you are in your home directory
  3. From the file panel, click “New Project” to create a new project
  4. In the pop up, select New Directory and then New Project
  5. Name it sandbox
  6. Click on the dropdown in the upper right corner to select your sandbox project
  7. Click on Tools > Project Options > General and change the first 2 options about saving and restoring the workspace to “No”

Installing packages

In the bottom right panel, select the Packages tab, click install and then start typing the name of the package. Then click Install.

The JupyterHub comes with many packages already installed so you shouldn’t have to install many packages.

When you want to use a package, you first need to load it with

library(hello)

You will see this in the tutorials. You might also see something like

hello::thefunction()

This is using thefunction() from the hello package.

Note

Python users. In R, you will always call a function like funtion(object) and never like object.function(). The exception is something called ‘piping’ in R, which I have never seen in Python. In this case you pass objects left to right. Like object %>% function(). Piping is very common in modern R but you won’t see it much in R from 10 years ago.

Uploading and downloading files

Note, Upload and download is only for the JupyterHub not on RStudio on your computer.

Uploading is easy.

Look for the Upload button in the Files tab of the bottom right panel.

Download is less intuitive.

  1. Click the checkbox next to the file you want to download. One only.
  2. Click the “cog” icon in the Files tab of the bottom right panel. Then click Export.

Creating files

When you start your server, you will have access to your own virtual drive space. No other users will be able to see or access your files. You can upload files to your virtual drive space and save files here. You can create folders to organize your files. Your personal directory is home/rstudio or home/jovyan. Everyone has the same home directory but your files are separate and cannot be seen by others.

There are a number of different ways to create new files. Let’s practice making new files in RStudio.

R Script

  1. Open RStudio
  2. In the upper right, make sure you are in your sandbox project.
  3. From the file panel, click on “New Blank File” and create a new R script.
  4. Paste
print("Hello World")
1+1

in the script. 7. Click the Source button (upper left of your new script file) to run this code. 8. Try putting your cursor on one line and running that line of code by clicking “Run” 9. Try selecting lines of code and running that by clicking “Run”

csv file

  1. From the file panel, click on “New Blank File” and create a Text File.
  2. The file will open in the top left corner. Paste in the following:
name, place, value
A, 1, 2
B, 10, 20
C, 100, 200
  1. Click the save icon (above your new file) to save your csv file

A Quarto document

Now let’s create some more complicated files using the RStudio template feature.

  1. From the upper left, click File -> New File -> Quarto
  2. Click “Ok” at the bottom.
  3. When the file opens, click Render (icon at top of file).
  4. It will ask for a name. Give it one and save.
  5. You file will render into html.

Quarto Cheatsheet

And many more

Play around with creating other types of documents using templates. Especially if you already use RStudio.