If I enter
print(cat(""))
I get
NULL
I want to use cat()
to print out the progress of an R script, but I don't understand why it is returning NULL
at the end of all of my concatenated strings, and more importantly, how to get it to stop?
All your answers are in the documentation for
?cat
. The portions that answer your specific question are:... and ...
So you can't stop
print(cat(...))
from returningNULL
because that's whatcat
returns. And you need to explicitly add newlines likecat("foo\n")
.If you want to assign it to a variable, for use in a LOOP of *apply or function (x), try this:
The name is the variable, the ".y" adds a string to it, paste says to print in, eval evaluates the print, <- assigns it to a variable, and ax is that variable.
I have had the exact same problem. In a nutshell,
cat()
is a little wonky under R. You didn't go into great detail about how you are trying to usecat()
but I would suggest looking atpaste()
.?paste
I think it may be what you are looking for.
For this, I often use
writeLines()
, in combination withstrwrap()
, andpaste()
to combine say the loop value if I'm printing out info on the current iteration.strwrap()
handles wrapping long lines as required, andwriteLines()
means I don't have to remember to add a"\n"
on the end of mycat()
calls.Here is an example using it to print out an iteration indicator:
Gives:
I do not see the need to use
print(cat())
. To printing a messagecat()
is already sufficient. This may be what you are looking for:NULL is the return value of "cat()". If you omit the outer "print()" you won't see the NULL.