r - `print(x)` not giving same output as `x` -
at r console, expected print(x)
give same output x
. had assumed print
used console printing everything. there null here print
:
library(data.table) print(data.table(1)[0]) # empty data.table (0 rows) of 1 col: v1 # null # why 'null' printed here? data.table(1)[0] # empty data.table (0 rows) of 1 col: v1 # .. no 'null' here?
this sample data created data.table
package, think general question still applies when not using data.table
: function/method used print return values @ console?
# r --vanilla # r version 3.2.3
update :
the fix has been merged in v1.10.5. michael chirico.
after running:
install.packages('data.table', type = 'source', repos = 'http://rdatatable.github.io/data.table')
it work expected:
library(data.table) # data.table 1.10.5 in development built 2017-05-18 00:04:56 utc; travis # fastest way learn (by data.table authors): https://www.datacamp.com/courses/data-analysis-the-data-table-way # documentation: ?data.table, example(data.table) , browsevignettes("data.table") # release notes, videos , 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
it might because print
method data.table
doing wrong thing. print methods expected return invisibly. suspect data.table:::print.data.table
returning visibly.
(update: i've submitted a bug report data.table. apologies them if i've analyzed incorrectly! )
from ?print
:
‘print’ prints argument , returns invisibly (via ‘invisible(x)’).
here tiny demo of might happening:
> x=list() > class(x) <- 'x' > print.x <- function(x) { print("i printing"); return(1729); } > x [1] "i printing" > print(x) [1] "i printing" [1] 1729
note how typing x
on own prints text, no number. typing print(x)
causes number printed also.
then, if arrange print method return invisibly follows:
> print.x <- function(x) { print("i printing"); return(invisible(1729)); }
.. print(x)
gives expected output
> print(x) [1] "i printing"
so, when type x
@ console, console call print
on behalf , ignores return value print
(which may visible). if type print(x)
, return value of print
printed if visible.
the ?print
documentation bit misleading think. print
methods supposed return argument , supposed invisibly, these rules not enforced
Comments
Post a Comment