stringr::str_wrap()

Sam Callis

In this document, I will introduce the str_wrap() function and show what it’s for.

#load tidyverse up
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.3
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.0.6     v dplyr   1.0.4
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.0.3
## Warning: package 'tibble' was built under R version 4.0.3
## Warning: package 'tidyr' was built under R version 4.0.3
## Warning: package 'readr' was built under R version 4.0.3
## Warning: package 'purrr' was built under R version 4.0.3
## Warning: package 'dplyr' was built under R version 4.0.3
## Warning: package 'stringr' was built under R version 4.0.3
## Warning: package 'forcats' was built under R version 4.0.3
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
#example dataset
string1 <- "This is a string"
string2 <- "Most programming languages have a data type called a string, which is used for data values that are made up of ordered sequences of characters, such as 'hello world'. A string can contain any sequence of characters, visible or invisible, and characters may be repeated."

What is it for?

The str_wrap() function gives us a way to wrap strings into paragraphs. The formatting of the paragraphs can be adjusted by width and can include an indent or exdent. str_wrap() is typically combined with the cat() (concatenate) function.

#without cat() function
str_wrap(string2, width = 80)
## [1] "Most programming languages have a data type called a string, which is used for\ndata values that are made up of ordered sequences of characters, such as 'hello\nworld'. A string can contain any sequence of characters, visible or invisible,\nand characters may be repeated."
#with cat() function
cat(str_wrap(string2, width = 80), "\n")
## Most programming languages have a data type called a string, which is used for
## data values that are made up of ordered sequences of characters, such as 'hello
## world'. A string can contain any sequence of characters, visible or invisible,
## and characters may be repeated.
cat(str_wrap(string2, width = 40, indent = 2),"\n")
##   Most programming languages have a
## data type called a string, which is
## used for data values that are made up
## of ordered sequences of characters, such
## as 'hello world'. A string can contain
## any sequence of characters, visible
## or invisible, and characters may be
## repeated.
cat(str_wrap(string1, width = 1, exdent = 2), "\n")
## This
##   is
##   a
##   string

Is it helpful?

I have not personally worked with many datasets that includes strings, but I would imagine str_wrap() would be very helpful for managing them. Especially if the strings are long.