47

I use print to output from a function in R, for example:

print("blah blah blah")

This outputs

[1] "blah blah blah"

To the console. How can I avoid the [1] and the quotes?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
John Montague
  • 1,910
  • 7
  • 21
  • 30

4 Answers4

38

Use cat("Your string") (type ?cat to see help page) to output concatenated objects to standard output.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • 1
    You probably want `cat("Your string\n")` since `print("Your string")` automatically adds line breaks – Nat May 11 '20 at 17:53
23

message is probably the best function to replace print for your needs. cat is also a good function to look at but message will print a new line for you as well. It is also better to use since it is easier to suppress the output from message than it is to suppress the output from cat.

If you just want to remove the quotes but don't mind the [1] printing then you could use the quote=FALSE option of print.


Edit: As noted in the comments, message isn't the same as a call to print as it sends the output to a different connection. Using cat will do what you want as others have noted but you'll probably want to add a new line on after your message.

Example

cat("This is a message\n") # You'll want to add \n at the end
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
Dason
  • 60,663
  • 9
  • 131
  • 148
  • 6
    Don't use `message`. It sends its output to the `stderr` connection, as opposed to `cat` (and `print`) which use `stdout`, and this can throw you if you ever decide to redirect your output, eg with `sink`. `message` is really more akin to `warning`, in that it's meant for diagnostics and warnings. – Hong Ooi Feb 04 '12 at 03:56
  • Ah. Thank you. I forgot about that. I usually use it just to print messages inside of functions either while debugging or to display some sort of progress check on long functions. I like it because then you can use suppressMessages easily (and I prefer using this as opposed to sink). But you're right and print and messages aren't exactly interchangeable. Using cat is probably a better choice. – Dason Feb 04 '12 at 04:41
  • 3
    It always depends on what you really mean. If you mean, yes, just print the text, I want to see it and nobody can stop me, you can use `cat()`; if you mean this is a message (should not go to `stdout`), `message()` is the correct choice. I believe many people are abusing `cat()`, especially in package startup messages. This is pretty annoying actually, because it is difficult to suppress the messages. The difference in the output from `cat()` and `message()` looks subtle, but there is a big difference behind the scene. – Yihui Xie Feb 04 '12 at 16:58
7

If you want to get rid of the [1] only, but preserve the line breaks:

string_vector <- c('some', 'words', 'in', 'a', 'vector')
paste(string_vector, collapse = '\n') %>% cat()

produces:

some
words
in
a
vector
Andrew
  • 9,090
  • 8
  • 46
  • 59
3

Messing around with print.data.frame may sometimes be useful for custom print methods.

print(data.frame("message" = "", row.names = "blah blah blah"))
#                message
# blah blah blah  

print(data.frame("message" = "blah blah blah", row.names = ""))
#         message
#  blah blah blah
CSJCampbell
  • 2,025
  • 15
  • 19