title: Strange difference between x and get("x")?
body: <p>Somehow, sometimes, I'm ending up in a state like this :</p>
  
  <pre><code>&gt; x
  [1] 1 2 3
  &gt; get("x")
  Error in get("x") : object 'x' not found
  &gt; x
  [1] 1 2 3
  </code></pre>
  
  <p>I can't reproduce it reliably. What sort of things might I have done wrong in my C code? Why would typing <code>x</code> at the prompt find it, but <code>get("x")</code> not? What's the difference internally between <code>x</code> and <code>get("x")</code>?</p>
  
  <p>Any hints much appreciated.  I've started seeing this since R 2.14.0 but my C code has also been changing too.</p>
  
  <p>EDIT : reproducible example</p>
  
  <pre><code>// test.c
  #include &lt;R.h&gt;
  #include &lt;Rdefines.h&gt; 
  
  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&lt;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);
  }
  </code></pre>
  
  <p>and to run it :</p>
  
  <pre><code>R --vanilla
  
  system("R CMD SHLIB -otest.so test.c")
  dyn.load("test.so")
  
  if (FALSE) A     # needed for error to occur (!)
  
  DF &lt;- 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")
  }
  </code></pre>
  
  <p>The output I get :</p>
  
  <pre><code>$ R --vanilla    
  R version 2.14.0 (2011-10-31)
  # [snip header]
  &gt; 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
  &gt; dyn.load("test.so")
  &gt; 
  &gt; if (FALSE) A     # needed for error to occur (!)
  &gt; 
  &gt; DF &lt;- data.frame(a = c("A", "Z"), b = 1:4)
  &gt; print(DF)
    a b
  1 A 1
  2 Z 2
  3 A 3
  4 Z 4
  &gt; .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
  &gt; print(DF)
    a b
  1 A 1
  2 Z 2
  3 A 3
  4 Z 4
  &gt; 
  &gt; A = data.frame()
  &gt; 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
  &gt; 
  &gt; 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     
  &gt; 
  </code></pre>
  
  <p>Any ideas?   If the <code>if (FALSE) A</code> line is commented out then it works fine. For repeated tests, R must be started fresh each time.</p>