Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of Defined Variables in R

Tags:

r

I'm using R in linux, command line only.

Coming back to a project after some time, I have forgotten the variable names I used, and the R command history doesn't contain them.

I seem to remember there being a command which lists all user defined variables, but don't remember what it is, and cannot find it on the web.

How can I list all the user defined variables in R?

like image 903
John Doucette Avatar asked Mar 16 '12 19:03

John Doucette


People also ask

How do I get a list of variables in R?

You can use ls() to list all variables that are created in the environment. Use ls() to display all variables.

What does list () do in R?

The list() function in R is used to create a list of elements of different types. A list can contain numeric, string, or vector elements.

How do I list all objects in memory in R?

Listing Object in Memory One can list all the objects in his working directory by using objects() or ls() function. objects() or ls() function can be used to get a vector of character strings of the names of all objects in the environment.


2 Answers

ls()

From the help page:

 ‘ls’ and ‘objects’ return a vector of character strings giving the  names of the objects in the specified environment.  When invoked  with no argument at the top level prompt, ‘ls’ shows what data  sets and functions a user has defined.  When invoked with no  argument inside a function, ‘ls’ returns the names of the  functions local variables.  This is useful in conjunction with  ‘browser’. 

Edit: I should note that to list ALL variables you would need to use

ls(all.names = TRUE) 

otherwise variables that begin with a dot won't show up in the listing.

like image 75
Dason Avatar answered Sep 25 '22 22:09

Dason


You may want to check out this link:

Tricks to manage the available memory in an R session

It has a great function to show objects along with their memory usage. It's part of my start-up script for R.

# Written by Dirk Eddelbuettel found here: https://stackoverflow.com/questions/1358003/tricks-to-manage-the-available-memory-in-an-r-session  # improved list of objects  .ls.objects <- function (pos = 1, pattern, order.by,                         decreasing=FALSE, head=FALSE, n=5) {     napply <- function(names, fn) sapply(names, function(x)                                          fn(get(x, pos = pos)))     names <- ls(pos = pos, pattern = pattern)     obj.class <- napply(names, function(x) as.character(class(x))[1])     obj.mode <- napply(names, mode)     obj.type <- ifelse(is.na(obj.class), obj.mode, obj.class)     obj.size <- napply(names, object.size)     obj.dim <- t(napply(names, function(x)                         as.numeric(dim(x))[1:2]))     vec <- is.na(obj.dim)[, 1] & (obj.type != "function")     obj.dim[vec, 1] <- napply(names, length)[vec]     out <- data.frame(obj.type, obj.size, obj.dim)     names(out) <- c("Type", "Size", "Rows", "Columns")     if (!missing(order.by))         out <- out[order(out[[order.by]], decreasing=decreasing), ]     if (head)         out <- head(out, n)     out } # shorthand lsos <- function(..., n=10) {     .ls.objects(..., order.by="Size", decreasing=TRUE, head=TRUE, n=n) } 
like image 23
screechOwl Avatar answered Sep 21 '22 22:09

screechOwl