使用mget()使用rbindlist()加入data.table时出现意外的错误消息

时间:2017-12-06 17:13:20

标签: r data.table

在准备this answer时,我收到了错误消息

  

错误:找不到'spine_hlfs'的值

从正在运行

setDT(giraffe)[rbindlist(mget(df_names), idcol = "df.name"), on = "runkey", project := df.name][]

,而

df_list <- mget(df_names)
setDT(giraffe)[rbindlist(df_list, idcol = "df.name"), on = "runkey", project := df.name][]

按预期工作。

在github上报告问题之前,我想与社区核实这确实是一个错误,或者是否有一个我不知道的错误消息的简单解释。

可重复的例子

set.seed(123L)
giraffe <- data.frame(runkey = 1:500,
                      X2 = sample.int(99L, 500L, TRUE),
                      X3 = sample.int(99L, 500L, TRUE),
                      X4 = sample.int(99L, 500L, TRUE),
                      project = "",
                      stringsAsFactors = FALSE)
spine_hlfs <- data.frame(runkey = c(1L, 498L, 5L))
ir_dia     <- data.frame(runkey = c(3L, 499L, 47L, 327L))
df_names <- c("spine_hlfs", "ir_dia")
library(data.table)

# this creates the error message
setDT(giraffe)[rbindlist(mget(df_names), idcol = "df.name"), on = "runkey", project := df.name][]
## Error: value for ‘spine_hlfs’ not found

# this works as expected
df_list <- mget(df_names)
setDT(giraffe)[rbindlist(df_list, idcol = "df.name"), on = "runkey", project := df.name][]

1 个答案:

答案 0 :(得分:6)

这基本上是因为(与mget} inherits = FALSE不同,mget(df_names, inherits = TRUE)默认为mget(df_names, envir = .GlobalEnv)。因此它只能在当地环境中寻找。更改为mget(或者,如果您希望明确data.table),则应解决此问题。

This was independently reported by @Arun on GH a while ago他打算将get的默认行为(在View v = findViewById(R.id.buttonlog); v.setOnClickListener((View.OnClickListener) this); 内使用时)更改为与 Button loginButton = (Button)findViewById(R.id.buttonlog); loginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this,LoginActivity.class); startActivity(intent); } }); 保持一致,敬请期待。