Getting Started with aWhere’s R Scripts
aWhere Training Tutorial
This tutorial aims to get you started leveraging R and RStudio to dig deeper into the historical and/or forecast weather trends for the locations you identified in the previous tutorial Generating Locations File in QGIS. Don’t worry if you have never used R – this is a beginner-friendly tutorial, no R experience needed!
Why should we use R?
R helps us programmatically create informative charts and maps, we can use Excel and GIS software as a starting point, but a programming language is useful to analyze specific regions and timescales efficiently and repeatedly. aWhere provides R code (scripts) for users to customize their data analysis and inform meaningful narratives.
At this point in the workflow you should feel comfortable:
▶ Creating the suggested folder structure
▶ Accessing resources on aWhere’s adaptER Platform
▶ Creating your credentials file with your API Key and Secret
▶ Accessing aWhere’s R Scripts using Github
▶ Using QGIS to make a map of aWhere’s GIS-ready weather files
▶ Generating locations file using QGIS
Before moving forward, please make sure your training folder includes the folders and files shown here. The tutorials above can show you where to find these materials if you are missing any. This setup is critical because we will now show you how to load these files into R to create useful charts and figures.
Using R to Generate Insights
Installing R and Studio
R and RStudio are both free, open-source software, available for all commonly used operating systems, including Windows, macOS, and Linux systems. R and RStudio install in the standard manner on each of these systems. If you have not yet installed R and RStudio – please do so now by following the instructions provided on this useful tutorial here.
Tip: Regardless of your operating system, you should install R before installing RStudio!
1. Once you have your software installed, please open RStudio and load the script 0-installation_and_setup.R by going to File > Open File > navigate to your training folder and select this script (see screenshots below) then click Open
2. Your RStudio screen should look like this:
A quick tour of the screen:
- Editor is where you can write new code and edit existing code
- Console is the place where R is waiting for you to tell it what to do, and where it will show the results of a command. You can type commands directly into the console, but they will be forgotten when you close the session.
- Environment and the Environment tab of this panel shows you the names of all the data objects (like vectors, matrices, and dataframes) that you’ve defined in your current R session. You can also see information like the number of observations and rows in data objects.
- Files and Plots where you can navigate to locally saved files, see charts and figures as they are created and more via these tabs:
- In the Files tab, you can navigate to and load existing scripts on your local computer.
- In the Plots tab, any charts that you create will appear.
- In the Packages tab, you can view which R packages are loaded. More in packages later.
- In the Help tab, you can view documentation for R code to learn how to use it.
- In the Viewer tab, you can view content generated for the web.
For this lesson, we will focus on the Editor and the Console sections.
If you have never used R before, here are a few necessary tips and resources to get you started.
▶ When using R you have to execute or “Run” each line in the code. You can do this by clicking the button in the top right corner of the code section:
Tip: You can also use a keyboard shortcut to Run the lines:
▷ command+Enter on a Mac
▷ CTRL+Enter on a Windows
▶ The lines of code that are green and have a green # sign in front indicate that those are not “live” lines but rather comments to guide you through the steps of running the code. Think of these scripts as an instruction manual – each line or section will have comments explaining the subsequent lines and their purpose.
▶ The lines that you will actually run are in black/blue text!
3. Let’s start running some lines of code. This script is very short and there are only a few lines you actually need to run. After reading through the first section of comments, you should arrive at line 36 run your first line. Put your cursor at line 36 and click the Run button.
Once you run this line you will see the progress in the Console section.
The Console is dynamic and will tell you how the script is progressing. You will be alerted of any problems with an error message in this section. If you do get an error message, you can either email our team (firstname.lastname@example.org) or search for the answer on R forums such as Stack Overflow – a community forum for developers. Copy your error directly into Stack Overflow and there will most likely be someone else who has had the same issue! R has a very large community of users!
Tip: You may also notice a red stop sign that appears in the top right corner of the Console pane, not to worry this means that the script is working running!
4. Once you have clicked “Run” for each line of code and didn’t see any error messages in the Console – you are ready for script #2! Your packages are installed and you can continue.
5. Still feel like you want to learn more about R? Check out these resources:
a. R for Beginners
b. Introduction to RStudio
c. A Pirate’s Guide to R
d. Check out online courses from places like DataCamp and Coursera to become even more comfortable with R.
Review the next tutorial in the series to start creating powerful data visualizations with the Tutorial: Generating Outputs with R.
If you have any questions, please contact email@example.com