-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_table.R
70 lines (61 loc) · 1.52 KB
/
demo_table.R
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
if(!require(dplyr)) {
install.packages("dplyr", repos="http://cran.us.r-project.org")
require(dplyr)
}
if(!require(rvest)) {
install.packages("rvest", repos="http://cran.us.r-project.org")
require(rvest)
}
if(!require(httr)) {
install.packages("httr", repos="http://cran.us.r-project.org")
require(httr)
}
if(!require(purrr)) {
install.packages("purrr", repos="http://cran.us.r-project.org")
require(purrr)
}
if(!require(jsonlite)) {
install.packages("jsonlite", repos="http://cran.us.r-project.org")
require(jsonlite)
}
html <- "
<html>
<body>
<table>
<tr>
<th>Name</th>
<th>Profession</th>
<th>Age</th>
<th>Country</th>
</tr>
<tr>
<td>Dillon Arroyo</td>
<td>Carpenter</td>
<td>54</td>
<td>UK</td>
</tr>
<tr> <td>Rebecca Douglas</td><td>Developer</td><td>32</td><td>USA</td> </tr>
</table>
</body>
</html>
"
read_html(html) %>%
html_table()
# get all cells
read_html(html) %>%
html_elements('tr') %>%
html_elements('td')
# get all cells of the second row
read_html(html) %>%
html_elements('tr') %>%
.[2] %>%
html_elements('td')
# base r:
html_elements(html_elements(read_html(html), 'tr')[2], 'td')
# reading tables remotely
read_html('https://www.uzh.ch/cmsssl/en/studies/dates/dates.html') %>%
html_table()
# reading one specific table
read_html('https://www.uzh.ch/cmsssl/en/studies/dates/dates.html') %>%
html_table() %>%
.[[2]] # the tidyverse way of selecting an element of a list