:=在data.table中分配后不抑制输出的选项 [英] An option to not suppress output after := assignment in data.table

查看:64
本文介绍了:=在data.table中分配后不抑制输出的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与> data.table对象在之后不打印从功能返回,但不是伪造。这个问题是专门寻找一种方法,在替换时使用赋值:= data.table行时不抑制输出。因此,在评估会导致R调用打印方法的情况下。

This is related to data.table objects not printed after returned from function but is not a dupe. This question is to specifically look for a way to not suppress output when using an assignment := data.table line at the repl. So in cases where an eval would cause R to call the print method.

我已经升级并注意到:= 现在隐身返回。当我使用交互式工作流进行开发时,这会在构建管道时中断流程,添加分配:= 行,然后希望看到结果用作上下文添加一个额外的步骤。

I've upgraded and noticed that := returns invisibly now. As I develop with an interactive workflow this disrupts the flow when I am building a pipeline, add an assignment := line, and then expect to see the results to use as context for adding an additional step.

我可以在任务行中添加 [] ,但这是在意识到我的任务行没有不会打印(因为我之前没有这样做),因此在添加之前,我的流程刚刚中断。或者,有人可能会争辩说,我应该始终在每个分配行上使用 [] ,这样我的流程就不会被打断,但是我必须记住仅这样做分配行,并且由于管道通常将分配与过滤器混合使用,因此在实践中,这会给最终用户带来额外的认知开销,使最终用户感到不必要,并且在管道中逐行移动时看起来不一致。

Sure I can add [] to the assignment lines, but this would be after realizing that my assignment line didn't print (because I didn't do this prior), and so my flow was just disrupted prior to adding this. Or, one might argue that I should always tack on [] with every assignment line so that my flow isn't disrupted, but then I have to remember to do that for only assignment lines, and since a pipeline commonly mixes assignment with filter, in practice this introduces extra cognitive overhead that - to an end user - feels unnecessary and looks inconsistent when going line by line in the pipeline.

我不希望选择是否隐式退货,因为我认为这已经进行了充分讨论,并且已经为大多数人和用例做出了正确的决定。相反,是否可以设置一个选项或解决方法,以使:= 不会隐式返回?

I would rather not argue about choosing to return invisibly or not, as I figure this has been thoroughly discussed already, and the correct decision for the majority of people and use cases has been made. Instead, is there an option I can set or workaround to have := not return invisibly?

推荐答案

1.9.6中的一种方法是修补print.data.table S3方法。

One approach in 1.9.6 is to patch the print.data.table S3 method.

在调用原始方法之前,请设置。 global $ print值设置为(默认值)。在data.table想要隐式返回(例如,赋值:=行)的情况下,这取消了在调用通用打印方法(使用动态作用域规则)之前刚刚更改此值的方式。

Prior to calling the original method, set the .global$print value to "" (default). This undoes how this value was just changed prior to the generic print method being called (using dynamic scoping rules), in the case where data.table would like to return invisibly (e.g., an assignment := line).

效果是仍然调用data.table的自定义打印方法,但是data.table不再尝试修改R的默认逻辑来决定何时以及何时不打印。

The effect is that the custom print method for data.table is still called, but data.table no longer tries to modify R's default logic to decide when and when not to print.

一个天真的解决方案,因为我仍在学习包,名称空间,环境,S3方法等。

Likely a naive solution, as I'm still learning about packages, namespaces, environments, S3 methods, etc.

library(data.table)
print.data.table.orig = get('print.data.table', envir=asNamespace('data.table'))
print.data.table.patch = function(x, ...) {
  .globalRef = get('.global', envir=asNamespace('data.table'))
  .globalRef$print = ""
  print.data.table.orig(x, ...)
}

library(R.methodsS3)
setMethodS3('print', 'data.table', print.data.table.patch) 


fTbl = data.table(x=1:500000)
fTbl[, x := 5]
        x
     1: 5
     2: 5
     3: 5
     4: 5
     5: 5
    ---  
499996: 5
499997: 5
499998: 5
499999: 5
500000: 5

fTbl
        x
     1: 5
     2: 5
     3: 5
     4: 5
     5: 5
    ---  
499996: 5
499997: 5
499998: 5
499999: 5
500000: 5
> 

这篇关于:=在data.table中分配后不抑制输出的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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