Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#' str_extract_all(shopping_list, "\\d", simplify = TRUE)
#'
#' # Extract all words
#' str_extract_all("This is, suprisingly, a sentence.", boundary("word"))
#' str_extract_all("This is, surprisingly, a sentence.", boundary("word"))
str_extract <- function(string, pattern, group = NULL) {
if (!is.null(group)) {
out <- str_match(string, pattern)[, group + 1]
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/_snaps/trunc.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# does not truncate to a length shorter than elipsis
# does not truncate to a length shorter than ellipsis

Code
str_trunc("foobar", 2)
Expand All @@ -10,4 +10,3 @@
Condition
Error in `str_trunc()`:
! `width` (3) is shorter than `ellipsis` (4).

2 changes: 1 addition & 1 deletion tests/testthat/test-flatten.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test_that("can remove missing values", {
expect_equal(str_flatten(c("a", NA), na.rm = TRUE), "a")
})

test_that("str_flatten_oxford removes comma iif necessary", {
test_that("str_flatten_oxford removes comma if necessary", {
expect_equal(str_flatten_comma(letters[1:2], ", or "), "a or b")

expect_equal(str_flatten_comma(letters[1:3], ", or "), "a, b, or c")
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-interp.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test_that("str_interp works with nested expressions", {
)
})

test_that("str_interp works in the absense of placeholders", {
test_that("str_interp works in the absence of placeholders", {
expect_equal(
str_interp("A quite static string here."),
"A quite static string here."
Expand All @@ -66,7 +66,7 @@ test_that("str_interp wraps parsing errors", {
expect_snapshot(str_interp("This is a ${1 +}"), error = TRUE)
})

test_that("str_interp formats list independetly of other placeholders", {
test_that("str_interp formats list independently of other placeholders", {
a_list <- c("item1", "item2", "item3")
other <- "1"
extract <- function(text) regmatches(text, regexpr("xx[^x]+xx", text))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-remove.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_that("succesfully wraps str_replace_all", {
test_that("successfully wraps str_replace_all", {
expect_equal(str_remove_all("abababa", "ba"), "a")
expect_equal(str_remove("abababa", "ba"), "ababa")
})
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-trunc.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test_that("truncations work for all sides", {
expect_equal(trunc("center", 5), "T...g")
})

test_that("does not truncate to a length shorter than elipsis", {
test_that("does not truncate to a length shorter than ellipsis", {
expect_snapshot(error = TRUE, {
str_trunc("foobar", 2)
str_trunc("foobar", 3, ellipsis = "....")
Expand Down
48 changes: 24 additions & 24 deletions vignettes/from-base.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ library(stringr)
library(magrittr)
```

This vignette compares stringr functions to their base R equivalents to help users transitioning from using base R to stringr.
This vignette compares stringr functions to their base R equivalents to help users transitioning from using base R to stringr.

# Overall differences

Expand Down Expand Up @@ -73,22 +73,22 @@ Overall the main differences between base R and stringr are:

1. stringr functions start with `str_` prefix; base R string functions have no
consistent naming scheme.
1. The order of inputs is usually different between base R and stringr.

1. The order of inputs is usually different between base R and stringr.
In base R, the `pattern` to match usually comes first; in stringr, the
`string` to manupulate always comes first. This makes stringr easier
`string` to manipulate always comes first. This makes stringr easier
to use in pipes, and with `lapply()` or `purrr::map()`.
1. Functions in stringr tend to do less, where many of the string processing

1. Functions in stringr tend to do less, where many of the string processing
functions in base R have multiple purposes.

1. The output and input of stringr functions has been carefully designed.
For example, the output of `str_locate()` can be fed directly into
For example, the output of `str_locate()` can be fed directly into
`str_sub()`; the same is not true of `regexpr()` and `substr()`.

1. Base functions use arguments (like `perl`, `fixed`, and `ignore.case`)
to control how the pattern is interpreted. To avoid dependence between
arguments, stringr instead uses helper functions (like `fixed()`,
arguments, stringr instead uses helper functions (like `fixed()`,
`regex()`, and `coll()`).

Next we'll walk through each of the functions, noting the similarities and important differences. These examples are adapted from the stringr documentation and here they are contrasted with the analogous base R operations.
Expand All @@ -97,7 +97,7 @@ Next we'll walk through each of the functions, noting the similarities and impor

## `str_detect()`: Detect the presence or absence of a pattern in a string

Suppose you want to know whether each word in a vector of fruit names contains an "a".
Suppose you want to know whether each word in a vector of fruit names contains an "a".

```{r}
fruit <- c("apple", "banana", "pear", "pineapple")
Expand All @@ -109,11 +109,11 @@ grepl(pattern = "a", x = fruit)
str_detect(fruit, pattern = "a")
```

In base you would use `grepl()` (see the "l" and think logical) while in stringr you use `str_detect()` (see the verb "detect" and think of a yes/no action).
In base you would use `grepl()` (see the "l" and think logical) while in stringr you use `str_detect()` (see the verb "detect" and think of a yes/no action).

## `str_which()`: Find positions matching a pattern

Now you want to identify the positions of the words in a vector of fruit names that contain an "a".
Now you want to identify the positions of the words in a vector of fruit names that contain an "a".

```{r}
# base
Expand All @@ -123,14 +123,14 @@ grep(pattern = "a", x = fruit)
str_which(fruit, pattern = "a")
```

In base you would use `grep()` while in stringr you use `str_which()` (by analogy to `which()`).
In base you would use `grep()` while in stringr you use `str_which()` (by analogy to `which()`).

## `str_count()`: Count the number of matches in a string

How many "a"s are in each fruit?

```{r}
# base
# base
loc <- gregexpr(pattern = "a", text = fruit, fixed = TRUE)
sapply(loc, function(x) length(attr(x, "match.length")))

Expand Down Expand Up @@ -166,15 +166,15 @@ hw <- "Hadley Wickham"

# base
substr(hw, start = 1, stop = 6)
substring(hw, first = 1)
substring(hw, first = 1)

# stringr
str_sub(hw, start = 1, end = 6)
str_sub(hw, start = 1)
str_sub(hw, end = 6)
```

In base you could use `substr()` or `substring()`. The former requires both a start and stop of the substring while the latter assumes the stop will be the end of the string. The stringr version, `str_sub()` has the same functionality, but also gives a default start value (the beginning of the string). Both the base and stringr functions have the same order of expected inputs.
In base you could use `substr()` or `substring()`. The former requires both a start and stop of the substring while the latter assumes the stop will be the end of the string. The stringr version, `str_sub()` has the same functionality, but also gives a default start value (the beginning of the string). Both the base and stringr functions have the same order of expected inputs.

In stringr you can use negative numbers to index from the right-hand side string: -1 is the last letter, -2 is the second to last, and so on.

Expand Down Expand Up @@ -256,7 +256,7 @@ matches <- gregexpr(pattern = "[a-z]+", text = shopping_list) # words
regmatches(shopping_list, m = matches)

# stringr
str_extract(shopping_list, pattern = "\\d+")
str_extract(shopping_list, pattern = "\\d+")
str_extract_all(shopping_list, "[a-z]+")
```

Expand Down Expand Up @@ -300,15 +300,15 @@ There are some subtle differences between base and stringr here. `nchar()` requi
#| error: true

# base
nchar(factor("abc"))
nchar(factor("abc"))
```

```{r}
# stringr
str_length(factor("abc"))
```

Note that "characters" is a poorly defined concept, and technically both `nchar()` and `str_length()` returns the number of code points. This is usually the same as what you'd consider to be a charcter, but not always:
Note that "characters" is a poorly defined concept, and technically both `nchar()` and `str_length()` returns the number of code points. This is usually the same as what you'd consider to be a character, but not always:

```{r}
x <- c("\u00fc", "u\u0308")
Expand All @@ -320,7 +320,7 @@ str_length(x)

## `str_pad()`: Pad a string

To pad a string to a certain width, use stringr's `str_pad()`. In base R you could use `sprintf()`, but unlike `str_pad()`, `sprintf()` has many other functionalities.
To pad a string to a certain width, use stringr's `str_pad()`. In base R you could use `sprintf()`, but unlike `str_pad()`, `sprintf()` has many other functionalities.

```{r}
# base
Expand Down Expand Up @@ -375,7 +375,7 @@ str_squish("\n\nString with excess, trailing and leading white space\n\n")

## `str_wrap()`: Wrap strings into nicely formatted paragraphs

`strwrap()` and `str_wrap()` use different algorithms. `str_wrap()` uses the famous [Knuth-Plass algorithm](http://litherum.blogspot.com/2015/07/knuth-plass-line-breaking-algorithm.html).
`strwrap()` and `str_wrap()` use different algorithms. `str_wrap()` uses the famous [Knuth-Plass algorithm](http://litherum.blogspot.com/2015/07/knuth-plass-line-breaking-algorithm.html).

```{r}
gettysburg <- "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal."
Expand All @@ -393,7 +393,7 @@ Note that `strwrap()` returns a character vector with one element for each line;

## `str_replace()`: Replace matched patterns in a string

To replace certain patterns within a string, stringr provides the functions `str_replace()` and `str_replace_all()`. The base R equivalents are `sub()` and `gsub()`. Note the difference in default input order again.
To replace certain patterns within a string, stringr provides the functions `str_replace()` and `str_replace_all()`. The base R equivalents are `sub()` and `gsub()`. Note the difference in default input order again.

```{r}
fruits <- c("apple", "banana", "pear", "pineapple")
Expand Down Expand Up @@ -502,7 +502,7 @@ anniversary <- as.Date("1991-10-12")

# base
sprintf(
"My name is %s my age next year is %s and my anniversary is %s.",
"My name is %s my age next year is %s and my anniversary is %s.",
name,
age + 1,
format(anniversary, "%A, %B %d, %Y")
Expand Down
Loading