1

Have:

long_string = "Here is a verbose, lengthy, no good and very long string literal that is definitely longer than 80 characters but should not have any newlines in it and it would be great if it was pleasantly formatted."

Want:

long_string = "Here is a verbose, lengthy, no good and very long string literal 
               that is definitely longer than 80 characters but should not have
               any newlines in it and it would be great if it was pleasantly formatted."

The latter, of course, inserts newlines ("\n") and spaces where I try to break and format the line.

To be clear, my question is about formatting the representation of long_string in the source code. I do not want to transform the string that long_string references.

In other language (C for instance), there is a line continuation operator \ -- that would be a perfect solution for my problem (if such an operator existed in R!)

Andrew M
  • 490
  • 5
  • 11
  • Where do you want to use this? You can use `cat(stringr::str_wrap(long_string, 60))` but it does insert new line characters (`\n`). – Ronak Shah Sep 17 '21 at 05:00
  • I want to break long_string approximately every 80 characters, so it doesn't wreck the pagination when viewing the source. New lines **cannot** be inserted in long_string (eg, it is an address to a web resource). – Andrew M Sep 17 '21 at 05:05
  • 1
    Any function that does what you want is going to insert new lines for the purposes of *display*, but not necessarily changing the source object. E.g. what Ronak suggests or the base R alternative - `cat(strwrap(long_string, 80), sep="\n")` – thelatemail Sep 17 '21 at 05:13
  • `cat` could be an (unwieldy) solution -- this would involve catting a sequence of strings to a `textConnection` that points to `long_string`. I don't want print `long_ string`, I want to do further programming with it. My sole goal is to avoid a line that is 300 characters wide in my source. – Andrew M Sep 17 '21 at 05:26
  • 1
    @AndrewM Yeah we understand, read [*@Andrew's answer*](https://stackoverflow.com/a/13384655/6574038)! – jay.sf Sep 17 '21 at 05:28
  • @jay.sf I think that's the ticket, for better or worse. I rather doubt there is anything more succinct. – Andrew M Sep 17 '21 at 05:37
  • @AndrewM You could propose it to [r-devel](https://stat.ethz.ch/mailman/listinfo/r-devel):) – jay.sf Sep 17 '21 at 06:36

0 Answers0