@@ -6,6 +6,22 @@ library(magrittr)
6
6
library(knitr)
7
7
knitr::opts_chunk$set(message = FALSE,
8
8
fig.align = "center")
9
+
10
+ print_html_nodes <- function(html_nodes_object) {
11
+ if(!is_latex_output()) {
12
+ html_nodes_object
13
+ } else {
14
+ output <- capture.output(html_nodes_object)
15
+
16
+ for (i in seq_along(output)) {
17
+ if (nchar(output[i]) <= 79) {
18
+ cat(output[i], sep = "\n")
19
+ } else {
20
+ cat(str_sub(output[i], start = 1, end = 79), sep = "\n")
21
+ }
22
+ }
23
+ }
24
+ }
9
25
```
10
26
11
27
## Overview
@@ -1056,7 +1072,7 @@ having one very long line of code with the string
1056
1072
`" td:nth-child(5),td:nth-child(7),.infobox:nth-child(122) td:nth-child(1),.infobox td:nth-child(3) " `
1057
1073
as the second argument of `html_nodes`:
1058
1074
1059
- ```{r 01-select-nodes}
1075
+ ```{r 01-select-nodes, results = 'hide', echo = TRUE }
1060
1076
selectors <- paste(" td:nth-child(5) " ,
1061
1077
" td:nth-child(7) " ,
1062
1078
" .infobox:nth-child(122) td:nth-child(1) " ,
@@ -1066,6 +1082,10 @@ population_nodes <- html_nodes(page, selectors)
1066
1082
head(population_nodes)
1067
1083
```
1068
1084
1085
+ ```{r echo = FALSE}
1086
+ print_html_nodes(head(population_nodes))
1087
+ ```
1088
+
1069
1089
Next we extract the meaningful data&mdash ; in other words, we get rid of the HTML code syntax and tags&mdash ; from
1070
1090
the nodes using the `html_text`
1071
1091
function. In the case of the example
0 commit comments