How to run str_wrap on a nonprintable character containing string?

Hi All,

I have a nonprintable character containing string that I want print to console with a column width of 78 characters. I tried stringr's str_wrap() function, but it seems to be counting the nonprintable characters in its calculation of the column width assignments.

Here is an example to illustrate the problem:

mystring <- "\033[30m\033[1mThis is some dummy text\033[22m\033[39m: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
cat(str_wrap(mystring, width=78))

Here is what I want the output to look like:

mystring <- "This is some dummy text: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
cat(str_wrap(mystring, width=78))

What is a tidyverse friendly way to column wrap a nonprintable character containing string?

Thanks in Advance,

Paul Sheridan

Is there any reason you want to keep those characters in the string? If not, have you considered something like this

cat(str_wrap(gsub('[:cntrl:]', '', mystring), width=78))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Actually, I do want to keep those nonprintable characters in the string. In retrospect, I would have done well to write that I want to output the string

my_formatted_string <- "\033[30m\033[1mThis is some dummy text\033[22m\033[39m: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

to the console in such a manner the column spacing comes out to be the same as with this string

my_unformatted_string <- "This is some dummy text: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

when I output it to the console as follows:

cat(str_wrap(my_unformatted_string, width=78))

Moreover, I want to achieve this in a manner that is in keeping with tidyverse best practices.

Apologies for the ambiguity in my original post.

Thanks in Advance!