-
Notifications
You must be signed in to change notification settings - Fork 1
/
ngsub.R
31 lines (28 loc) · 1.06 KB
/
ngsub.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#' Remove Extra Spaces and Newline Characters
#'
#' This function removes extra spaces and newline characters from the given text.
#' It replaces sequences of multiple spaces with a single space and removes newline characters followed by a space.
#'
#' @title ngsub
#' @description Remove extra spaces and newline characters from text.
#' @param text The input text from which extra spaces and newline characters need to be removed.
#' @return Returns the modified text as a character string.
#' @export ngsub
#' @author Satoshi Kume
#' @examples
#' \dontrun{
#' ngsub("This is a text \n with extra spaces.")
#' }
ngsub <- function(text){
# Assertions for input validation
assertthat::assert_that(
assertthat::is.string(text),
assertthat::noNA(text)
)
# Remove newline characters followed by a space and replace multiple spaces with a single space
results <- gsub("\n ", " ", gsub(" ", " ", text))
results <- sub("^ ", "", results)
results <- sub(" $", "", results)
# Return the modified text as a character string
return(as.character(results))
}