Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/davidfoord1/suitestrings

A demo package of a stringr/stringi-like interface around base R string operations.
https://github.com/davidfoord1/suitestrings

Last synced: about 2 months ago
JSON representation

A demo package of a stringr/stringi-like interface around base R string operations.

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)

library(suitestrings)
```

# suitestrings

[![Repo status: inactive](https://www.repostatus.org/badges/latest/inactive.svg)](https://www.repostatus.org/#inactive) [![R-CMD-check](https://github.com/davidfoord1/suitestrings/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/davidfoord1/suitestrings/actions/workflows/R-CMD-check.yaml) [![codecov](https://codecov.io/gh/davidfoord1/suitestrings/graph/badge.svg?token=F41JP7X4YP)](https://codecov.io.gh.davidfoord1/suitestrings)

This is primarily a personal learning project. It uses wrappers around base R string operations to provide a comprehensive and convenient set of functions for working with strings in R. More specifically, for:

- Combining strings.
- Cleaning and transforming strings.
- Manipulating strings based on regular expression patterns.

The interface is built with a consistent naming scheme and argument structure. The first argument will be the string or strings to work on, which makes it particularly convenient to work with pipes. Functions start with prefixes for easy identification and auto-completion:

- `str_` for vectorised operations on strings.
- `chr_` for vector-wide operations over strings.

You can read more at `vignette("suitestrings-conventions")`.

## Installation

You can install suitestrings from [GitHub](https://github.com/) with:

``` r
# install.packages("devtools")
devtools::install_github("davidfoord1/suitestrings", build_vignettes = TRUE)

# Load the package to your library
library(suitestrings)
```

This package is built with base R, so there are no additional dependencies to install.

## Examples

### Combine strings

```{r}
str_concat(c("Hello", "How are"), c("world!", "you?"), separator = " ")
```

**With R expressions**

`str_glue()` treats text in braces `{}` like R code

```{r}
statement <- "{x} squared is {x^2} and its square root is {sqrt(x)}."

x <- 25
str_glue(statement)

x <- 16
str_glue(statement)
```

### Clean and transform strings

#### Shorten strings

```{r}
# Remove leading/trailing spaces and reduce middle spaces down to one
str_squish(" Too much space ")

# Reduce a string to a specified length
str_truncate("This string is far too long, let's make it shorter", 20)
```

#### Extend strings

```{r}
# Append a specific number of characters
str_indent(c("Hello", "World"), 3)

# Repeat the contents of strings
str_repeat("hello", 3, ", ")
```

#### Reformat strings

```{r}
# Convert strings to different cases
str_to_snake_case(" This /IS/ a ->>!!GREAT!!<<-STriNg!!")
```

### Manipulate strings based on regular expression patterns

```{r}
# Prepare an example character vector.
strings <- c("flat-hat", "backpack", "roll", "cat-sat-on-a-mat")
# Define a pattern for a three letter sequence with "a" in the middle.
pattern <- "\\wa\\w"
```

#### Detect, extract and replace patterns in strings

```{r}
# Does a string contain a match?
str_detect(strings, pattern)
# Get the first match in a string
str_extract_first(strings, pattern)
# Replace the first match in a string
str_replace_first(strings, pattern, "rat")
```

#### Work with specific occurrences of patterns

Using suffixes \_first, \_nth and \_last

```{r}
# Get the second match for each string
str_extract_nth(strings, pattern, 2)
# Remove the last match in each string
str_remove_last(strings, pattern)
```

#### Work with every occurrence of a pattern

With suffix \_all

```{r}
str_locate_all(strings, pattern)

# Get a list of every match for each string
str_extract_all(strings, pattern)
```

#### Work on the character vector as a whole

With prefix chr\_

```{r}
# Get every match from `strings` into one character vector
chr_extract_all(strings, pattern)

# Which elements of the vector contain a match?
chr_which(strings, pattern)

# Get the subset of `strings` that contain a match
chr_subset(strings, pattern)
```