47

I'm looking for how to do printf in r, ie I want to type:

printf("hello %d\n", 56 )

and get the same output as typing:

print(sprintf("hello %d\n", 56 )

I've read the following links:

... so I'm aware I could use cat("hello", 56) , and that's probably what I will do, but just wondering if there is some shortcut way of writing print(sprintf(...))?

Sorry if this question is a duplicate (I dont know). It's awfully hard to search for 'printf r', since it returns results for php, c, ...

Community
  • 1
  • 1
Hugh Perkins
  • 7,975
  • 7
  • 63
  • 71
  • 15
    Search by adding tags in square brackets: `[r] printf`, not just `r printf`. – Dirk Eddelbuettel Oct 23 '12 at 03:54
  • You could also try to show what you have tried, what you expected and where it failed -- lots of good, practical advice on asking good questions out there. Your questions are often not among the more focused ones. – Dirk Eddelbuettel Oct 23 '12 at 11:32
  • @Dirk, by the way, in fairness, despite my hating being downvoted whenever my question is not perfectly written, I think the speed with which questions are answered about [r] is very impressive. – Hugh Perkins Oct 23 '12 at 12:50
  • @Dirk, whoa, it's possible to write papers about R. That's interesting. – Hugh Perkins Oct 23 '12 at 12:55
  • The output of `print(sprintf("hello %d\n", 56 ))` results in `"hello 56\n"`. Can you please explain why you want the "\n" to show up in the output as a literal? That is NOT what printf is intuitively supposed to show based on its behavior in other programming languages, notably C. Please change your question so that it makes sense. – stackoverflowuser2010 Sep 03 '14 at 06:09
  • Printing in R is discouraged. It's considered proper encapsulation for functions to not reveal information about their state, this reduces errors from young programmers. This explains why you can't print strings and numbers on the same line without a 2 or 3 lines of code. – Eric Leschinski Jan 26 '17 at 23:27

4 Answers4

34
printf <- function(...) invisible(print(sprintf(...)))

The outer invisible call may be unnecessary, I'm not 100% clear on how that works.

zwol
  • 135,547
  • 38
  • 252
  • 361
  • 9
    Just to add to this, for future newbies: one can add this definition to ~/.Rprofile , and then it will automatically be present in all new sessions. – Hugh Perkins Oct 24 '12 at 06:47
  • 2
    Any insights into if invisible were required and what it does here? – WestCoastProjects Jan 18 '15 at 19:14
  • @javadba I have been unable to figure out whether it is necessary or not and have asked a new question specifically about that: http://stackoverflow.com/questions/32573848/when-does-the-object-returned-by-invisible-cease-to-be-invisible – zwol Sep 14 '15 at 21:02
  • 2
    I've found that I'm better off doing a `cat(sprintf(...))` - no invisible or print needed. – Reinderien Feb 05 '17 at 02:54
  • @Reinderien, that is the solution. It is a bit cumbersome, especially when concatenate strings, but that is working – Coliban Mar 28 '19 at 09:27
  • `help(invisible)`: *This function can be useful when it is desired to have functions return values which can be assigned, but which do not print when they are not assigned.* – not2qubit Nov 20 '20 at 13:28
22

My solution:

> printf <- function(...) cat(sprintf(...))
> printf("hello %d\n", 56)
hello 56

The answers given by Zack and mnel print the carriage return symbol as a literal. Not cool.

> printf <- function(...) invisible(print(sprintf(...)))
> printf("hello %d\n", 56)
[1] "hello 56\n"
> 
> printf <- function(...)print(sprintf(...))
> printf("hello %d\n", 56)
[1] "hello 56\n"
stackoverflowuser2010
  • 38,621
  • 48
  • 169
  • 217
  • -1 This doesn't give the same output as requested by the OP. You are writing the string to the console, not printing the object, which is a different operation entirely in R. The literal `\n` *is* important in the output. – Gavin Simpson Feb 23 '14 at 22:09
  • You obviously don't know what printf is supposed to do. I've flagged your comment. – stackoverflowuser2010 Feb 24 '14 at 02:14
  • 3
    I *do*, but the OP *explicitly* asks for a function that would give them the same output as `print(sprintf("hello %d\n", 56 )`, which isn't what `cat()` or `writeLines()` on a string produces. The literal `\n` is how R views the way the string is represented. Catting that gives the string, not the R representation. Given you added this, with "snark", years after the Q&A was initiated & in clear contradiction of what the OP wants, I don't see why a -1 here is inappropriate. You might have spent you time better tidying the Question for example. But you prefer being petty. – Gavin Simpson Feb 24 '14 at 04:22
4

If you want a function that does print(sprintf(...)), define a function that does that.

printf <- function(...)print(sprintf(...))


printf("hello %d\n", 56)
## [1] "hello 56\n"
 d <- printf("hello %d\n", 56)
## [1] "hello 56\n"
d
## [1] "hello 56\n"
mnel
  • 113,303
  • 27
  • 265
  • 254
0

I do it this way:

printf = function(s, ...) cat(paste0(sprintf(s, ...)), '\n')
nickray
  • 9
  • 2