You can use as.list
on an environment and then use sapply
with is.whatever
like this:
list_all_x <- function(is.x = is.data.frame, env = .GlobalEnv){
env <- as.list(env)
names(env)[sapply(env, is.x)]
}
# or related to ThomasIsCoding's great answer
list_all_x <- function(is.x = is.data.frame, env = .GlobalEnv)
names(Filter(is.x, as.list(env)))
# check the function
d1 <- numeric()
d2 <- data.frame()
d3 <- data.frame()
list_all_x()
#R> [1] "d2" "d3"
list_all_x(is.x = is.numeric)
#R> [1] "d1"
You can use the above if you want to apply the function to another environment using the env
argument or look for another type by changing the is.x
argument as shown above.