Search code examples
rprintingconsoleformattingstdio

How to print out multiple items to output?


I am a beginner in R, and currently for my output I use:

print("Hello World!")

However, I would like to give something like this out:

x <- 1
print("Hello World!" + x)#the + x does not 

How can such a thing be coded in R?

I appreciate your answer!


Solution

  • Use cat:

    cat("Hello World!", x, '\n')
    

    (Note the trailing '\n', otherwise no newline character will be appended.)

    Alternatively, you can combine the print statement with a formatting statement (sprintf);

    cat(sprintf('Hello World!%s\n', x))
    

    The formatting syntax of sprintf corresponds to that in C. The documentation linked above has more information.