7

I have a very long string inside sprintf(). It's so long that it would be useful (readability) to break it (but only in the source code, not in the output). But whenever I break the long string, it introduces a \n and thus the output has a linebreak as well. How can I break the string in the source code such that it isn't broken in the output?

Marius Hofert
  • 6,546
  • 10
  • 48
  • 102
  • does this work? `sprintf('%s%s%s', 'this is a going to be a very long sentence ', 'much longer than the 80 character limit on my terminal ', 'window.')` – rawr Jan 19 '15 at 05:39
  • Hi, thanks for helping. My problem is not the length of the second argument of `sprintf`, but the first: instead of your '%s%s%s', I have a string far extending 80 chars and that's why I would like to break it. – Marius Hofert Jan 19 '15 at 05:43
  • @MariusHofert - can you show a clipped version of your exact `sprintf` code? – thelatemail Jan 19 '15 at 05:45
  • @rawr... okay, your idea led me in the right direction. I simply used a construction of the form `cat(sprintf("short string", ...), sprintf("short string", ...))` and so I have essentially two shorter `sprintf()` strings/calls. – Marius Hofert Jan 19 '15 at 05:46
  • If the string is too long to be readable in source code, isn't it likely to be too long in the output too? Surely your scripts are being used for their output, not for their source.... – A5C1D2H2I1M1N2O1R2T1 Jan 19 '15 at 05:47
  • @AnandaMahto: It interestingly isn't. The 'format strings' contain a lot of symbols for correctly formating/displaying the numbers, the numbers themselves are very short. I was surprised by that, too. – Marius Hofert Jan 19 '15 at 05:49
  • Break up the first argument and paste them together.... – A5C1D2H2I1M1N2O1R2T1 Jan 19 '15 at 05:57

3 Answers3

11

Perhaps something like the following would be useful (though it's hard to tell without knowing what your input strings actually look like or how you intend to use them).

Fmt <- c(" %s is %d feet tall.\n", 
         "%s likes chocolate.\n",
         "%s eats %d bars of chocolate", 
         "every night after dinner.")

sprintf(paste(Fmt, collapse = " "), "Sven", 7, "Sven", "He", 3)
# [1] " Sven is 7 feet tall.\n Sven likes chocolate.\n He eats 3 bars of chocolate every night after dinner."
cat(.Last.value)
#  Sven is 7 feet tall.
#  Sven likes chocolate.
#  He eats 3 bars of chocolate every night after dinner.
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
2

Use gsub() to perform the relevant replacement, e.g., replace 'space' characters (including new lines) occurring 2 or more times in a row with a single space

> sprintf("string: %s", gsub("[[:space:]]{2,}", " ", "a very
+   long
+   string"))
[1] "string: a very long string"
Martin Morgan
  • 45,935
  • 7
  • 84
  • 112
0

Using the concept given by @Martin but in a slightly different way.

> sprintf(gsub("[[:space:]]{2,}"," ","
+         %s is %d feet tall.
+              %s likes chocolate.
+              %s eats %d bars of chocolate 
+              every night after dinner."), "Sven", 7, "Sven", "He", 3)
[1] " Sven is 7 feet tall. Sven likes chocolate. He eats 3 bars of chocolate every night after dinner."
> 
Adarsha
  • 2,267
  • 22
  • 29