x和get(“x”)之间的奇怪差异?

时间:2011-11-10 10:02:53

标签: r

不知何故,有时候,我最终会处于这样的状态:

> x
[1] 1 2 3
> get("x")
Error in get("x") : object 'x' not found
> x
[1] 1 2 3

我无法可靠地重现它。在我的C代码中,我可能做错了什么?为什么在提示符下键入x会找到它,但get("x")不是? xget("x")之间的内部差异是什么?

任何提示都非常赞赏。我从R 2.14.0开始就看到这个,但我的C代码也一直在变化。

编辑:可重现的例子

// test.c
#include <R.h>
#include <Rdefines.h> 

SEXP test(SEXP df)
{
    SEXP levels, s;
    int j;

    levels = getAttrib(VECTOR_ELT(df,0), R_LevelsSymbol);
    Rprintf("levels %u, type %d, length %d, truelength %d\n",
             levels,TYPEOF(levels),LENGTH(levels),TRUELENGTH(levels));

    for (j=0; j<length(levels); j++) {
        s = STRING_ELT(levels,j);
        Rprintf("%d %d %s %u %d %d\n", length(levels), TYPEOF(s),
                        CHAR(s), s, LENGTH(s), TRUELENGTH(s));
        SET_TRUELENGTH(s,1);  // clobbers the 65, but why 65 ("A") there?
        Rprintf("%d %d %s %u %d %d\n", length(levels), TYPEOF(s),
                        CHAR(s), s, LENGTH(s), TRUELENGTH(s));
    }
    return(R_NilValue);
}

并运行它:

R --vanilla

system("R CMD SHLIB -otest.so test.c")
dyn.load("test.so")

if (FALSE) A     # needed for error to occur (!)

DF <- data.frame(a = c("A", "Z"), b = 1:4)
print(DF)
.Call("test",DF)
print(DF)

A = data.frame()
for (i in 1:100) {
    cat(i,"")
    assign(paste("v",i,sep=""),i)
    get("A")
}

我得到的输出:

$ R --vanilla    
R version 2.14.0 (2011-10-31)
# [snip header]
> system("R CMD SHLIB -otest.so test.c")
gcc -std=gnu99 -I/usr/share/R/include      -fpic  -std=c99 -O6 -Wall -Wno-unused -pedantic -c test.c -o test.o
gcc -std=gnu99 -shared -o test.so test.o -otest.so -L/usr/lib/R/lib -lR
> dyn.load("test.so")
> 
> if (FALSE) A     # needed for error to occur (!)
> 
> DF <- data.frame(a = c("A", "Z"), b = 1:4)
> print(DF)
  a b
1 A 1
2 Z 2
3 A 3
4 Z 4
> .Call("test",DF)
levels 151395176, type 16, length 2, truelength 0
2 9 A 149596512 1 65   # why this 65 here?
2 9 A 149596512 1 1
2 9 Z 149596320 1 0
2 9 Z 149596320 1 1
NULL
> print(DF)
  a b
1 A 1
2 Z 2
3 A 3
4 Z 4
> 
> A = data.frame()
> for (i in 1:100) {
+     cat(i,"")
+     assign(paste("v",i,sep=""),i)
+     get("A")
+ }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Error in get("A") : object 'A' not found
> 
> sessionInfo()
R version 2.14.0 (2011-10-31)
Platform: i686-pc-linux-gnu (32-bit)

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=C                 LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

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

有什么想法吗?如果注释掉if (FALSE) A行,那么它可以正常工作。对于重复测试,R必须每次都重新开始。

2 个答案:

答案 0 :(得分:6)

这确实是我的C代码。我知道TRUELENGTH有时被R使用但我没想到CHARSXP。当变量名与某个字符值相同时,R使用CHARSXP的TRUELENGTH来保存内部哈希值,请参阅main / envir.c。我在CHARSXP上的SET_TRUELENGTH破坏了哈希值。感谢Simon Urbanek解释这一点,并感谢评论中的所有提示和想法。

演示:

$ R --vanilla
R version 2.14.0 (2011-10-31)

> system("R CMD SHLIB -otest.so test.c")
> dyn.load("test.so")
> truelength = function(x)invisible(.Call("truelength",x))
> 
> truelength("A")
'A' has length 1 and truelength 0
> truelength("ABC")
'ABC' has length 3 and truelength 0
> A=123
> truelength("A")
'A' has length 1 and truelength 65    # 65 is the HASHPRI, for bound variable A
> truelength("ABC")
'ABC' has length 3 and truelength 0    # no variable ABC so truelength unused
> ABC=456
> truelength("ABC")
'ABC' has length 3 and truelength 17763   # now ABC symbol is bound
> 
> foo = 7
> truelength("foo")               
'foo' has length 3 and truelength 27999   # bound
> truelength("bar")               
'bar' has length 3 and truelength 0       # not bound
> .Internal(inspect("foo"))
@876eb08 16 STRSXP g0c1 [NAM(2)] (len=1, tl=0)   # tl=0 of STRSXP vector
  @81759e8 09 CHARSXP g0c1 [gp=0x21] "foo"       # tl of CHARSXP not shown by inspect

其中用于查看CHARSXP的TRUELENGTH的C代码是:

// test.c
#include <R.h>
#include <Rdefines.h> 

SEXP truelength(SEXP v)
{
    SEXP s = STRING_ELT(v,0);
    Rprintf("'%s' has length %d and truelength %d\n",
                  CHAR(s), LENGTH(s), TRUELENGTH(s));
    return(R_NilValue);
}

答案 1 :(得分:1)

评论流非常接近这个问题,这似乎很难/不可能重现:

R> x <- 1L:3L
R> x
[1] 1 2 3
R> get("x")
[1] 1 2 3
R> matt <- function() { y <- 7L:9L; get("y") }
R> matt()
[1] 7 8 9
R>

同样通过littler:

edd@max:~$ r -e 'x <- 1L:3L; print(get("x"))'
[1] 1 2 3
edd@max:~$

我们需要看一个可重复的例子。如果它只击中你的系统,特别是只有在你的data.table加载之后,你必须看看那里。不知何故,“封闭框架中的查找符号”逻辑似乎已被击中头部。