Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in R, can I stop print(cat("")) from returning NULL? and why does cat("foo") return foo>

Tags:

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?

like image 680
David LeBauer Avatar asked Oct 29 '10 20:10

David LeBauer


People also ask

What is the difference between cat () and print () in R?

The simple printing method in R is to use print() . As its name indicates, this method prints its arguments on the R console. However, cat() does the same thing but is valid only for atomic types (logical, integer, real, complex, character) and names, which will be covered in the later chapters.

What does cat () do in R?

cat() function in R Language is used to print out to the screen or to a file.

Why does R return null?

NULL represents the null object in R. NULL is used mainly to represent the lists with zero length, and is often returned by expressions and functions whose value is undefined. as. null ignores its argument and returns the value NULL .

How do you print in R?

Every language provides some functions that can help you print the data on the console, and R is no different. To print the data on the console in R, use the print() function.


1 Answers

All your answers are in the documentation for ?cat. The portions that answer your specific question are:

Arguments:

fill: a logical or (positive) numeric controlling how the output is       broken into successive lines.  If ‘FALSE’ (default), only       newlines created explicitly by ‘"\n"’ are printed.       Otherwise, the output is broken into lines with print width       equal to the option ‘width’ if ‘fill’ is ‘TRUE’, or the value       of ‘fill’ if this is numeric.  Non-positive ‘fill’ values       are ignored, with a warning. 

... and ...

Value:

 None (invisible ‘NULL’). 

So you can't stop print(cat(...)) from returning NULL because that's what cat returns. And you need to explicitly add newlines like cat("foo\n").

like image 119
Joshua Ulrich Avatar answered Sep 18 '22 13:09

Joshua Ulrich