-
Notifications
You must be signed in to change notification settings - Fork 10
/
getting_started.Rmd
190 lines (129 loc) · 2.88 KB
/
getting_started.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
---
title: "Getting Started with R"
author: "Tony Yao-Jen Kuo"
output:
revealjs::revealjs_presentation:
highlight: pygments
reveal_options:
slideNumber: true
previewLinks: true
---
```{r include=FALSE}
knitr::opts_chunk$set(results = "hold")
```
# Getting Started
## The characteristics of R
- Comparing to Matlab, SAS, SPSS: Free
- Comparing to Julia: Larger community
- Functional Programming
## R in one sentence
> Applying functions to objects.
## R: core of computation
- Download from <https://cran.r-project.org/>
![](https://i.imgur.com/A5OwsnX.png)
## RStudio: the IDE
- Download from <https://www.rstudio.com/products/rstudio/download>
# Interface of RStudio
## Four blocks
- Script
- Console
- Environment
- Multi-functional
## Console tricks
- Using `Ctrl + L` for clearing
- Using Up and Down arrow checking executed scripts
## Keyboard shortcuts
- Using `Alt + Shift + K` for shortcuts
# Quickstart
## Using `<-` for assignment
- `=` is OKAY, but `<-` is more common
- Using `Alt + -` to get `<-`
## Using `#` for comments
```{r}
# Declaring objects
# Declaring function
```
## Assignments in action
```{r}
# Declaring objects
my_favorite_star <- "Tom Cruise"
my_lucky_number <- 24
r_is_easy <- TRUE
# Declaring function
say_hello <- function(){
return("Hello R!")
}
```
## Printing objects or calling function
```{r results='hold'}
# Printing objects
my_favorite_star
my_lucky_number
r_is_easy
# Calling function
say_hello()
```
## Using `rm()` to remove objects
```{r eval=FALSE}
rm(r_is_easy)
r_is_easy # Error
```
## Why is R Console showing `+`?
- The reason is that R is still expecting inputs from us
```{r eval=FALSE}
my_favorite_player <- "Steve Nash
say_hello <- function(){
return("Hello R!")
help(print
```
## 2 ways to solve it
1. Complete your inputs
2. ESC
## Install packages
- `install.packages()`
- Do it once
## Library packages
- `library()`
- Do it every time
# Useful functions
## Function to query functions or data
```{r eval=FALSE}
help(print) # ?print
help(cars) # ?cars
```
## Function to show relative information
```{r}
sessionInfo()
```
## Function to get locale
```{r}
Sys.getlocale()
```
## Function to get current working directory
```{r}
getwd()
```
## Function to set working directory
- Always use forward slash when declaring path
- Back slash has other functionalities, such as escape, Unicode...
- Try not to use non-English username
```{r eval=FALSE}
setwd("/Users/USERNAME/Desktop") # MacOS desktop
setwd("C:/Users/USERNAME/Desktop") # Windows desktop
```
## Function to quit RStudio
- Not saving workspace image is recommended
```{r eval=FALSE}
q()
```
## Common learning path for programming
- Installation of compiler or interpreter
- Choosing a friendly IDE
- Quickstart
- Variable types
- Functions
- Control flows
- Data structure
- Iterations
- (Optional) Class
- (Optional) Package development