#' strClean
#'
#' @param x string to be cleaned
#' @param DEBUG T/F verbose output (default = FALSE)
#' @param upper transform to upper case (default = FALSE)
#' @param forbiden.char vector with characters to change for '_' if found anywhere (default = c(" ",".",":","-","__","(",")","[","]","{","}","$","%","&","/","\\","!"))
#' @param forbiden.last.char vector with characters to remove if found at the end of the string (default = c("_"," "))
#'
#' @return String cleaned
#'
#' @examples
#'
#' strClean("VAR_NAM;E") #... will return "VAR_NAM_E"
#'
#' @importFrom crayon bold green red
#' @export
strClean <- function(x,DEBUG=FALSE, upper = FALSE,
forbiden.char = c(" ",".",":",";","-","__","(",")","[","]","{","}","$","%","&","/","\\","!"),
forbiden.last.char = c("_"," ")
){
# if(is.null(x)) stop("string to given")
crayon <- "crayon" %in% installed.packages()
if(!is.character(x)) {
stop("x is not a string")
}
texto <- iconv(iconv(x, from = 'UTF-8', to = 'ASCII//TRANSLIT'), to = 'UTF-8', from = 'ASCII//TRANSLIT')
for(c in forbiden.char){
texto <- gsub(c,"_",texto,fixed = TRUE)
}
if(upper) texto <- toupper(texto)
clean = FALSE
while(!clean){
last_char <- substr(texto, nchar(texto), nchar(texto))
if(DEBUG) {
message("Texto: ",texto)
message("Last character: ",last_char)
}
if(last_char %in% forbiden.last.char) {
texto <- substr(texto,1, nchar(texto)-1)
if(DEBUG) message("... ",ifelse(crayon,crayon::bold("CLEANING"),"CLEANING")," ...")
} else {
if(DEBUG) message("... ",ifelse(crayon,crayon::bold("DONE"),"DONE")," ...")
clean = TRUE
}
}
return(texto)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.