Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum number of objects in r workspace

Tags:

r

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

(length(e) contains the number of variable in e; it is the same as length(ls(e)).)

like image 870
Hugh Avatar asked Sep 15 '14 05:09

Hugh


People also ask

How many objects are there in R?

There are 6 types of objects in R Programming. They include vector, list, matrix, array, factor, and data frame.

What takes an object out of the workspace in R?

Actually, there are two different functions that can be used for clearing specific data objects from the R workspace: rm() and remove().

Where are objects stored in R?

At the end of a session the objects in the global environment are usually kept in a single binary file in the working directory called . RData. When a new R session begins with this as the initial working directory, the objects are loaded back into memory.

How do I list all objects in R?

ls() function in R Language is used to list the names of all the objects that are present in the working directory.


1 Answers

The source code for ls() (in src/main/envir.c) defines it's 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 }

The return function return R_lsInternal(env, all);, takes a list of environments and a boolean indicating whether to get all names and is defined at line 2452 of the envir.c. It computes the length of names of objects in an environment(s) storing the length in an int datatype (k below), therefore the number of objects in an environment must be bounded by the maximum size of this datatype:

 #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 the objects in a specified environment. I see no reason that you can't specify sub-environements, each of which could have .Machine$integer.max objects in it! So the limit should only be bounded by your machine memory. I'd love someone to test 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 compute the length of objects using length(e) where e is an environment then the function envlength will be dispatched as length is an internal generic for which there are several methods written for various object types, including one for environments, as noted by @RichieCotton above and @hadley in the comments below.

like image 116
Simon O'Hanlon Avatar answered Sep 27 '22 20:09

Simon O'Hanlon