何时和为什么“打印”需要两次尝试打印“data.table”? [英] When and why does "print" need two attempts to print a "data.table"?

查看:102
本文介绍了何时和为什么“打印”需要两次尝试打印“data.table”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时 print 需要两次尝试打印 data.table

Sometimes print needs two attempts to print a data.table:

> library(data.table)
> 
> rm(list=ls())
> 
> Tbl <- fread( input = "Nr; Value
+                        Nr 1;46.73
+                        Nr 2;49.02
+                        Nr 3;50.62
+                        Nr 4;49.80
+                        Nr 5;50.15",
+               sep    = ";",
+               header = TRUE,
+               colClasses = c("character","numeric") )
> print(Tbl)
     Nr Value
1: Nr 1 46.73
2: Nr 2 49.02
3: Nr 3 50.62
4: Nr 4 49.80
5: Nr 5 50.15
> Tbl <- Tbl[, Nr := as.numeric( gsub( "Nr ", "", Tbl$Nr ))]
> print(Tbl)
> print(Tbl)
   Nr Value
1:  1 46.73
2:  2 49.02
3:  3 50.62
4:  4 49.80
5:  5 50.15
> 

不是这样 data.frame

> rm(list=ls())
> 
> DF <- read.table( text = "Nr; Value
+                           Nr 1;46.73
+                           Nr 2;49.02
+                           Nr 3;50.62
+                           Nr 4;49.80
+                           Nr 5;50.15",
+                   sep    = ";",
+                   header = TRUE,
+                   colClasses = c("character","numeric"))
> 
> DF$Nr <- as.numeric( gsub( "Nr ", "", DF$Nr ))
> print(DF)
  Nr Value
1  1 46.73
2  2 49.02
3  3 50.62
4  4 49.80
5  5 50.15
> 

如果代码包含在脚本文件中, data.table 会立即打印:

If the code is contained in a script file, the data.table is printed immediately:

> source(path_to_Script_1,echo=TRUE,prompt.echo="(script)   ",max.deparse.length=500)

(script)   library(data.table)

(script)   rm(list=ls())

(script)   Tbl <- fread( input = "Nr; Value
+                        Nr 1;46.73
+                        Nr 2;49.02
+                        Nr 3;50.62
+                        Nr 4;49.80
+                        Nr 5;50.15",
+               sep    = ";",
+               header = TRUE,
+               colClasses = c("character","numeric") )

(script)   Tbl <- Tbl[, Nr := as.numeric( gsub( "Nr ", "", Tbl$Nr ))]

(script)   print(Tbl)
   Nr Value
1:  1 46.73
2:  2 49.02
3:  3 50.62
4:  4 49.80
5:  5 50.15
> 

但如果 print(Tbl)从脚本文件中, print 在控制台上需要两次尝试:

But if print(Tbl) is omitted from the script file, print on the console again needs two attempts:

> source(path_to_Script_2,echo=TRUE,prompt.echo="(script)   ",max.deparse.length=500)

(script)   library(data.table)

(script)   rm(list=ls())

(script)   Tbl <- fread( input = "Nr; Value
+                        Nr 1;46.73
+                        Nr 2;49.02
+                        Nr 3;50.62
+                        Nr 4;49.80
+                        Nr 5;50.15",
+               sep    = ";",
+               header = TRUE,
+               colClasses = c("character","numeric") )

(script)   Tbl <- Tbl[, Nr := as.numeric( gsub( "Nr ", "", Tbl$Nr ))]
> print(Tbl)
> print(Tbl)
   Nr Value
1:  1 46.73
2:  2 49.02
3:  3 50.62
4:  4 49.80
5:  5 50.15
> 

任何人都可以告诉我何时为什么打印需要两次尝试?
我使用R版本3.2.2:

Can anybody tell me when ans why print needs two attempts? I'm using R version 3.2.2:

> R.version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          2.2                         
year           2015                        
month          08                          
day            14                          
svn rev        69053                       
language       R                           
version.string R version 3.2.2 (2015-08-14)
nickname       Fire Safety   

操作系统为Windows 7。

Operating system is Windows 7.

推荐答案

引用新闻(1.版本1.9.6中的错误修复):

Quoting the NEWS (1. bug fix in version 1.9.6):


if ,LHS:= RHS] 不再打印,#869和#1122。测试添加。
为了让这个工作,我们不得不忍受一个缺点:如果:=
在一个函数内使用没有 DT [] 在函数结束之前,
然后下一次 DT (DT)是在提示符下键入的,不会打印
。将打印重复的 DT 打印(DT)。为了避免这种情况:
在函数中的最后一个:= 之后包含 DT [] 如果这不是
可能(例如,它不是一个可以更改的函数),则保证打印
提示符下的 DT [] 。 / p>

if (TRUE) DT[,LHS:=RHS] no longer prints, #869 and #1122. Tests added. To get this to work we've had to live with one downside: if a := is used inside a function with no DT[] before the end of the function, then the next time DT or print(DT) is typed at the prompt, nothing will be printed. A repeated DT or print(DT) will print. To avoid this: include a DT[] after the last := in your function. If that is not possible (e.g., it's not a function you can change) then DT[] at the prompt is guaranteed to print.

< - 是一个函数。当然,你不应该使用< - 。它创建了一个不必要的副本(因为:= 修改了data.table列的位置),因此效率低下。

<- is a function. Of course, you shouldn't use <- there at all. It creates an unnecessary copy (since := modifies the data.table column in place) and is therefore inefficient.

这篇关于何时和为什么“打印”需要两次尝试打印“data.table”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆