Eka
Eka

Reputation: 15000

How to print text and variables in a single line

Is there a way to print text and variables in a single line in r

eg

a="Hello"
b="EKA"
c="123456"

print("$a !! my name is $b and my number is $c")

out put will be like this

Hello !! my name is EKA and my number is 123456

Upvotes: 19

Views: 95136

Answers (3)

profPlum
profPlum

Reputation: 498

I like to do this: print(c('x=',x)) The output format is messy but all and all it is most convenient and flexible method I know (it works INSIDE FUNCTIONS unlike the sprintf solution & it can handle more data types too).

It is much faster to type than: print(paste('x=',x)). Which is rather cumbersome for a simple print function!

Notable Alternative: cat('x=',x,'\n')

I tend to avoid this also because I find it tedius to always specify the newline character. Although technically it was 'the function' that was invented to easily do what you are asking, it was designed poorly IMO.

Upvotes: 2

Elligno
Elligno

Reputation: 79

print( paste("You have choosen the following file name: ", fileName)) 

will do the job

Upvotes: 2

MarkusN
MarkusN

Reputation: 3223

I'd suggest using sprintf function. The advantage of this function is, that the variables can be of any class (here, c is numeric).

a="Hello"
b="EKA"
c=123456

sprintf("%s !! my name is %s and my number is %i", a, b, c)

Upvotes: 26

Related Questions