Skip to contents

This is a non-exported function called in scrub_tabnames() to alter spreadsheet sheet names.

Usage

.forbidden_chars_replace(
  names,
  pattern = "",
  replacement = "",
  pattern_text = pattern,
  repl_text = replacement,
  quiet = FALSE
)

Arguments

names

vector of tab/sheet names

pattern

a regular expression describing forbidden characters

replacement

the text taking the place of the forbidden characters

pattern_text

description of characters being replaced in the pattern regex, default is a repeat of pattern

repl_text

description of the replacements for forbidden chars, default is a repeat of replacement

quiet

TRUE or FALSE to quiet notices of forbidden char replacement

Value

the input vector with forbidden characters replaced/removed

Examples

# for tests:
tab_names <- c(NA, "''''''", "[]hi];", "'HI'", "'hist''", "[:/]", "?/a\\\\",
"'?,*?*'", "'[history coursework]'", "'HISTORIC'AL''''")
xlr:::.forbidden_chars_replace(tab_names, pattern = '[\\\\/:]', replacement = '-',
                               pattern_text = '\\, /, or :')
#> ! Sheet names should not contain:  `\, /, or :`
#> → Replacing forbidden character with:  `-`
#>  [1] NA                       "''''''"                 "[]hi];"                
#>  [4] "'HI'"                   "'hist''"                "[--]"                  
#>  [7] "?-a--"                  "'?,*?*'"                "'[history coursework]'"
#> [10] "'HISTORIC'AL''''"      
xlr:::.forbidden_chars_replace(tab_names, pattern = '[?*]', replacement = '#',
                               pattern_text = '? or *')
#> ! Sheet names should not contain:  `? or *`
#> → Replacing forbidden character with:  `#`
#>  [1] NA                       "''''''"                 "[]hi];"                
#>  [4] "'HI'"                   "'hist''"                "[:/]"                  
#>  [7] "#/a\\\\"                "'#,###'"                "'[history coursework]'"
#> [10] "'HISTORIC'AL''''"      
xlr:::.forbidden_chars_replace(tab_names, pattern = '(?i)(hist)ory',
                               replacement = '\\1', pattern_text = 'history',
                               repl_text = 'hist')
#> ! Sheet names should not contain:  `history`
#> → Replacing forbidden character with:  `hist`
#>  [1] NA                    "''''''"              "[]hi];"             
#>  [4] "'HI'"                "'hist''"             "[:/]"               
#>  [7] "?/a\\\\"             "'?,*?*'"             "'[hist coursework]'"
#> [10] "'HISTORIC'AL''''"   
xlr:::.forbidden_chars_replace(tab_names, pattern = '\\[', replacement = '{',
                               pattern_text = '[brackets]', repl_text = '{curly braces}')
#> ! Sheet names should not contain:  `[brackets]`
#> → Replacing forbidden character with:  `{curly braces}`
#>  [1] NA                       "''''''"                 "{]hi];"                
#>  [4] "'HI'"                   "'hist''"                "{:/]"                  
#>  [7] "?/a\\\\"                "'?,*?*'"                "'{history coursework]'"
#> [10] "'HISTORIC'AL''''"      
xlr:::.forbidden_chars_replace(tab_names, pattern = ']', replacement = '}', quiet = TRUE)
#>  [1] NA                       "''''''"                 "[}hi};"                
#>  [4] "'HI'"                   "'hist''"                "[:/}"                  
#>  [7] "?/a\\\\"                "'?,*?*'"                "'[history coursework}'"
#> [10] "'HISTORIC'AL''''"      
xlr:::.forbidden_chars_replace(tab_names, pattern = "^'+", replacement = '`',
                               pattern_text = "single quotes '' at tabname start or end",
                               repl_text = "the empty string ''")
#> ! Sheet names should not contain:  `single quotes '' at tabname start or end`
#> → Replacing forbidden character with:  `the empty string ''`
#>  [1] NA                       "`"                      "[]hi];"                
#>  [4] "`HI'"                   "`hist''"                "[:/]"                  
#>  [7] "?/a\\\\"                "`?,*?*'"                "`[history coursework]'"
#> [10] "`HISTORIC'AL''''"      
xlr:::.forbidden_chars_replace(tab_names, pattern = "'+$", replacement = '', quiet = TRUE)
#>  [1] NA                      ""                      "[]hi];"               
#>  [4] "'HI"                   "'hist"                 "[:/]"                 
#>  [7] "?/a\\\\"               "'?,*?*"                "'[history coursework]"
#> [10] "'HISTORIC'AL"