{String Operations} has some functions to make the life easier of those who deals with strings and strings transformation.

Installing String Operations

Install using {devtools} package

install.packages("devtools") # if devtools is not installed yet
devtools::install_github("SoaresAlisson/sto")

Or install using {pak} package

install.packages("pak", dependencies = TRUE) # if {pak} is not installed yet
pak::pkg_install("SoaresAlisson/sto")

Loading packages

The first thing to do is to load the packages, you can use the function to load many packages as easy as this, with the string to load libraries. So, instead of

To load many packages with {string operations} simply use the load libraries function, or ll(), ex.: sto::ll("package1 package2 package3 ..."):

sto::ll("sto dplyr ggplot2 stringr tidyr")
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
#> [[1]]
#> [1] "sto"       "nvimcom"   "stats"     "graphics"  "grDevices" "utils"    
#> [7] "datasets"  "methods"   "base"     
#> 
#> [[2]]
#>  [1] "dplyr"     "sto"       "nvimcom"   "stats"     "graphics"  "grDevices"
#>  [7] "utils"     "datasets"  "methods"   "base"

Instead of loading the packages, it is also possible to print the vector of library(package) using the option print=TRUE

sto::ll("rvest stringr dplyr", print = TRUE)
#> library(rvest)
#> library(stringr)
#> library(dplyr)

Generate a string using variables

Use f() and the variable inside curly brackets {var}

var1 <- 912 * 2
lorem <- "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

f("Bla bla: {var1}. Ble ble:
  {lorem}")
#> Bla bla: 1824. Ble ble:
#> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Note: The f() is a wrapper around glue::glue() to use it even more easily.

Another facility is to easily create vector from char text

"a b c d" |> s2v()
#> [1] "a" "b" "c" "d"
s2v("Argentina China India Brazil Germany France")
#> [1] "Argentina" "China"     "India"     "Brazil"    "Germany"   "France"
# the function strips the extra white spaces
"a b c     d" |> s2v()
#> [1] "a" "b" "c" "d"
# By default, it also remove commas
"a, b, c, d" |> s2v()
#> [1] "a" "b" "c" "d"
# by default, whitespaces and line breaks are understood as element separator
"a b c\nd\te" |> s2v()
#> [1] "a" "b" "c" "d" "e"
# And specifying another character separator:
"a,b,c,d" |> s2v(sep = ",")
#> [1] "a" "b" "c" "d"
"a|b|c|d e" |> s2v(sep = "\\|")
#> [1] "a"   "b"   "c"   "d e"
# If you need  whitespace in the element of the created vector, use underscore
"bla foo_bar ble bli-blo" |> s2v()
#> [1] "bla"     "foo bar" "ble"     "bli-blo"
# If you need  whitespace in the element of the created vector, but do not want
# to use underscore, so specify the wss parameter
"bla foo_bar ble bli-blo" |> s2v(wss = "-")
#> [1] "bla"     "foo_bar" "ble"     "bli blo"
# To print the output as a string, so you can copy and paste the results in your code
"a b c d" |> s2v(print = TRUE)
#> [1] "c('a', 'b', 'c', 'd')"

grep2, grepl2, gsub2

To easily use the native grep, grepl and gsub functions with the native R pipe, lazy strings have some alternatives, that actually are wrapper around this functions

grep2

c("a", "b", "c", "d") |> grep2("a")
#> [1] "a"
# instead of
grep("a", c("a", "b", "c", "d"), value = T)
#> [1] "a"
# grep 2 is case insensitive by default
c("a", "b", "c", "d") |> grep2("A")
#> [1] "a"
s2v("a b c d") |> grep2("A")
#> [1] "a"
# to disable ignore cases:
c("a", "b", "c", "d", "A") |> grep2("A", ic = F)
#> [1] "A"
# to obtain indexes instead of values:
c("a", "b", "c", "d") |> grep2("a", value = F)
#> [1] 1

grepl2

c("a", "b", "c", "d") |> grepl2("a")
#> [1]  TRUE FALSE FALSE FALSE

gsub2

c("a", "b", "c", "d") |> gsub2("a", "x")
#> [1] "x" "b" "c" "d"
# in the case of character
"a b c d" |> gsub2("a", "x")
#> [1] "x b c d"
# If no second argument is provided, than it will erase the provided pattern:
"'bla bla1 'bla" |> gsub2("'")
#> [1] "bla bla1 bla"
"'bla bla1 'bla" |> gsub2("bla1")
#> [1] "'bla  'bla"

Another function available is nothing(), that does… nothing! It just passes the input, but it is useful when you want to use the native R pipe, so you can comment and uncomment the lines without worrying to change the last pipe.

s2v("a b c d 1 2 3 4") |>
  grep2("[[:alpha:]]") |>
  # grep2("[[:digit:]]")  |>
  nothing()
#> [1] "a" "b" "c" "d"

The items of the pipe can easily be changed:

s2v("a b c d 1 2 3 4") |>
  # grep2("[[:alpha:]]") |>
  grep2("[[:digit:]]") |>
  nothing()
#> [1] "1" "2" "3" "4"