-
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 5 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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,389 @@ | ||||||
--- | ||||||
title: "Introduction to R and RStudio fundamentals" | ||||||
author: "Michelle Kang (adapted from Dr. Kim Dill-McFarland)" | ||||||
date: "version `r format(Sys.time(), '%B %d, %Y')`" | ||||||
output: | ||||||
learnr::tutorial: | ||||||
progressive: true | ||||||
allow_skip: true | ||||||
runtime: shiny_prerendered | ||||||
description: Welcome to R! If you want to analyze and visualize data reproducibly, you've come to the right place. This tutorial covers the basics of R and RStudio. RStudio is a free program used for coding in R. After learning about its features and functionality, we will dive into R language basics, where you will create functions and load packages. | ||||||
--- | ||||||
|
||||||
```{r setup, include = FALSE} | ||||||
# General learnr setup | ||||||
library(learnr) | ||||||
knitr::opts_chunk$set(echo = TRUE) | ||||||
library(educer) | ||||||
# Helper function to set path to images to "/images" etc. | ||||||
setup_resources() | ||||||
|
||||||
# Tutorial specific setup | ||||||
library(dplyr) | ||||||
library(readr) | ||||||
total <- 4 | ||||||
``` | ||||||
|
||||||
## Learning objectives | ||||||
|
||||||
By the end of this tutorial you should be able to: | ||||||
|
||||||
- Identify the different components of RStudio. | ||||||
- Declare variables in R. | ||||||
- Recognize and use functions. | ||||||
- Install and load R packages. | ||||||
- Load and subset tabular data using tidyverse. | ||||||
- Use the `help` function in R console to troubleshoot and identify required arguments for a given function | ||||||
|
||||||
|
||||||
## A Tour of RStudio | ||||||
By the end of this section, you will be able to: | ||||||
- Name the three panes in RStudio and what they do | ||||||
- Change the sizes of the panes | ||||||
- Navigate through the console using common keyboard shortcuts | ||||||
- Change the appearance of RStudio | ||||||
When you start RStudio, you will see something like the following window appear: | ||||||
|
||||||
{width=100%} | ||||||
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 image shows up |
||||||
|
||||||
Notice that the window has three "panes": | ||||||
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. There are four panes and an option in, tools > global options > pane layout to customize the panels. Would it be useful to include here? |
||||||
|
||||||
- Console (lower left side): this is your view of the R engine. You can type in R commands here and see the output printed by R. (To tell them apart, your input is in blue, and the output is black.) There are several editing conveniences available: up and down arrow keys to go back to previously entered commands which you then can edit and re-run, TAB for completing the name before the cursor, and so on. See more in [online docs](http://www.rstudio.com/ide/docs/using/keyboard_shortcuts). | ||||||
cathy-y marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
- Environment/History (tabbed in the upper right): view current user-defined objects and previously-entered commands, respectively. | ||||||
|
||||||
- Files/Help/Plots/Packages (tabbed in the lower right): as their names suggest, you can view the contents of the current directory, the built-in help pages, and the graphics you created, as well as manage R packages. | ||||||
|
||||||
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. | ||||||
```{r quiz: R Tour, echo=FALSE} | ||||||
|
||||||
question("Which pane enables you to manage R packages?", | ||||||
answer("The console"), | ||||||
answer("Lower right pane", correct=TRUE), | ||||||
answer("Upper right pane") | ||||||
) | ||||||
|
||||||
## RStudio Projects | ||||||
By the end of this section, you will be able to: | ||||||
- List the benefits of using RStudio Projects | ||||||
- Create a new RStudio Project | ||||||
- Open or switch to an existing RStudio Project | ||||||
When you create a project, RStudio creates an `.Rproj` file that links all of your files and outputs to the project directory. When you import data from a file, R automatically looks for it in the project directory instead of you having to specify a full file path on your computer (like `/Users/<username>/Desktop/`). R also automatically saves any output to the project directory. Finally, projects allow you to save your R environment in `.RData` so that when you close RStudio and then re-open it, you can start right where you left off without re-importing any data or re-calculating any intermediate steps. | ||||||
|
||||||
RStudio has a simple interface to create and switch between projects, accessed from the button in the top-right corner of the RStudio window. (Labeled "Project: (None)", initially.) | ||||||
|
||||||
Let's create a project to work in for this tutorial. Start by clicking the "Project" button in the upper right or going to the "File" menu. Select "New Project", and the following will appear: | ||||||
|
||||||
{width=75%} | ||||||
|
||||||
|
||||||
Choose "New Directory" followed by "New Project" and click on "Browse...". Navigate to your Desktop, and name the directory `<course_name> R`(replace `<course_name>` with the name of your class, e.g. `MICB301`) for this project. | ||||||
|
||||||
After your project is created, navigate to its directory using your Finder/File explorer or the integrated Terminal in RStudio. You will see the ".RProj" file has been created. | ||||||
|
||||||
You can open this project in the future in one of three ways: | ||||||
|
||||||
- In your file browser (e.g. Finder or Explorer), simply double-click on the `.RProj` file | ||||||
- In an open RStudio window, choose "File" → "Open Project" | ||||||
- Switch among projects by clicking on the R project symbol in the upper left | ||||||
corner of RStudio | ||||||
|
||||||
```{r quiz: R Projects, echo=FALSE} | ||||||
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) | ||||||
) | ||||||
|
||||||
|
||||||
## Variables in R | ||||||
By the end of this section, you will be able to: | ||||||
- Declare variables | ||||||
- Perform operations to change the value of variables | ||||||
We use variables to store data that we want to access or manipulate later. Variables must have unique names. | ||||||
|
||||||
Without declaring a variable the sum of these two numbers will be printed to console but cannot be accessed for future use: | ||||||
|
||||||
```{r novar, exercise=TRUE} | ||||||
2 + 2 | ||||||
``` | ||||||
|
||||||
To declare a variable, follow the pattern of: `variable <- value`. Let's declare a variable `total` as the sum of two numbers. | ||||||
|
||||||
```{r d_var, exercise=TRUE} | ||||||
total <- 2 + 2 | ||||||
``` | ||||||
|
||||||
We access the value of `total`: | ||||||
|
||||||
```{r var, exercise=TRUE} | ||||||
total | ||||||
``` | ||||||
|
||||||
We can use the value stored in `total`: | ||||||
|
||||||
```{r sub_var, exercise=TRUE} | ||||||
total - 1 | ||||||
``` | ||||||
|
||||||
After declaring a variable, we can perform operations to change the value stored in the variable: | ||||||
|
||||||
```{r sub_var2, exercise=TRUE} | ||||||
total <- total - 1 | ||||||
|
||||||
|
||||||
total | ||||||
``` | ||||||
Now it's your turn! Declare a variable "product" and set its value to 3 * 5. Next, operating on "product", declare a variable called "difference", whose final value is 8. | ||||||
|
Now it's your turn! Declare a variable "product" and set its value to 3 * 5. Next, operating on "product", declare a variable called "difference", whose final value is 8. | |
Now it's your turn! Declare a variable "product" and set its value to the product of the numbers 3 and 5. Next, using the variable "product", declare a variable called "difference", whose final value is 8. |
cathy-y marked this conversation as resolved.
Show resolved
Hide resolved
cathy-y marked this conversation as resolved.
Show resolved
Hide resolved
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.
Nice job! I might also give a few use cases for functions. Python has those neat recursive functions (not sure if those exist in R), but you could also talk about taking some raw data doing some long processing all in one shot, if you have the processing function already written. This could open the door to the whole API style of programming, though that might be beyond the scope of this tutorial.
Outdated
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.
Does this need to be in a "child document?"
I couldn't figure out how to do this on my tutorial though...
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.
Yes, exactly. Look at the Slack#educe, Gil talks about how he set up the child document.
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.
Do I just replace all of the text here with his code? Not super sure what exactly the user is supposed to see here.
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.
Nice, much better description.