How to do printf in r?

Solution 1:

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

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

Solution 2:

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"

Solution 3:

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"