library(ggplot2) # package for plotting
library(sf)
library(stars)
library(dplyr)
<- "https://coastwatch.pfeg.noaa.gov/erddap/griddap/ncdcOisst21Agg.nc?sst%5B(2023-08-27T12:00:00Z)%5D%5B(0.0)%5D%5B(-7.875):(44.875)%5D%5B(39.625):(92.375)%5D&.draw=surface&.vars=longitude%7Clatitude%7Csst&.colorBar=%7C%7C%7C%7C%7C&.bgColor=0xffccccff"
url
<- "sst.nc"
fil if(!exists(fil)){
download.file(url=url, destfile=fil)
}
<- raster::raster(fil) %>% st_as_stars()
stars_object ggplot() + geom_stars(data = stars_object)
RStudio - R
Open RStudio in the JupyterHub
- Login the 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. You personal directory is home/rstudio
. Everyone has the same home directory but your files are separate and cannot be seen by others.
Python users: If you open a Python image instead of the R image, your home is home/jovyan
.
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 Rmarkdown document
Now let’s create some more complicated files using the RStudio template feature.
- From the upper left, click File -> New File -> RMarkdown
- Click “Ok” at the bottom.
- When the file opens, click Knit (icon at top of file).
- It will ask for a name. Give it one and save.
- You file will render into html.
Reference sheet for writing in RMarkdown or go to Help > Markdown Quick Reference
A Rmarkdown presentation
- From the upper left, click File -> New File -> RMarkdown
- Click “Presentation” on left of the popup and click “Ok” at the bottom.
- When the file opens, click Knit (icon at top of file).
- It will ask for a name. Give it one and save.
- You file will render into html.
(advanced) An interactive application
- From the upper left, click File -> New File -> Shiny Web App
- In the popup, give the app a name and make sure the app is saved to
my-files
- When the file opens, Run App (icon at top of file).
And many more
Play around with creating other types of documents using templates. Especially if you already use RStudio.
More tips
Learn some tips and tricks from these
- https://colorado.posit.co/rsc/the-unknown/into-the-unknown.html
- https://www.dataquest.io/blog/rstudio-tips-tricks-shortcuts/
Plotting a netCDF file
- https://pjbartlein.github.io/REarthSysSci/netCDF.html
- https://r-spatial.github.io/sf/articles/sf1.html
webpage:
https://coastwatch.pfeg.noaa.gov/erddap/griddap/ncdcOisst21Agg.graph?sst%5B(2023-08-27T12:00:00Z)%5D%5B(0.0)%5D%5B(-7.8):(44.8)%5D%5B(39.7):(92.3)%5D&.draw=surface&.vars=longitude%7Clatitude%7Csst&.colorBar=%7C%7C%7C%7C%7C&.bgColor=0xffccccff
url from the dropdown on that page
url <- https://coastwatch.pfeg.noaa.gov/erddap/griddap/ncdcOisst21Agg.nc?sst%5B(2023-08-27T12:00:00Z)%5D%5B(0.0)%5D%5B(-7.875):(44.875)%5D%5B(39.625):(92.375)%5D&.draw=surface&.vars=longitude%7Clatitude%7Csst&.colorBar=%7C%7C%7C%7C%7C&.bgColor=0xffccccff
- Open an R script
Add this code.