如何获取当前环境中可用的数据帧名称向量?我试过了:获取可用数据帧列表
sapply(ls(), is.data.frame)
但是这个失败是因为ls
返回字符串的向量。我打算将此列表用作Shiny应用程序中动态选择的输入。
如何获取当前环境中可用的数据帧名称向量?我试过了:获取可用数据帧列表
sapply(ls(), is.data.frame)
但是这个失败是因为ls
返回字符串的向量。我打算将此列表用作Shiny应用程序中动态选择的输入。
您可以在环境中使用eapply
遍历对象:
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"
你需要get
,试试这个:
x <- sapply(sapply(ls(), get), is.data.frame)
names(x)[(x==TRUE)]
我想你问这些数据帧而不是数据帧本身的实际名称?你可以这样做:
l <- ls()
l[sapply(l, function(x) is.data.frame(get(x)))]
get()
将返回给定角色名称的对象的值。
Filter(function(x) is.data.frame(get(x)), ls())
我使用基于改良lsos()
功能基本上还是在做同样的事情
整洁的方式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
我使用基于[这个问题]的修改的'lsos()'函数(http://stackoverflow.com/questions/1358003/tricks-to-manage-the-available-memory-in-an-r-session ) –