Wrapping strings, but not substring in quotation marks, using R

This question is related to my question about Roxygen.

I want to write a new function that performs word wrap for strings, similar to strwrapor stringr::str_wrap, but with the following twist: Any elements (substrings) in a string enclosed in quotation marks should not be allowed to wrap.

So, for example, using the following data examples

test <- "function(x=123456789, y=\"This is a long string argument\")"
cat(test)
function(x=123456789, y="This is a long string argument")

strwrap(test, width=40)
[1] "function(x=123456789, y=\"This is a long"
[2] "string argument\")"      

I want the desired result to newWrapFunction(x, width=40, ...)be as follows:

desired <- c("function(x=123456789, ", "y=\"This is a long string argument\")")
desired
[1] "function(x=123456789, "               
[2] "y=\"This is a long string argument\")"

identical(desired, newWrapFunction(tsring, width=40))
[1] TRUE

Can you come up with a way to do this?


PS. If you can help me solve this problem, I suggest this code as a patch for roxygen2. I have determined where this patch should apply and your input will be recognized.

+5
1

, strwrap, : A) "" , "~ | ~" : strwrapqt

 ....  
 zz <- strsplit(x, "\'") # will be only working on even numbered sections
   for (i in seq_along(zz) ){ 
       for (evens in seq(2, length(zz[[i]]), by=2)) {
            zz[[i]][evens] <- gsub("[ ]", "~|~", zz[[i]][evens])}
                       }
 zz <- unlist(zz) 
  .... insert just before
 z <- lapply(strsplit) ...........

"~ | ~" . , "", .

....
 y <- gsub("~\\|~", " ", y)
....

: @joran. , , - , , zz <- strsplit(x, "\'|\"") .

+2

All Articles