-
Notifications
You must be signed in to change notification settings - Fork 1
R and rstudio basic #15
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: main
Are you sure you want to change the base?
Changes from 4 commits
1bc8755
3100f3b
09c6fca
5d9bcbb
a6ab1b7
53f8905
4d92829
d5f518b
4f3e598
2187691
7267170
9c8116b
1d952f5
88bf430
9c93172
64ffef4
5a98376
493826e
ee6ba8e
a9ca887
11a2ff1
9e8bd34
b6c988a
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 |
---|---|---|
|
@@ -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: | ||
|
@@ -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. | ||
|
||
cathy-y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
### Check Your Understanding | ||
|
||
When trying to run a command, you see this error: | ||
|
||
{width=100%} | ||
|
||
|
||
```{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)) | ||
|
||
) | ||
``` | ||
|
||
|
@@ -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)) | ||
) | ||
``` | ||
|
||
cathy-y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
## R Scripts | ||
|
||
|
@@ -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") | ||
), | ||
|
||
|
||
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 | ||
|
||
cathy-y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
@@ -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. | ||
cathy-y marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
```{r Help, echo=FALSE} | ||
### Check Your Understanding | ||
|
||
Here is the help page for the function mean(): | ||
|
||
{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: | ||
|
||
{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)) | ||
) | ||
``` | ||
|
||
cathy-y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
## R packages | ||
|
@@ -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. | ||
{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")) | ||
) | ||
``` |
Uh oh!
There was an error while loading. Please reload this page.