`print (x)` does not give the same result as `x`

On the R console, I expected print(x) always produce the same result as x . I have always assumed that print used by the console to actually print for everything. But there is an extra NULL from print :

 library(data.table) print(data.table(1)[0]) # Empty data.table (0 rows) of 1 col: V1 # NULL # why is this 'NULL' printed here? data.table(1)[0] # Empty data.table (0 rows) of 1 col: V1 # .. but no 'NULL' here? 

This sample data is created by the data.table package, but I think that the general question still applies even if data.table is not used: what function / method is used to print the return values ​​to the console?

 # R --vanilla # R version 3.2.3 
+6
source share
3 answers

Update:

The fix has just been merged in v1.10.5. Thanks to Michael Chirico.

After launch:

 install.packages('data.table', type = 'source', repos = 'http://Rdatatable.imtqy.com/data.table') 

It will work as expected:

 library(data.table) # data.table 1.10.5 IN DEVELOPMENT built 2017-05-18 00:04:56 UTC; travis # The fastest way to learn (by data.table authors): https://www.datacamp.com/courses/data-analysis-the-data-table-way # Documentation: ?data.table, example(data.table) and browseVignettes("data.table") # Release notes, videos and slides: http://r-datatable.com print(data.table(1)[0]) # Empty data.table (0 rows) of 1 col: V1 data.table(1)[0] # Empty data.table (0 rows) of 1 col: V1 

Perhaps this is due to the fact that the print method for data.table does the wrong thing. Printing methods are expected to be invisible. But I suspect that data.table:::print.data.table returning noticeably.

(Update: I just sent an error report to data.table . I apologize to them if I analyzed it incorrectly!)

From ?print :

'prints its argument and returns it invisibly (via "invisible (x)).

Here is a tiny demonstration of what might happen:

 > x=list() > class(x) <- 'X' > print.X <- function(x) { print("I am printing"); return(1729); } > x [1] "I am printing" > print(x) [1] "I am printing" [1] 1729 

Note that typing x by itself simply prints text, but not a number. But by typing print(x) , you can also print the number.

Then, if I arrange for this printing method to return invisibly as follows:

 > print.X <- function(x) { print("I am printing"); return(invisible(1729)); } 

.. then print(x) gives the expected output

 > print(x) [1] "I am printing" 

So, when you type x on the console, the console calls print on your behalf and ignores the return value from print (which may be visible). But if you type print(x) , then the return value of print will be printed if it is visible.


The documentation ?print bit wrong, I think. The print methods are supposed to return their argument and should do this invisibly, but these rules do not apply.

+8
source

This may be due to the print return value. A.

From ?print :

'prints its argument and returns it invisibly (via "invisible (x)).

So print has a return value, which in this case is NULL . On the other hand, entering a variable in the console produces output, but it does not return anything, and NULL not displayed.

+3
source

The printing method invoked depends on the class of the object. So there is print.table, print.data.frame, print.factor, etc., Each of them has a different function that determines what is printed.

try it

 x<-NULL print.data.frame(x) print.table(x) 

Look at the features

 print.data.frame print.table 

You will see what returns, depending on which S3 printing method is invoked, and there are many printing methods. A.

The exact print you invoke depends on the class of your data.table object.

It also means that you can create your own S3 method for printing by assigning your own class to your object and creating your own print function.

 class(object)<-"customclass" print.customclass<-function(object,...){ body of function } print(object) 
0
source

All Articles