The maximum number of objects in the workspace r

Is there a limit on the number (not size) of objects that the workspace R can contain? What is the maximum possible value of length(e) , where e is the medium?

( length(e) contains the number of variables in e , this is the same as length(ls(e)) .)

+6
source share
1 answer

The source code for ls() (in src/main/envir.c ) determines its return value using another function: return R_lsInternal(env, all); ;

  #2518 SEXP attribute_hidden do_ls(SEXP call, SEXP op, SEXP args, SEXP rho) #2519 { #2520 SEXP env; #2521 int all; #.... #2537 return R_lsInternal(env, all); #2538 } 

Return function return R_lsInternal(env, all); , accepts a list of environments and a boolean value indicating whether to get all the names and is defined on line 2452 envir.c . It calculates the length of the names of objects in the environment (s), preserving the length in the int type below ( k below), so the number of objects in the environment should be limited by the maximum size of this data type:

  #2542 SEXP R_lsInternal(SEXP env, Rboolean all) #2543 { #2544 int k; <==== INTEGER TYPE DEFINED HERE LIMITS NUMBER OF OBJECTS IN ENVIRONMENT #2545 SEXP ans; #2546 #2547 #2548 /* Step 1 : Compute the Vector Size */ #2549 k = 0; #2550 if (env == R_BaseEnv || env == R_BaseNamespace) #2551 k += BuiltinSize(all, 0); #... #2562 /* Step 2 : Allocate and Fill the Result */ #2563 PROTECT(ans = allocVector(STRSXP, k)); #... #2576 return ans; #2577 } 

However, this is for objects in the specified environment . I see no reason why you cannot specify sub-environements, each of which can have .Machine$integer.max objects in it! Thus, the restriction should be limited only by your machine memory. I would like someone to experience this though!

 # Example of assigning values in sub environments... e <- new.env() e$f <- new.env() # Environment `e` now has one object in, which is another environment... length ( ls( e ) ) # [1] 1 e$f$a <- 2 # Environment `f` now also has one object in, which is `a` length ( ls( e$f ) ) # [1] 1 

Note: if you calculate the length of objects using length(e) , where e is the environment, then the envlength function will be sent, since length is an internal common function for which there are several methods written for different types of objects, including one for environments as pointed out by @RichieCotton above and @hadley in the comments below.

+3
source

Source: https://habr.com/ru/post/975274/


All Articles