RStudio - R
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
Login the NMFS Openscapes JupyterHub
Click on the RStudio button when the Launcher appears
Look for the browser tab with the RStudio icon.
Create an RStudio project
- Open RStudio
- In the file panel, click on the Home icon to make sure you are in your home directory
- From the file panel, click “New Project” to create a new project
- In the pop up, select New Directory and then New Project
- Name it
sandbox
- Click on the dropdown in the upper right corner to select your
sandbox
project - 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.
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.
- Click the checkbox next to the file you want to download. One only.
- 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
- Open RStudio
- In the upper right, make sure you are in your
sandbox
project. - From the file panel, click on “New Blank File” and create a new R script.
- 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
- From the file panel, click on “New Blank File” and create a Text File.
- 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
- 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.
- From the upper left, click File -> New File -> Quarto
- Click “Ok” at the bottom.
- When the file opens, click Render (icon at top of file).
- It will ask for a name. Give it one and save.
- You file will render into html.
And many more
Play around with creating other types of documents using templates. Especially if you already use RStudio.