From a1316bd012c83bba3b263077a2986f3ff7954f12 Mon Sep 17 00:00:00 2001 From: John E Date: Tue, 6 Oct 2026 10:15:38 -0400 Subject: [PATCH] docs: fix some typos and trim some whitespace Signed-off-by: John E --- R/extract.R | 2 +- tests/testthat/_snaps/trunc.md | 3 +-- tests/testthat/test-flatten.R | 2 +- tests/testthat/test-interp.R | 4 +-- tests/testthat/test-remove.R | 2 +- tests/testthat/test-trunc.R | 2 +- vignettes/from-base.Rmd | 48 +++++++++++++++++----------------- 7 files changed, 31 insertions(+), 32 deletions(-) diff --git a/R/extract.R b/R/extract.R index 5dc80672..95255607 100644 --- a/R/extract.R +++ b/R/extract.R @@ -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] diff --git a/tests/testthat/_snaps/trunc.md b/tests/testthat/_snaps/trunc.md index a1c4f064..b46497c1 100644 --- a/tests/testthat/_snaps/trunc.md +++ b/tests/testthat/_snaps/trunc.md @@ -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) @@ -10,4 +10,3 @@ Condition Error in `str_trunc()`: ! `width` (3) is shorter than `ellipsis` (4). - diff --git a/tests/testthat/test-flatten.R b/tests/testthat/test-flatten.R index 14ceb4cd..b3c7c935 100644 --- a/tests/testthat/test-flatten.R +++ b/tests/testthat/test-flatten.R @@ -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") diff --git a/tests/testthat/test-interp.R b/tests/testthat/test-interp.R index e1511003..9506e12e 100644 --- a/tests/testthat/test-interp.R +++ b/tests/testthat/test-interp.R @@ -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." @@ -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)) diff --git a/tests/testthat/test-remove.R b/tests/testthat/test-remove.R index f8ea1b7c..214079b9 100644 --- a/tests/testthat/test-remove.R +++ b/tests/testthat/test-remove.R @@ -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") }) diff --git a/tests/testthat/test-trunc.R b/tests/testthat/test-trunc.R index 4b44a408..74b968eb 100644 --- a/tests/testthat/test-trunc.R +++ b/tests/testthat/test-trunc.R @@ -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 = "....") diff --git a/vignettes/from-base.Rmd b/vignettes/from-base.Rmd index a6786ecb..71b1e8c6 100644 --- a/vignettes/from-base.Rmd +++ b/vignettes/from-base.Rmd @@ -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 @@ -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. @@ -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") @@ -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 @@ -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"))) @@ -166,7 +166,7 @@ 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) @@ -174,7 +174,7 @@ 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. @@ -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]+") ``` @@ -300,7 +300,7 @@ There are some subtle differences between base and stringr here. `nchar()` requi #| error: true # base -nchar(factor("abc")) +nchar(factor("abc")) ``` ```{r} @@ -308,7 +308,7 @@ nchar(factor("abc")) 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") @@ -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 @@ -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." @@ -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") @@ -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")