make_or_pattern: Create "or" regex patterns

View source: R/utilities.R

make_or_patternR Documentation

Create "or" regex patterns

Description

This function makes "or" regex patterns from vectors or lists of strings. This function can be used with the matsbyname::select_rows_byname() and matsbyname::select_cols_byname functions. make_or_pattern() correctly escapes special characters in strings, such as ( and ⁠)⁠, as needed. Thus, it is highly recommended that make_or_pattern be used when constructing patterns for row and column selections with matsbyname::select_rows_byname() and matsbyname::select_cols_byname().

Usage

make_or_pattern(
  strings,
  pattern_type = c("exact", "leading", "trailing", "anywhere", "literal")
)

Arguments

strings

A vector of row and column names.

pattern_type

One of "exact", "leading", "trailing", "anywhere", or "literal". Default is "exact".

Details

pattern_type controls the type of pattern created:

  • exact produces a regex pattern that selects row or column names by exact match.

  • leading produces a regex pattern that selects row or column names if the item in strings matches the beginnings of row or column names.

  • trailing produces a regex pattern that selects row or column names if the item in strings matches the ends of row or column names.

  • anywhere produces a regex pattern that selects row or column names if the item in strings matches any substring of row or column names.

  • literal returns strings unmodified, and it is up to the caller to formulate a correct regex.

Value

An "or" regex pattern suitable for selecting row and column names. Amenable for use with matsbyname::select_rows_byname or matsbyname::select_cols_byname.

Examples

make_or_pattern(strings = c("a", "b"), pattern_type = "exact")
make_or_pattern(strings = c("a", "b"), pattern_type = "leading")
make_or_pattern(strings = c("a", "b"), pattern_type = "trailing")
make_or_pattern(strings = c("a", "b"), pattern_type = "anywhere")
make_or_pattern(strings = c("a", "b"), pattern_type = "literal")

RCLabels documentation built on May 29, 2024, 5:56 a.m.