Commit 5a901d5b authored by Jonathan Mang's avatar Jonathan Mang 💡
Browse files

feat: added function `string_replacements`

parent f3ac8271
Pipeline #22831 passed with stages
in 37 minutes and 3 seconds
Package: DIZutils
Title: Utilities for 'DIZ' R Package Development
Version: 0.0.6.9007
Version: 0.0.6.9008
Date: 2021-01-25
Authors@R:
c(person(given = "Jonathan M.",
......
......@@ -16,4 +16,5 @@ export(global_env_hack)
export(query_database)
export(set_env_vars)
export(setdiff_all)
export(string_replacements)
export(xml_2_json)
#' @title Clean string with a given set of replacements
#' @description This function provides the functionality to clean a string
#' with a given set of replacements. This is e.g. useful to create
#' filenames or paths that are not allowed to contain spaces.
#' @param input (string) The character string to be processed.
#' @param replace_mapping (Optional, list, default = "default") The mapping
#' containing what should be replaced with what:
#' `replace_mapping <- list("replace_this" = "with_this")`
#'
#' @return (String) All elements (names) of the input `replace_mapping` or
#' the default mapping are replaced by its values of the mapping.
#'
#' @examples
#' clean_string(input = "Ab 20. April 2020 (((___((N = 1.234)")
#' # Result: "Ab_20_April_2020_N_1234"
#'
#' @export
#'
string_replacements <- function(input, replace_mapping = "default") {
res <- input
replaceme <- list(
# "replace_this" = "with_this",
"(" = "",
")" = "",
"{" = "",
"}" = "",
"." = "",
" " = "_",
"=" = "",
"__" = "_"
)
if (is.list(replace_mapping)) {
## Use the mapping handed over to the function:
replaceme <- replace_mapping
} else if (replace_mapping == "default") {
replaceme <- replaceme
} else {
DIZutils::feedback(
print_this = paste0(
"There is something handed over as `replace_mapping`",
" which is not a list. Using default replacement instead."
),
type = "Warning",
findme = "7c9d35cd88"
)
}
iteration <- 0
finished_changing <- FALSE
while (iteration < 1000 && !finished_changing) {
finished_changing <- TRUE
for (replace_this in names(replaceme)) {
res <-
gsub(
pattern = replace_this,
replacement = replaceme[[replace_this]],
x = res,
fixed = TRUE
)
}
for (replace_this in names(replaceme)) {
## Check if there is still something left to change:
if (grepl(pattern = replace_this,
x = res,
fixed = TRUE)) {
## There is a character left which needs to be changed,
## so this loop will be computed again.
finished_changing <- FALSE
}
}
iteration <- iteration + 1
}
return(res)
}
......@@ -37,7 +37,7 @@ my_desc$set_authors(c(
my_desc$del("Maintainer")
# Set the version
my_desc$set_version("0.0.6.9007")
my_desc$set_version("0.0.6.9008")
# The title of your package
my_desc$set(Title = "Utilities for 'DIZ' R Package Development")
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/string_replacements.R
\name{string_replacements}
\alias{string_replacements}
\title{Clean string with a given set of replacements}
\usage{
string_replacements(input, replace_mapping = "default")
}
\arguments{
\item{input}{(string) The character string to be processed.}
\item{replace_mapping}{(Optional, list, default = "default") The mapping
containing what should be replaced with what:
`replace_mapping <- list("replace_this" = "with_this")`}
}
\value{
(String) All elements (names) of the input `replace_mapping` or
the default mapping are replaced by its values of the mapping.
}
\description{
This function provides the functionality to clean a string
with a given set of replacements. This is e.g. useful to create
filenames or paths that are not allowed to contain spaces.
}
\examples{
clean_string(input = "Ab 20. April 2020 (((___((N = 1.234)")
# Result: "Ab_20_April_2020_N_1234"
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment