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.
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.
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"
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())
You need get
, try this:
x <- sapply(sapply(ls(), get), is.data.frame)
names(x)[(x==TRUE)]
I use a modified lsos()
function based on this question
library(devtools)
source_url("https://raw.github.com/rsaporta/pubR/gitbranch/memoryFunctions.R")
## only show data.frames of at least ~1KB
lsos(t="data.frame")
## show data.frames of any size
lsos(t="data.frame", b=1)
### OUTPUT
KB Type Rows Columns
anotherDF 5 data.frame 50 4
df 0.8 data.frame 5 2