Print string and variable contents on the same line in R

216

Is there a way to print text and variable contents on the same line? For example,

wd <- getwd()
print("Current working dir: ", wd)

I couldn't find anything about the syntax that would allow me to do this.

This question is tagged with r printing

~ Asked on 2013-03-23 17:07:06

The Best Answer is


328

You can use paste with print

print(paste0("Current working dir: ", wd))

or cat

cat("Current working dir: ", wd)

~ Answered on 2013-03-23 17:08:41


67

{glue} offers much better string interpolation, see my other answer. Also, as Dainis rightfully mentions, sprintf() is not without problems.

There's also sprintf():

sprintf("Current working dir: %s", wd)

To print to the console output, use cat() or message():

cat(sprintf("Current working dir: %s\n", wd))
message(sprintf("Current working dir: %s\n", wd))

~ Answered on 2013-03-23 17:18:12


Most Viewed Questions: