Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1bc8755
Move file from master to individual branch
stephan-koenig May 30, 2020
3100f3b
Fix text formatting and add donwload link for data
stephan-koenig Sep 25, 2020
09c6fca
Apply suggestions from TA code review
stephan-koenig Oct 6, 2020
5d9bcbb
Apply suggestions from code review
cathy-y Oct 17, 2020
a6ab1b7
Apply suggestions from code review
cathy-y Oct 17, 2020
53f8905
Fixed markups for code chunks
cathy-y Oct 23, 2020
4d92829
Restructured order of sections, moved learning objectives to the begi…
cathy-y Oct 24, 2020
d5f518b
Update r_and_rstudio_basic.html
cathy-y Oct 31, 2020
4f3e598
Rewrote questions (and uploaded images to go with them)
cathy-y Oct 31, 2020
2187691
Question revisions
cathy-y Oct 31, 2020
7267170
Removed the working with data section
cathy-y Oct 31, 2020
9c8116b
Changed capitalization
cathy-y Nov 7, 2020
1d952f5
Added section on data types, amended questions
cathy-y Nov 7, 2020
88bf430
Update inst/tutorials/r_and_rstudio_basic/r_and_rstudio_basic.Rmd
cathy-y Nov 9, 2020
9c93172
Added section on vectors and data frames, covered logical operators i…
cathy-y Nov 15, 2020
64ffef4
Started the troubleshooting section
cathy-y Dec 1, 2020
5a98376
Completed getting help section
cathy-y Dec 1, 2020
493826e
Edited the troubleshooting section
cathy-y Dec 6, 2020
ee6ba8e
Added internal links
cathy-y Dec 12, 2020
a9ca887
Updated internal links
cathy-y Dec 12, 2020
11a2ff1
Merge branch 'main' into r-and-rstudio-basic
cathy-y Feb 3, 2021
9e8bd34
Merge branch 'main' into r-and-rstudio-basic
stephan-koenig Feb 3, 2021
b6c988a
Merge branch 'main' into r-and-rstudio-basic
stephan-koenig Mar 17, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added inst/resources/images/mean.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/resources/images/meanNA.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/resources/images/objecterror.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/resources/images/selecterror.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
161 changes: 70 additions & 91 deletions inst/tutorials/r_and_rstudio_basic/r_and_rstudio_basic.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ R Packages:
- Understand what R packages are and how they are used
- Install and load packages

Working with Data:

- Load data into R
- Save loaded data in the environment

## A Tour of RStudio

When you start RStudio, you will see something like the following window appear:
Expand All @@ -85,12 +80,23 @@ Notice that the window has three "panes":
To change the look of RStudio, you can go to Tools → Global Options → Appearance and select colours, font size, etc. If you plan on working for longer periods of time, we suggest choosing a dark background colour which is less hard on your computer battery and your eyes.
You can also change the sizes of the panes by dragging the dividers or clicking on the expand and compress icons at the top right corner of each pane.

### Check Your Understanding

When trying to run a command, you see this error:

![](/images/objecterror.png){width=100%}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This image does not show

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So where in the repo did you place your image?


```{r Tour, echo=FALSE}
quiz(
question("Which pane enables you to manage R packages?",
question("Where is the command typed?",
answer("The console", correct=TRUE),
answer("Lower right pane"),
answer("Upper right pane")),

question("Where can you go to try to locate 'object'?",
answer("The console"),
answer("Lower right pane", correct=TRUE),
answer("Upper right pane"))
answer("Lower right pane"),
answer("Upper right pane", correct=TRUE))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a neat idea. I like this style of questioning--introducing students to the types of common errors they might encounter is prudent. Well done.

)
```

Expand All @@ -116,14 +122,6 @@ You can open this project in the future in one of three ways:
- Switch among projects by clicking on the R project symbol in the upper left
corner of RStudio

```{r Projects, echo=FALSE}
quiz(
question("What is not a benefit of using RStudio projects?",
answer("All of your files and outputs are linked to the project directory"),
answer("R automatically looks for files in the project directory so you don't have to specify a full file path"),
answer("When you reopen a project, your code is saved so all you need to do is rerun it", correct=TRUE))
)
```

## R Scripts

Expand All @@ -137,20 +135,6 @@ Let's create an R script (File > New File > R Script) and save it as `tidyverse.

We can copy and paste the previous commands in this tutorial and aggregate it in our R script.

```{r Scripts, echo=FALSE}
quiz(
question("How do R scripts make your work reproducible?",
answer("Trick question: they work just like Excel and don't make work reproducible"),
answer("They keep a record of all actions done to get from raw data to final results", correct=TRUE),
answer("You can use them to test different operations on your data")
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth introducing the set.seed() function for reproducible random simulations? Might be out of the scope of this tutorial but it's also a pretty straightforward function. What do you think @cathy-y and @stephan-koenig?


question("How do you annotate R scripts with comments?",
answer("You start the line with 'comment:'"),
answer("You start the line with the # symbol", correct=TRUE),
answer("You can just start typing and R will know it's a comment automatically"))
)
```

## Variables in R

Expand Down Expand Up @@ -215,6 +199,26 @@ product <- 3 * 5
difference <- product - 7
```

### Check Your Understanding

Without running the code below, what is the final value of x?

```{r solve-x, exercise=TRUE, exercise.eval=FALSE}
x <- 5
y <- 2
x <- y * x
y <- x - 4
```

```{r solve-x-q, echo=FALSE}
quiz(
question("What is the final value of x?",
answer("5"),
answer("10", correct=TRUE),
answer("6"))
)
```

## Functions in R

### Overview
Expand Down Expand Up @@ -245,13 +249,6 @@ On the other hand, the function `paste()`, which links together words, does acce
paste("Hello", "world", sep = " ")
```

```{r Functions, echo=FALSE}
quiz(
question("True or False: Functions accept inputs of all types",
answer("True"),
answer("False", correct=TRUE))
)
```

### Getting Help

Expand Down Expand Up @@ -313,19 +310,33 @@ The **Method** section describes the implementation method of the function `sele

The **Examples** Section has examples of the function that can be directly copy and pasted into your terminal and ran.

```{r Help, echo=FALSE}
### Check Your Understanding

Here is the help page for the function mean():

![](/images/mean.png){width=100%}

```{r Functions1, echo=FALSE}
quiz(
question("What types of arguments can be passed to mean()?",
answer("Logical (True/False)", correct=TRUE),
answer("Numeric", correct=TRUE),
answer("Text"),
answer("Numeric vectors", correct=TRUE))
)
```

When trying to find the mean of x, NA is the output:

![](/images/meanNA.png){width=100%}

```{r Functions2, echo=FALSE}
quiz(
question("How would you launch a help section for a given function",
answer("Place an exclamation mark in front of the function"),
answer("Place an question mark in front of the function", correct = TRUE),
answer("Place an question mark after of the function"),
answer("Type out help and function name in the console")
),

question("What does an = sign indicate in the help section",
answer("There exists a default and is not mandatory", correct = TRUE),
answer("Is a mandatory input in the function")
))
question("Why did this happen?",
answer("The last value of x needs to be removed using the 'trim' argument"),
answer("Since x is not composed of only numbers and logicals, an error is thrown"),
answer("The NA in x need to be removed using the na.rm argument", correct=TRUE))
)
```

## R packages
Expand Down Expand Up @@ -364,49 +375,17 @@ You can then use the function `BiocManager::install()` to install a Bioconductor
`BiocManager::install("annotate")`
Sometimes two packages include functions with the same name. A common example is that a `select()` function is included both in the `dplyr` and `MASS` packages. Therefore, to specify the use of a function from a particular package, you can precede the function with a the following notation: `package::function()`.

```{r Packages, echo=FALSE}
quiz(
question("True or False: Packages are installed once, but loaded every time",
answer("True", correct=TRUE),
answer("False")
))
```

## Working with data

### Data description

The data used throughout this module were collected as part of an on-going oceanographic time-series program in Saanich Inlet, a seasonally anoxic fjord on the East coast of Vancouver Island, British Columbia.

The data that you will use in R are 16S rRNA amplicon profiles of microbial communities at several depths in Saanich Inlet from one time-point in this series (August 2012). These ~300 bp sequences were processed using [mothur](https://www.mothur.org/wiki/Main_Page) to yield 97% (approximately species-level) operational taxonomic units (OTUs).

`Saanich_OTU_metadata.csv` is a comma-delimited table of counts of four OTUs in each sample, normalized to 100,000 sequences per sample and the corresponding conditions of each sample (Depth, NO2, NO3 etc.).

For a brief introduction to these data, see Hallam SJ et al. 2017. Monitoring microbial responses to ocean deoxygenation in a model oxygen minimum zone. Sci Data 4: 170158 [doi:10.1038/sdata.2017.158](https://www.nature.com/articles/sdata2017158).
### Check Your Understanding

### Loading tabular data
After installing the `dplyr` package, you encounter this error:

Tabular data can be loaded into R using the tidyverse `read_*` functions, which generate data frames. Each row in a data frame represents one observation, and each column represents one variable.
![](/images/selecterror.png){width=100%}

in your file browser, create a `data` directory in your project directory. Download the [`Saanich_OTU_metadata.csv`](https://github.com/EDUCE-UBC/educer/blob/master/data-raw/Saanich_OTU_metadata.csv) file and save it in your `data` directory.

For example, we can load our Saanich data into R with `read_csv` for a comma-separated file and specify the arguments that describe our data as follows:

- `file`: the name of the file you want to load
- `col_names`: can take either the value `TRUE` or `FALSE` and tells R if the first row contains column names

```{r, eval = FALSE}
read_csv(file="data/Saanich_OTU_metadata.csv", col_names = TRUE)
```

```{r, echo = FALSE}
OTU_metadata_table <- combined
```

### Save data in the environment

Since we want to do more with our data after reading it in, we need to save it as a variable in R as we did previously with the `<-` operator. You can choose to name the object whatever you like, though this module assumes the names used below.

```{r eval = FALSE}
OTU_metadata_table <- read_csv(file="data/Saanich_OTU_metadata.csv", col_names = TRUE)
```{r Packages, echo=FALSE}
quiz(
question("Why did this happen?",
answer("The function `select()` wasn't installed"),
answer("The package `dplyr` wasn't loaded after installation", correct=TRUE),
answer("The arguments given to `select()` are invalid"))
)
```
Loading