R: General case conversion

to_any_case {snakecase} | R Documentation

## General case conversion

### Description

Function to convert strings to any case

### Usage

    to_any_case(string, case = c("snake", "small_camel", "big_camel",
      "screaming_snake", "parsed", "mixed", "lower_upper", "upper_lower", "swap",
      "all_caps", "lower_camel", "upper_camel", "internal_parsing", "none", "flip"),
      abbreviations = NULL, sep_in = "[^[:alnum:]]", parsing_option = 1,
      transliterations = NULL, sep_out = NULL, unique_sep = NULL,
      empty_fill = NULL, prefix = "", postfix = "")

### Arguments

`string` | A string (for example names of a data frame).
`case` | The desired target case, provided as one of the following: snake_case: `"snake"` lowerCamel: `"lower_camel"` or `"small_camel"` UpperCamel: `"upper_camel"` or `"big_camel"` ALL_CAPS: `"all_caps"` or `"screaming_snake"` lowerUPPER: `"lower_upper"` UPPERlower: `"upper_lower"` There are five "special" cases available: `"parsed"`: This case is underlying all other cases. Every substring a string consists of becomes surrounded by an underscore (depending on the `parsing_option`). Underscores at the start and end are trimmed. No lower or upper case pattern from the input string are changed. `"mixed"`: Almost the same as `case = "parsed"`. Every letter which is not at the start or behind an underscore is turned into lowercase. If a substring is set as an abbreviation, it will stay in upper case. `"swap"`: Upper case letters will be turned into lower case and vice versa. Also `case = "flip"` will work. Doesn't work with any of the other arguments except `unique_sep`, `empty_fill`, `prefix` and `postfix`. `"none"`: Neither parsing nor case conversion occur. This case might be helpful, when one wants to call the function for the quick usage of the other parameters. To suppress replacement of spaces to underscores set `sep_out = NULL`. Works with `sep_in`, `transliterations`, `sep_out`, `prefix`, `postfix`, `empty_fill` and `unique_sep`. `"internal_parsing"`: This case is returning the internal parsing (suppressing the internal protection mechanism), which means that alphanumeric characters will be surrounded by underscores. It should only be used in very rare use cases and is mainly implemented to showcase the internal workings of `to_any_case()`
`abbreviations` | character with (uppercase) abbreviations. This marks abbreviations with an underscore behind (in front of the parsing). Useful if `parsing_option` 1 is needed, but some abbreviations within the string need `parsing_option` 2. Use this feature with care: One letter abbreviations and abbreviations next to each other may not be handled correctly, since those cases would introduce ambiguity in parsing.
`sep_in` | (short for separator input) if character, is interpreted as a regular expression (wrapped internally into `stringr::regex()`). The default value is a regular expression that matches any sequence of non-alphanumeric values. All matches will be replaced by underscores (additionally to `"_"` and `" "`, for which this is always true, even if `NULL` is supplied). These underscores are used internally to split the strings into substrings and specify the word boundaries.
`parsing_option` | An integer that will determine the parsing_option. 1: `RRRStudio -> RRR_Studio` 2: `RRRStudio -> RRRS_tudio` 3: This `parsing_option` will suppress the conversion after non-alphanumeric values. See examples. 4: digits directly behind/in front non-digits, will stay as is. any other integer <= 0: no parsing"
`transliterations` | A character vector (if not `NULL`). The entries of this argument need to be elements of `stringi::stri_trans_list()` (like "Latin-ASCII", which is often useful) or names of lookup tables (currently only "german" is supported). In the order of the entries the letters of the input string will be transliterated via `stringi::stri_trans_general()` or replaced via the matches of the lookup table. You should use this feature with care in case of `case = "parsed"`, `case = "internal_parsing"` and `case = "none"`, since for upper case letters, which have transliterations/replacements of length 2, the second letter will be transliterated to lowercase, for example Oe, Ae, Ss, which might not always be what is intended.
`sep_out` | (short for separator output) String that will be used as separator. The defaults are `"_"` and `""`, regarding the specified `case`.
`unique_sep` | A string. If not `NULL`, then duplicated names will get a suffix integer in the order of their appearance. The suffix is separated by the supplied string to this argument.
`empty_fill` | A string. If it is supplied, then each entry that matches "" will be replaced by the supplied string to this argument.
`prefix` | prefix (string).
`postfix` | postfix (string).

### Value

A character vector according the specified parameters above.

### Note

`to_any_case()` is vectorised over `string`, `sep_in`, `sep_out`, `empty_fill`, `prefix` and `postfix`.

### Author(s)

Malte Grosser, malte.grosser@gmail.com

### See Also

snakecase on github or `caseconverter` for some handy shortcuts.

### Examples

    ### Cases
    strings <- c("this Is a Strange_string", "AND THIS ANOTHER_One")
    to_any_case(strings, case = "snake")
    to_any_case(strings, case = "lower_camel") # same as "small_camel"
    to_any_case(strings, case = "upper_camel") # same as "big_camel"
    to_any_case(strings, case = "all_caps") # same as "screaming_snake"
    to_any_case(strings, case = "lower_upper")
    to_any_case(strings, case = "upper_lower")
    to_any_case(strings, case = "parsed")
    to_any_case(strings, case = "mixed")
    to_any_case(strings, case = "internal_parsing")
    to_any_case(strings, case = "none")

    ### Parsing options
    # the default option makes no sense in this setting
    to_any_case("HAMBURGcity", case = "parsed", parsing_option = 1)
    # so the second parsing option is the way to address this example
    to_any_case("HAMBURGcity", case = "parsed", parsing_option = 2)
    # By default (option 1) characters are converted after non alpha numeric characters.
    # This option (3) suppresses this behaviour
    to_any_case("blaBla.bla", case = "big_camel", parsing_option = 3)
    # Numbers are always separated from characters via an underscore.
    # To suppress this behaviour choose parsing_option 4
    snakecase::to_any_case("d3 library", case = "upper_camel", parsing_option = 4, sep_out = " ")
    # there might be reasons to suppress the parsing, while choosing neither one or two
    to_any_case("HAMBURGcity", case = "parsed", parsing_option = 0)

    ### Abbreviations
    to_any_case(c("RSSfeedRSSfeed", "USPassport", "USpassport"), abbreviations = c("RSS", "US"))

    ### Separator input
    string <- "R.St\u00FCdio: v.1.0.143"
    to_any_case(string)
    to_any_case(string, case = "snake", sep_in = ":|\\.")
    to_any_case(string, case = "snake",
                sep_in = ":|(?<!\\d)\\.")

    ### Transliterations
    to_any_case("\u00E4ngstlicher Has\u00EA", transliterations = c("german", "Latin-ASCII"))

    ### sep_out
    strings2 <- c("this - Is_-: a Strange_string", "AND THIS ANOTHER_One")
    to_any_case(strings2, case = "snake", sep_in = "-|\\:", sep_out = " ")
    to_any_case(strings2, case = "big_camel", sep_in = "-|\\:", sep_out = "//")

    ### Empty fill and unique sep
    to_any_case(c("","",""), empty_fill = c("empty", "empty", "also empty"))

    to_any_case(c("same", "same", "same", "other"), unique_sep = c(">"))

    ### Pre -and postfix
    to_any_case(strings2, case = "big_camel", sep_in = "-|\\:", sep_out = "//",
                prefix = "USER://", postfix = ".exe")

[Package snakecase version 0.9.1 Index]