Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Llist dataframe names in Global environment [duplicate]

I am trying to write a function that lists the names of dataframes in my global environment.

I can do this by using the code:

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

I need to convert this into a function that I can easily call upon.

like image 696
PJC Avatar asked Jan 24 '23 18:01

PJC


1 Answers

You have to be aware that ls() by default lists objects in the current environment. If you wrap your code in a function, this current environment is the internal function environment, which is empty at that point (we are at the first line of the function and nothing has been defined yet). Since you are interested in the global environment, you have to explicitly specify this with .GlobalEnv:

lsf <- function() {
  l<-ls(.GlobalEnv)
  l[sapply(l, function(x) is.data.frame(get(x, envir = .GlobalEnv)))]
}

lsf()
like image 149
Bas Avatar answered Jan 31 '23 14:01

Bas