-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgetting_data.Rmd
224 lines (160 loc) · 4.09 KB
/
getting_data.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
---
title: "Getting Data with R"
author: "Tony Yao-Jen Kuo"
output:
revealjs::revealjs_presentation:
highlight: pygments
reveal_options:
slideNumber: true
previewLinks: true
---
# How to get data with R
## Overview
>- From files
>- From web
# Getting data from files
## Using `read.csv()` for CSV files
- CSV stands for **comma separated values**
```{r}
file_url <- "https://storage.googleapis.com/ds_data_import/chicago_bulls_1995_1996.csv"
df <- read.csv(file_url, stringsAsFactors = FALSE) # personal preference...
dim(df)
```
## Using `read.table()` for general tabular text files
```{r}
file_url <- "https://storage.googleapis.com/ds_data_import/chicago_bulls_1995_1996.txt"
df <- read.table(file_url, header = TRUE, sep = ";")
dim(df)
```
## Using `readxl` package for Excel spreadsheets
```{r eval=FALSE}
install.packages("readxl")
```
```{r}
library(readxl)
file_path <- "/Users/kuoyaojen/Downloads/fav_nba_teams.xlsx"
chicago_bulls <- read_excel(file_path)
head(chicago_bulls)
```
## Importing other sheets
```{r eval=FALSE}
boston_celtics <- read_excel(file_path, sheet = "boston_celtics_2007_2008")
head(boston_celtics)
```
## Reading specific cell ranges
```{r eval=FALSE}
partial_chi <- read_excel(file_path, range = "B8:C13", col_names = FALSE)
knitr::kable(partial_chi)
```
## Using `jsonlite` package for JSON files
```{r eval=FALSE}
install.packages("jsonlite")
```
```{r}
library(jsonlite)
file_url <- "https://storage.googleapis.com/ds_data_import/chicago_bulls_1995_1996.json"
chicago_bulls <- fromJSON(file_url)
class(chicago_bulls)
```
## A quick review
|Source|Format|
|------|------|
|CSV|`data.frame`|
|TXT|`data.frame`|
|Spreadsheet|`data.frame`|
|JSON|`list`|
# Getting data from web
## `jsonlite` for RESTful APIs
```{r}
library(jsonlite)
web_url <- "https://ecshweb.pchome.com.tw/search/v3.3/all/results?q=macbook&page=1&sort=rnk/dc"
macbook <- fromJSON(web_url)
class(macbook)
names(macbook)
```
## `rvest` for HTML documents
```{r eval=FALSE}
install.packages("rvest")
```
## The use of `%>%` operator
- Originated from `magrittr` package
- Now an important operator for the `tidyverse` eco-system
- Can be generated with: Ctrl + Shift + m
## How to call a function
```{r}
library(rvest)
# traditional
sum(1:10)
# using %>%
1:10 %>%
sum()
```
## More examples
```{r}
# traditional
toupper(paste0(strsplit("Jeremy Lin", split = " ")[[1]][2], "sanity"))
# using %>%
"Jeremy Lin" %>%
strsplit(split = " ") %>%
`[[` (1) %>%
`[` (2) %>%
paste0("sanity") %>%
toupper()
```
## `read_html()` for reading all html contents
```{r}
library(rvest)
mi_url <- "https://www.imdb.com/title/tt4912910/"
html_doc <- mi_url %>%
read_html()
```
## `html_nodes()` to locate elements
```{r}
html_doc %>%
html_nodes("strong span") # CSS selector
```
## `html_text()` to remove tags
```{r}
html_doc %>%
html_nodes("strong span") %>%
html_text()
```
## Data of html document are characters
```{r}
html_doc %>%
html_nodes("strong span") %>%
html_text() %>%
as.numeric()
```
## How to locate elements?
>- By CSS Selectors
>- By XPath
# The use of Chrome plugins
## SelectorGadget
A Chrome plugin for CSS selectors: [SelectorGadget](https://chrome.google.com/webstore/detail/selectorgadget/mhjhnkcfbdhnjickkkdbjoemdmbfginb)
## How to use SelectorGadget?
## XPath Helper
A Chrome plugin for XPath: [XPath Helper](https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl)
## How to use XPath Helper?
## Practices: Getting genre information from IMDB.com
```{r}
mi_url <- "https://www.imdb.com/title/tt4912910/"
```
```{r echo=FALSE}
html_doc %>%
html_nodes(".subtext a") %>%
html_text() %>%
`[` (-4)
```
## Practices: Getting cast information from IMDB.com
```{r}
mi_url <- "https://www.imdb.com/title/tt4912910/"
```
```{r echo=FALSE}
html_doc %>%
html_nodes(".primary_photo+ td a") %>%
html_text()
```
## Practices: Getting price ranking from Yahoo! Stock
- Top 100 for TSE: <https://tw.stock.yahoo.com/d/i/rank.php?t=pri&e=tse&n=100>
- Top 100 for OTC: <https://tw.stock.yahoo.com/d/i/rank.php?t=pri&e=otc&n=100>