English 中文(简体)
x与(“x”)之间的距离?
原标题:Strange difference between x and get("x")?
  • 时间:2011-11-10 10:02:52
  •  标签:
  • r

有时,我最后这样说:

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

我可以可靠地复制。 我在《刑法》中做了哪些事情? 为什么在迅速发现<代码>x时打字,但get(x)不是? <代码>x和get(`x')内部有何区别?

任何方面都非常赞赏。 我从R 2.14.0开始看到这一点,但我的《刑法》也在发生变化。

EDIT:可推广的实例

// 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
",
             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
", 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
", 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     
> 

任何想法? 如果对<代码>(FALSE)A线作出评论,则该编码行将予以罚款。 对于反复试验,R必须每次重新开始。

最佳回答

这确实是我的法典。 我知道TRUELENGTH有时被R所利用,但我没有思考CHARSXP。 如果一个变数名称与一些特性值相同,则R公司使用CHARSXP TRUELENGTH持有内部散射值,见主/envir.c。 我的SET_TRUELENGTH on the CHARSXP was 遮掩了 has。 感谢西蒙·巴雷克对此作出解释,并感谢所有意见和看法。

To demonstrate :

$ 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

The C Code to see the TRUELENGTH of the CHARSXP is :

// 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
",
                  CHAR(s), LENGTH(s), TRUELENGTH(s));
    return(R_NilValue);
}
问题回答

评注与问题相当接近,这似乎难以/不可能复制:

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>

同样微薄:

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

我们需要看到一个可再生的例子。 如果它只打上你的系统,特别是在以下之后:data。 当时,你不得不去看。 在某种程度上,在封闭框架逻辑上的看像标志似乎被击中头部。





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签