Skip to content

Commit 698ad82

Browse files
luisDVAhadley
andauthored
Replace magritttr pipes with base pipes (#418)
Fixes #415 --------- Co-authored-by: Hadley Wickham <h.wickham@gmail.com>
1 parent e345f5f commit 698ad82

32 files changed

Lines changed: 234 additions & 222 deletions

R/encoding.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
#' # A file with bad encoding included in the package
1212
#' path <- system.file("html-ex", "bad-encoding.html", package = "rvest")
1313
#' x <- read_html(path)
14-
#' x %>% html_elements("p") %>% html_text()
14+
#' x |> html_elements("p") |> html_text()
1515
#'
1616
#' html_encoding_guess(x)
1717
#' # Two valid encodings, only one of which is correct
18-
#' read_html(path, encoding = "ISO-8859-1") %>% html_elements("p") %>% html_text()
19-
#' read_html(path, encoding = "ISO-8859-2") %>% html_elements("p") %>% html_text()
18+
#' read_html(path, encoding = "ISO-8859-1") |> html_elements("p") |> html_text()
19+
#' read_html(path, encoding = "ISO-8859-2") |> html_elements("p") |> html_text()
2020
html_encoding_guess <- function(x) {
2121
check_installed("stringi")
2222

R/form.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
#' html <- read_html("http://www.google.com")
2323
#' search <- html_form(html)[[1]]
2424
#'
25-
#' search <- search %>% html_form_set(q = "My little pony", hl = "fr")
25+
#' search <- search |> html_form_set(q = "My little pony", hl = "fr")
2626
#'
2727
#' # Or if you have a list of values, use !!!
2828
#' vals <- list(q = "web scraping", hl = "en")
29-
#' search <- search %>% html_form_set(!!!vals)
29+
#' search <- search |> html_form_set(!!!vals)
3030
#'
3131
#' # To submit and get result:
3232
#' \dontrun{

R/html.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
#' url <- "https://rvest.tidyverse.org/articles/starwars.html"
99
#' html <- read_html(url)
1010
#'
11-
#' html %>%
12-
#' html_element("div") %>%
13-
#' html_children() %>%
11+
#' html |>
12+
#' html_element("div") |>
13+
#' html_children() |>
1414
#' html_name()
1515
#' @export
1616
#' @importFrom xml2 xml_name
@@ -35,11 +35,11 @@ html_name <- function(x) {
3535
#' <li><a href="https://c.com">b</a></li>
3636
#' </ul>')
3737
#'
38-
#' html %>% html_elements("a") %>% html_attrs()
38+
#' html |> html_elements("a") |> html_attrs()
3939
#'
40-
#' html %>% html_elements("a") %>% html_attr("href")
41-
#' html %>% html_elements("li") %>% html_attr("class")
42-
#' html %>% html_elements("li") %>% html_attr("class", default = "inactive")
40+
#' html |> html_elements("a") |> html_attr("href")
41+
#' html |> html_elements("li") |> html_attr("class")
42+
#' html |> html_elements("li") |> html_attr("class", default = "inactive")
4343
#' @export
4444
#' @importFrom xml2 xml_attr
4545
html_attr <- function(x, name, default = NA_character_) {

R/live.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ read_html_live <- function(url) {
7272
#' sess <- read_html_live("https://www.bodybuilding.com/exercises/finder")
7373
#' sess$view()
7474
#'
75-
#' sess %>% html_elements(".ExResult-row") %>% length()
75+
#' sess |> html_elements(".ExResult-row") |> length()
7676
#' sess$click(".ExLoadMore-btn")
77-
#' sess %>% html_elements(".ExResult-row") %>% length()
77+
#' sess |> html_elements(".ExResult-row") |> length()
7878
#' sess$click(".ExLoadMore-btn")
79-
#' sess %>% html_elements(".ExResult-row") %>% length()
79+
#' sess |> html_elements(".ExResult-row") |> length()
8080
#' }
8181
LiveHTML <- R6::R6Class(
8282
"LiveHTML",

R/rvest-package.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@
3030
#' # Then find elements that match a css selector or XPath expression
3131
#' # using html_elements(). In this example, each <section> corresponds
3232
#' # to a different film
33-
#' films <- starwars %>% html_elements("section")
33+
#' films <- starwars |> html_elements("section")
3434
#' films
3535
#'
3636
#' # Then use html_element() to extract one element per film. Here
3737
#' # we the title is given by the text inside <h2>
38-
#' title <- films %>%
39-
#' html_element("h2") %>%
38+
#' title <- films |>
39+
#' html_element("h2") |>
4040
#' html_text2()
4141
#' title
4242
#'
4343
#' # Or use html_attr() to get data out of attributes. html_attr() always
4444
#' # returns a string so we convert it to an integer using a readr function
45-
#' episode <- films %>%
46-
#' html_element("h2") %>%
47-
#' html_attr("data-id") %>%
45+
#' episode <- films |>
46+
#' html_element("h2") |>
47+
#' html_attr("data-id") |>
4848
#' readr::parse_integer()
4949
#' episode
5050
xml2::read_html

R/selectors.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
#' <p class='important'>This is an important paragraph</p>
4141
#' ")
4242
#'
43-
#' html %>% html_element("h1")
44-
#' html %>% html_elements("p")
45-
#' html %>% html_elements(".important")
46-
#' html %>% html_elements("#first")
43+
#' html |> html_element("h1")
44+
#' html |> html_elements("p")
45+
#' html |> html_elements(".important")
46+
#' html |> html_elements("#first")
4747
#'
4848
#' # html_element() vs html_elements() --------------------------------------
4949
#' html <- minimal_html("
@@ -54,18 +54,18 @@
5454
#' <li><b>R4-P17</b> is a <i>droid</i></li>
5555
#' </ul>
5656
#' ")
57-
#' li <- html %>% html_elements("li")
57+
#' li <- html |> html_elements("li")
5858
#'
5959
#' # When applied to a node set, html_elements() returns all matching elements
6060
#' # beneath any of the inputs, flattening results into a new node set.
61-
#' li %>% html_elements("i")
61+
#' li |> html_elements("i")
6262
#'
6363
#' # When applied to a node set, html_element() always returns a vector the
6464
#' # same length as the input, using a "missing" element where needed.
65-
#' li %>% html_element("i")
65+
#' li |> html_element("i")
6666
#' # and html_text() and html_attr() will return NA
67-
#' li %>% html_element("i") %>% html_text2()
68-
#' li %>% html_element("span") %>% html_attr("class")
67+
#' li |> html_element("i") |> html_text2()
68+
#' li |> html_element("span") |> html_attr("class")
6969
html_element <- function(x, css, xpath) {
7070
UseMethod("html_element")
7171
}

R/session.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@
2222
#' @export
2323
#' @examples
2424
#' s <- session("http://hadley.nz")
25-
#' s %>%
26-
#' session_jump_to("hadley.jpg") %>%
27-
#' session_jump_to("/") %>%
25+
#' s |>
26+
#' session_jump_to("hadley.jpg") |>
27+
#' session_jump_to("/") |>
2828
#' session_history()
2929
#'
30-
#' s %>%
31-
#' session_jump_to("hadley.jpg") %>%
32-
#' session_back() %>%
30+
#' s |>
31+
#' session_jump_to("hadley.jpg") |>
32+
#' session_back() |>
3333
#' session_history()
3434
#'
3535
#' \donttest{
36-
#' s %>%
37-
#' session_follow_link(css = "p a") %>%
36+
#' s |>
37+
#' session_follow_link(css = "p a") |>
3838
#' html_elements("p")
3939
#' }
4040
session <- function(url, ...) {

R/table.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
#' <tr><td>4</td><td>y</td></tr>
3131
#' <tr><td>10</td><td>z</td></tr>
3232
#' </table>")
33-
#' sample1 %>%
34-
#' html_element("table") %>%
33+
#' sample1 |>
34+
#' html_element("table") |>
3535
#' html_table()
3636
#'
3737
#' # Values in merged cells will be duplicated
@@ -41,8 +41,8 @@
4141
#' <tr><td colspan='2'>4</td><td>5</td></tr>
4242
#' <tr><td>6</td><td colspan='2'>7</td></tr>
4343
#' </table>")
44-
#' sample2 %>%
45-
#' html_element("table") %>%
44+
#' sample2 |>
45+
#' html_element("table") |>
4646
#' html_table()
4747
#'
4848
#' # If a row is missing cells, they'll be filled with NAs
@@ -52,8 +52,8 @@
5252
#' <tr><td colspan='2'>3</td></tr>
5353
#' <tr><td>4</td></tr>
5454
#' </table>")
55-
#' sample3 %>%
56-
#' html_element("table") %>%
55+
#' sample3 |>
56+
#' html_element("table") |>
5757
#' html_table()
5858
html_table <- function(x,
5959
header = NA,

R/text.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@
2727
#'
2828
#' # html_text() returns the raw underlying text, which includes whitespace
2929
#' # that would be ignored by a browser, and ignores the <br>
30-
#' html %>% html_element("p") %>% html_text() %>% writeLines()
30+
#' html |> html_element("p") |> html_text() |> writeLines()
3131
#'
3232
#' # html_text2() simulates what a browser would display. Non-significant
3333
#' # whitespace is collapsed, and <br> is turned into a line break
34-
#' html %>% html_element("p") %>% html_text2() %>% writeLines()
34+
#' html |> html_element("p") |> html_text2() |> writeLines()
3535
#'
3636
#' # By default, html_text2() also converts non-breaking spaces to regular
3737
#' # spaces:
3838
#' html <- minimal_html("<p>x&nbsp;y</p>")
39-
#' x1 <- html %>% html_element("p") %>% html_text()
40-
#' x2 <- html %>% html_element("p") %>% html_text2()
39+
#' x1 <- html |> html_element("p") |> html_text()
40+
#' x2 <- html |> html_element("p") |> html_text2()
4141
#'
4242
#' # When printed, non-breaking spaces look exactly like regular spaces
4343
#' x1

README.Rmd

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,21 @@ starwars <- read_html("https://rvest.tidyverse.org/articles/starwars.html")
5050
# Then find elements that match a css selector or XPath expression
5151
# using html_elements(). In this example, each <section> corresponds
5252
# to a different film
53-
films <- starwars %>% html_elements("section")
53+
films <- starwars |> html_elements("section")
5454
films
5555
5656
# Then use html_element() to extract one element per film. Here
5757
# we the title is given by the text inside <h2>
58-
title <- films %>%
59-
html_element("h2") %>%
58+
title <- films |>
59+
html_element("h2") |>
6060
html_text2()
6161
title
6262
6363
# Or use html_attr() to get data out of attributes. html_attr() always
6464
# returns a string so we convert it to an integer using a readr function
65-
episode <- films %>%
66-
html_element("h2") %>%
67-
html_attr("data-id") %>%
65+
episode <- films |>
66+
html_element("h2") |>
67+
html_attr("data-id") |>
6868
readr::parse_integer()
6969
episode
7070
```
@@ -74,7 +74,7 @@ If the page contains tabular data you can convert it directly to a data frame wi
7474
```{r}
7575
html <- read_html("https://en.wikipedia.org/w/index.php?title=The_Lego_Movie&oldid=998422565")
7676
77-
html %>%
78-
html_element(".tracklist") %>%
77+
html |>
78+
html_element(".tracklist") |>
7979
html_table()
8080
```

0 commit comments

Comments
 (0)