Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6825839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:04:43+00:00 2026-05-26T22:04:43+00:00

Somehow, sometimes, I’m ending up in a state like this : > x [1]

  • 0

Somehow, sometimes, I’m ending up in a state like this :

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

I can’t reproduce it reliably. What sort of things might I have done wrong in my C code? Why would typing x at the prompt find it, but get("x") not? What’s the difference internally between x and get("x")?

Any hints much appreciated. I’ve started seeing this since R 2.14.0 but my C code has also been changing too.

EDIT : reproducible example

// 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);
}

and to run it :

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")
}

The output I get :

$ 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     
> 

Any ideas? If the if (FALSE) A line is commented out then it works fine. For repeated tests, R must be started fresh each time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:04:43+00:00Added an answer on May 26, 2026 at 10:04 pm

    This indeed turned out to be my C code. I knew TRUELENGTH is sometimes used by R but I didn’t think on CHARSXP. When a variable name is the same as some character value, the CHARSXP’s TRUELENGTH is used by R to hold an internal hash value, see main/envir.c. My SET_TRUELENGTH on the CHARSXP was clobbering the hash. Thanks to Simon Urbanek for explaining this, and thanks for all the tips and ideas in comments.

    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
    

    where 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\n",
                      CHAR(s), LENGTH(s), TRUELENGTH(s));
        return(R_NilValue);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a problem in my database - somehow changes sometimes are not being
Somehow, using linq I can't test it with this CUF field in the beginning:
somehow couldn't find this with a google search, but I feel like it has
Somehow, Tortoise against Subversion can't seem to figure out that the source is not
This question is perhaps somehow odd, but how can I speed up g++ compile
Maybe someone can explain this behavior. Sometimes I will do an SVN update, and
Dear Guys I am somehow newbie and sometimes guru , but at this moment
I sometimes get the following exception: COM object that has been separated from its
somehow I get never any results when I call: select * from table_1 t1
Does anyone else have this problem or is my Delphi cursed somehow? I'll have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.