-
Notifications
You must be signed in to change notification settings - Fork 0
Added links, fixed typos, and some suggestions #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: gh-pages
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ a:visited {color: #91170a;} | |
|
|
||
| # [WUR Geoscripting](https://geoscripting-wur.github.io/) <img src="https://www.wur.nl/upload/854757ab-168f-46d7-b415-f8b501eebaa5_WUR_RGB_standard_2021-site.svg" alt="WUR logo" style="height: 35px;"/> | ||
|
|
||
| # Week 1, Tutorial 3: Intro to functions and refresher on R | ||
| # Intro to functions and refresher on R | ||
|
|
||
| ## Learning objectives | ||
| * Learn to write a function | ||
|
|
@@ -28,7 +28,11 @@ a:visited {color: #91170a;} | |
| * Use control flow for efficient function writing | ||
|
|
||
| # Basic *R* knowledge useful for Geo-Scripting | ||
| Scripting with *R* to handle spatial problems requires a core set of basic *R* skills. To prepare you well for the coming weeks, we've summarized what we think are important skills for geoscripting below. | ||
| Scripting with *R* to handle spatial problems requires a core set of basic *R* skills. To prepare you well for the coming weeks, we've summarized what we think are important skills for geoscripting below. | ||
|
|
||
| ```{block type="alert alert-info"} | ||
| **Note** that this tutorial uses the basic programming structure with lists, strings, and more. For a refresher on these elements click [here](https://geoscripting-wur.github.io/RPythonBasics/). | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wanted to put a reference to the R and Python Basics tutorial in case students skipped it. I am not 100% sure how hyperlinks work in Rmd, I tried to look at other scripts for reference, but my links could have errors... On another note, it could be nice to have an additional section on getting started with RStudio. Going over the RStudio structure, the console, ... It is probably that some students have never used any IDE before. |
||
| ``` | ||
|
|
||
| ## Vector handling and vector arithmetic | ||
| **In *R* we call a one-dimensional array of values a *vector*, and a two-dimensional array of values a *matrix*.** | ||
|
|
@@ -98,10 +102,10 @@ When working with real data, such as satellite imagery or KML files, the data al | |
| Key functions for handling character strings are listed below: | ||
|
|
||
| * `list.files()`: to list files in a directory. | ||
| * `glob2rx()`: to select files using a wildcard. Note: if you know regular expressions, you do not need that function. But most people are more comfortable working with wildcards. | ||
| * `glob2rx()`: to select files using a wildcard. Note: if you know regular expressions, you do not need that function. But most people are more comfortable working with wildcards. For a list of wildcards click [here](../Intro2Linux/index.html#file-manipulation). | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added link to tutorial with table of wildcards. Check that the link works. |
||
| * `paste()`, `paste0()`, `sprintf()`: useful to combine vectors e.g. to create a file name. | ||
| * `strsplit()`: to split up a string. | ||
| # `switch()`: to return a value based on a match. | ||
| * `switch()`: to return a value based on a match. | ||
|
|
||
| ### Example of `list.files()` | ||
|
|
||
|
|
@@ -286,7 +290,7 @@ In order to achieve these objectives, you should try to follow a few good practi | |
| - Consistent placement of curly braces. | ||
| * [Make your own packages](#__optional__Writing_packages_). | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This link is not working but I am not sure how to fix it. Goes to the start of the tutorial instead of going to the section on writing packages. |
||
| * Keep a similar [directory structure](../RProjectManagement/index.html) across your projects. | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is unclear which part of tutorial 1 is relevant for keeping a consistent directory structure. Perhaps the link is not necessary. |
||
| * Use [version control](../RProjectManagement/index.html) to develop/maintain your projects and packages. | ||
|
||
| * Use [version control](../RProjectManagement/index.html#version-control) to develop/maintain your projects and packages. | ||
| * Never place `rm(list = ls())` anywhere in your script. If you do so, you may [find your computer set on fire](https://www.tidyverse.org/blog/2017/12/workflow-vs-script/). | ||
| * Use relative file paths and never use `setwd()`. Do press the button below every time you open a script, and assume everyone else will do that too. | ||
|
|
||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
@@ -369,7 +373,7 @@ More flexibility in your function can be achieved through some easy control flow | |
| Control flow refers to the use of conditions in your code that redirect the flow to different directions depending on variables values or class. Make use of that in your code, as this will make your functions more flexible and generic. | ||
|
|
||
| ### Object classes and Control flow | ||
| You have seen in a previous tutorial already that every variable in your R working environment belongs to a class. You can take advantage of that, using control flow, to make your functions more flexible. | ||
| You have seen in a [previous tutorial](../RPythonBasics/index.html/#data-types) already that every variable in your R working environment belongs to a class. You can take advantage of that, using control flow, to make your functions more flexible. | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check if link works. |
||
|
|
||
| A quick reminder on classes: | ||
|
|
||
|
|
@@ -389,6 +393,8 @@ c <- rast(ncol = 10, nrow = 10) | |
| class(c) | ||
| ``` | ||
|
|
||
| Here we used the function `rast` from the Terra package to create an object of class `SpatRaster`. `SpatRaster` is a class for rasters (or matrices) that comprise the necessary elements for spatial data. With Terra and `SpatRaster`, complex spatial data will be processed more efficiently by using specifically tailored functions for `SpatRaster`. This will be studied further in [another tutorial](../IntroToRaster/index.html). | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code above uses the Terra package and the SpatRaster class. I added a short description for context even though this will be covered in later tutorials. However, this paragraph is not necessary if we want to make the tutorial shorter. |
||
|
|
||
| ### Controlling the class of input variables of a function | ||
|
|
||
| One way of making functions more auto-adaptive is by adding checks of the input variables. Using object class can greatly simplify this task. For example let's imagine that you just wrote a simple Hello World function. | ||
|
|
@@ -588,24 +594,24 @@ freq(a, value = -2)$count | |
| # but some elements of the list are impredictibly corrupted, so the list looks as follows | ||
| b <- a | ||
| c <- NA | ||
| list <- list(a, b, c) | ||
|
||
| rasterList <- list(a, b, c) | ||
|
|
||
| # Now, b and a are SpatRasters, and c is ''corrupted'' | ||
| ``` | ||
|
|
||
| ```{r, error=TRUE} | ||
| # Running freq(c) would return an error and stop the whole process | ||
| out <- list() | ||
| for(i in 1:length(list)) { | ||
| out[i] <- freq(list[[i]], value = -2)$count | ||
| for(i in 1:length(rasterList)) { | ||
| out[i] <- freq(rasterList[[i]], value = -2)$count | ||
| } | ||
| ``` | ||
|
|
||
| ```{r} | ||
| # If you wrap the call in a try(), you still get an error, but it's non-fatal | ||
| out <- list() | ||
| for(i in 1:length(list)) { | ||
| out[i] <- try(freq(list[[i]], value = -2)$count) | ||
| for(i in 1:length(rasterList)) { | ||
| out[i] <- try(freq(rasterList[[i]], value = -2)$count) | ||
| } | ||
| out | ||
| ``` | ||
|
|
@@ -624,14 +630,14 @@ fun <- function(x, value) { | |
|
|
||
| # Let's try to run the loop again | ||
| out <- list() | ||
| for(i in 1:length(list)) { | ||
| out[i] <- fun(list[[i]], value = -2) | ||
| for(i in 1:length(rasterList)) { | ||
| out[i] <- fun(rasterList[[i]], value = -2) | ||
| } | ||
| out | ||
|
|
||
| # Note that using a function of the apply family would be a more | ||
| # elegant/shorter way to obtain the same result | ||
| (out <- sapply(X = list, FUN = fun, value = -2)) | ||
| (out <- sapply(X = rasterList, FUN = fun, value = -2)) | ||
| ``` | ||
|
|
||
| ### Function debugging | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the tutorial numbering to make it more flexible. Also, the tutorial could be adapted it a bit since this is no longer a "refresher" on R. I didn't want to change it in case it created problems elsewhere. Suggestion: "Intro to functions and handling spatial data in R".