Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of available data frames

Tags:

r

shiny

How do I get vector of data frame names available in current environment? I've tried:

sapply(ls(), is.data.frame)

But this fails because ls returns vector of strings. I'm planning to use this list as a input for dynamic selection in Shiny app.

like image 752
Tomas Greif Avatar asked Oct 30 '13 14:10

Tomas Greif


People also ask

Can you have a list of data frames?

A Data frame is simply a List of a specified class called “data. frame”, but the components of the list must be vectors (numeric, character, logical), factors, matrices (numeric), lists, or even other data frames.

How do you access data frames in R?

Accessing the columns of a data frame The column items in a data frame in R can be accessed using: Single brackets [] , which would display them as a column. Double brackets [[]] , which would display them as a list. Dollar symbol $ , which would display them as a list.

How do I create an empty DataFrame in R?

One simple approach to creating an empty DataFrame in the R programming language is by using data. frame() method without any params. This creates an R DataFrame without rows and columns (0 rows and 0 columns).


3 Answers

You can use eapply to loop through the objects in an environment:

x <- 1:10
y <- mtcars
eapply(.GlobalEnv,is.data.frame)
$x
[1] FALSE

$y
[1] TRUE

names(which(unlist(eapply(.GlobalEnv,is.data.frame))))
[1] "y"
like image 122
James Avatar answered Oct 14 '22 17:10

James


I think you're asking for the actual names of these data frames rather than the data frames themselves? You can do:

l <- ls()
l[sapply(l, function(x) is.data.frame(get(x)))]

get() will return the value of an object given a character name.


Tidier way of doing basically the same thing:

Filter(function(x) is.data.frame(get(x)), ls())
like image 39
Ciarán Tobin Avatar answered Oct 14 '22 17:10

Ciarán Tobin


You need get, try this:

x <- sapply(sapply(ls(), get), is.data.frame)
names(x)[(x==TRUE)] 
like image 30
Jilber Urbina Avatar answered Oct 14 '22 18:10

Jilber Urbina