Welcome to DevCon2024! In the Hydrofabric Workshop we will use R and RStudio. Ideally, you will be able to follow along on your own laptop and it is preferable to have the software on your system before the session.
The document will help get R & RStudio on your own computer, along with several other useful tools or packages. If you have problems, you can contact Mike Johnson
Do you already have R installed?
If you have an older versions of R/RStudio installed, we recommend updating. You can check what version of R you are running by typing version in the console of RStudio.
version$version.string
## [1] "R version 4.4.0 (2024-04-24)"
To check your RStudio version in Windows, click the About RStudio menu option in the Help menu. To check on a Mac, click the About RStudio menu option in the RStudio menu.
If you don’t have at least R 4.4.XXX and RStudio 2023.6.0.XXXX. We suggest upgrading.
1. Getting R and RStudio
R is a programming language and free software environment for statistical computing and graphics supported by the R Foundation for Statistical Computing. The R language is widely used for developing statistical software and data analysis. R also provides unparalleled opportunities for analyzing spatial data for spatial modeling.
RStudio is an integrated development environment (IDE) for R. Combined the R language and RStudio IDE provide the open-source, free, toolset for this course.
MacOS Install
Install R
Download the latest release (“R-version.pkg”) of R and save the .pkg file. Double-click it to open, and follow the installation instructions. Note that there are different version in you have an Intel chip (e.g. R-4.1.0.pkg) or an Apple M1 chip (e.g. R-4.1.0-arm64.pkg)
Now that R is installed, you can download and install RStudio.
Install RStudio
- Download RStudio for Mac. After downloading, double-click the file to open it, and make sure it ends up in your applications folder.
Windows Install
Install R
Download the latest R installer (.exe) for Windows. Install the downloaded file as any other windows app.
Now that R is installed, you can download and install RStudio.
Install RStudio
- Download the RStudio version for Windows. Run the installer (.exe file) and follow the instructions.
2. Launch RStudio
Once both R and RStudio are installed, you can find and click on the RStudio icon to open the program. The RStudio icon looks like this:
Opening RStudio will launch a workplace. If something like the below image appears for you, then you are all set!
We will spend the session working in this IDE so the layout and features will become clear as we go. A few things to note now are:
- Your session is linked to a project (pink box).
- In the blue box,
you have:
- An Environment tab that shows all the active objects in your session
- A files tab that shows all the files and folders in your default (project) workplace
- A plots tab that will show all your graphs once we start visualizing data.
- In the red window you will write code and open new/existing scripts and markdown files.
- All output will be printed to your console (yellow box). Code and prompts can be directly entered in the console but will not be saved after executing.
Your layout and theme may not look like the above (all the elements are the same but arranged differently). If you want to change your theme and layout got to RStudio –> Preferences.
Here you can adjust the “Appearance” and “Pane Layout”. If you want your to look like mine, see below:
3. Install NOAA-OWP/hydrofabric
R comes loaded with many base packages and tools. There are also many
additional tools that will make your life easier. Throughout this
session, we’ll use packages that exist in the aggregate
hydrofabric
package.
The hydrofabric
contains a collection of very useful
packages that play nicely together for data exploration, manipulation,
wrangling and visualization (among other things). Packages can be
installed from CRAN (Comprehensive R Archive Network) using the
following pattern which should be typed in the console (yellow box):
install.packages('powerjoin')
install.packages("remotes")
In development package (yay DevCon!) can be installed from Github
using the remotes
package (installed above):
remotes::install_github("NOAA-OWP/hydrofabric")
Once it’s installed, you can ensure that everything worked by
loading/attaching the hydrofabric
libray using the
library
call:
## ── Attaching packages ───────────────────────────────────── hydrofabric 0.1.0 ──
## ✔ dplyr 1.1.4 ✔ hfsubsetR 0.0.9
## ✔ climateR 0.3.5 ✔ ngen.hydrofab 0.0.4
## ✔ nhdplusTools 1.2.0 ✔ sf 1.0.17
## ✔ hydrofab 0.5.2 ✔ terra 1.7.78
## ✔ zonal 0.0.3
## ── Conflicts ──────────────────────────────────────── hydrofabric_conflicts() ──
## ✖ dplyr::group_rows() masks kableExtra::group_rows()
## ✖ terra::plot() masks climateR::plot()
##
## Attaching package: 'hydrofabric'
## The following objects are masked _by_ 'package:hydrofab':
##
## append_style, hf_dm
If you got something similar to the above you’ve successfully installed R, RStudio, and the hydrofabric package!
If you really want to make sure you are ready, ensure you get all
TRUE
below:
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [16] TRUE TRUE
Quick R Notes
## NOTE: What is glue?
x <- "Welcome to DevCon"
y <- "2024"
z <- c('2023', '2024', '2025', '...')
# single string
glue("{x} {y}")
## Welcome to DevCon 2024
# multiple strings from vectors
glue("{x} {z}")
## Welcome to DevCon 2023
## Welcome to DevCon 2024
## Welcome to DevCon 2025
## Welcome to DevCon ...
4. Visit Lynker Spatial
Lynker spatial provides open data and software services aimed at improving user experience with hydrofabric allowing for rapid access, subsetting, and integration with other cloud native resources.
Lynker-spatial Data offerings for hydrofabric can be found here
Lynker-spatial Services are typically provided on Github here.
It’s suggested to install the latest version of
hfsubsetCLI
from the release page for your specific operation system.
5. (Optional) Review Technical Background
If you are interested in the technical tools we use, how they are structured, and their application please review the Background document. While we will talk through this during the session, having a familiarity will help as we move (quickly) through them.
6. (Optional) Install QGIS
QGIS is a free and open-source geographic information system (GIS) software that allows users to visualize, analyze, and manage spatial data. You can install the binaries for your system directly from their website. Or using the following package managers.