数据表出现“对象未找到”错误。

19

我有一个 data.table:

library(data.table)
mydt <- data.table(index = 1:10)

我在全局环境中尝试时可以使其正常工作,但在调试器中或在包测试中使用它时无法正常工作。

问题在于我不能以标准方式对其进行子集筛选。

Browse[2]> mydt[,index]
Error in `[.data.frame`(x, i, j) : object 'index' not found
Browse[2]> mydt[,list(index)]
Error in `[.data.frame`(x, i, j) : object 'index' not found

下面是一个可重现的例子,我在其中创建了一个包,并在评估名为myfunction的函数时出现了错误:

library(devtools)
setwd(tempdir())
# make dummy package called foo
create("foo")
setwd("foo")

# add data.table as a package dependency
a <- readLines("DESCRIPTION")
depends.idx <- grepl("Depends", a)
a[depends.idx] <- paste0(a[depends.idx], ", data.table")
writeLines(a, "DESCRIPTION")

# create a dummy function 
writeLines("myfunction <- function() {a <- data.table(b=1); return(a[,b])}",
            "R/foo.R")

# check and throw error
check()
library(foo)
myfunction()

这里是会话信息:

Browse[2]> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C         LC_TIME=C            LC_COLLATE=C         LC_MONETARY=C       
 [6] LC_MESSAGES=C        LC_PAPER=C           LC_NAME=C            LC_ADDRESS=C         LC_TELEPHONE=C      
[11] LC_MEASUREMENT=C     LC_IDENTIFICATION=C 

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] PEcAn.data.atmosphere_1.3.3 data.table_1.9.2            RPostgreSQL_0.4             PEcAn.settings_1.3.3       
 [5] lubridate_1.3.3             PEcAn.DB_1.3.3              DBI_0.2-7                   PEcAn.utils_1.3.3          
 [9] udunits2_0.6                ncdf4_1.12                  randtoolbox_1.14            rngWELL_0.10-2             
[13] ggplot2_1.0.0               XML_3.98-1.1                plyr_1.8.1                  abind_1.4-0                
[17] testthat_0.8.1              devtools_1.5.0.99          

loaded via a namespace (and not attached):
 [1] MASS_7.3-29      RCurl_1.95-4.1   Rcpp_0.11.2      colorspace_1.2-4 digest_0.6.4     evaluate_0.5.5   grid_3.0.2      
 [8] gtable_0.1.2     httr_0.3         memoise_0.2.1    munsell_0.4.2    parallel_3.0.2   proto_0.3-10     reshape2_1.4    
[15] roxygen2_4.0.1   scales_0.2.4     stringr_0.6.2    tools_3.0.2      whisker_0.3-2      

2
@Pascal index 是 data.table 的一列名称。如果您不熟悉 data.table 包,其中一个特点是允许在不在列名周围加引号的情况下进行索引。 - Abe
1
你在检查时是否注意到了这些行?
  • 检查 R 代码中的依赖项... 注意 Depends 字段中的包未从 'data.table' 导入 这些包需要从 (NAMESPACE 文件中) 导入,以便在加载但未附加此命名空间时使用。
- user3710546
1
你是否遵循了https://dev59.com/GWkv5IYBdhLWcg3wiRWo#10529888? - Matt Dowle
1
@MattDowle,这不是我发布的可重现示例中演示过了吗?它说要将data.table放在DESCRIPTION文件的Depends部分,对吗? - Abe
2
@arun 是的,看起来是一样的。 - Abe
显示剩余14条评论
1个回答

8

非常好的可重现问题!这是我得到的结果。由于您没有发布您的输出,因此可能您得到的输出与我不同。我已经分割了输出,并在可能出问题的位置放置了HERE

$ R
R version 3.1.0 (2014-04-10) -- "Spring Dance"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

> library(devtools)
> setwd(tempdir())
> # make dummy package called foo
> create("foo")
Creating package foo in .
No DESCRIPTION found. Creating with values:

Package: foo
Title: 
Description: 
Version: 0.1
Authors@R: # getOptions('devtools.desc.author')
Depends: R (>= 3.1.0)
License: # getOptions('devtools.desc.license')
LazyData: true
> setwd("foo")
> 
> # add data.table as a package dependency
> a <- readLines("DESCRIPTION")
> depends.idx <- grepl("Depends", a)
> a[depends.idx] <- paste0(a[depends.idx], ", data.table")
> writeLines(a, "DESCRIPTION")
> 
> # create a dummy function 
> writeLines("myfunction <- function() {a <- data.table(b=1); return(a[,b])}",
+             "R/foo.R")
> 
> # check and throw error
> check()
Loading required package: roxygen2
Updating foo documentation
Loading foo

这里

Invalid DESCRIPTION:
Authors@R field gives no person with author role.
Authors@R field gives no person with maintainer role and email address.

See the information on DESCRIPTION files in section 'Creating R packages' of the 'Writing R Extensions' manual.

Loading required package: data.table
data.table 1.9.2  For help type: help("data.table")

Attaching package: ‘data.table’

The following object is masked from ‘package:bit’:

    setattr

Updating collate directive in  /tmp/Rtmp0h3yz9/foo/DESCRIPTION 
Updating namespace directives
Writing foo.Rd
'/usr/lib/R/bin/R' --vanilla CMD build '/tmp/Rtmp0h3yz9/foo' --no-manual --no-resave-data 

* checking for file '/tmp/Rtmp0h3yz9/foo/DESCRIPTION' ... OK
* preparing 'foo':

这里

* checking DESCRIPTION meta-information ... ERROR
Authors@R field gives no person with author role.
Authors@R field gives no person with maintainer role and email address.

See the information on DESCRIPTION files in section 'Creating R
packages' of the 'Writing R Extensions' manual.

Error: Command failed (1)
> library(foo)
> myfunction()
Error in `[.data.frame`(x, i, j) : object 'b' not found
> 

1
似乎是一个旧的开发工具错误 https://github.com/hadley/devtools/issues/192 - Abe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接