Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the size of all objects in the current workspace in R? (not in WIndows)

In Windows only one can do memory.size() to get the total amount of memory eaten up by the (objects in the) current R session.

It's also possible to understand the size of an individual object with print( object.size( thing ), units='auto') which says how many megabytes/kilobytes that particular data-frame/table takes up.

But how to do the equivalent of print( object.size( ---workspace--- ))?

Looping for (thing in ls()) print( object.size( thing ), units='auto' ) prints the wrong output, such as:

64 bytes
72 bytes
88 bytes
88 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
72 bytes
88 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes

which is not what I meant.

like image 496
isomorphismes Avatar asked Feb 25 '14 18:02

isomorphismes


People also ask

How do I find the size of an object in R?

To find the object size in R, we can use object. size function. For example, if we have a data frame called df then the size of df can be found by using the command object. size(df).

What statement shows the objects in your workspace R?

The ls() code lists all of the objects in your workspace. The rm() code removes objects in your workspace. You can begin your code with the rm() function to clear all of the objects from your workspace to start with a clean environment.

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.

How do I read an R workspace file?

To access file click File and then load workspace. A dialog box will appear, browse to the folder where you saved the . RData file and click open.


1 Answers

To print the size of the whole workspace, you could try the following function:

workspace.size <- function() {
  ws <- sum(sapply(ls(envir=globalenv()), function(x)object.size(get(x))))
  class(ws) <- "object_size"
  ws
}

workspace.size()
# 35192 bytes
like image 106
sgibb Avatar answered Sep 23 '22 18:09

sgibb