简体   繁体   English

检索 rData 文件中所有 data.frame class 对象的路径

[英]Retrieving the path to all data.frame class objects in rData files

I have multiple.rData files whose top level Global Environment variables are a mix of data.frames, lists, deeply nested lists.我有多个 .rData 文件,其顶级全局环境变量是 data.frames、列表、深度嵌套列表的混合。 I know that many of the nested lists have within them data.frame types, but I'm having trouble retrieving the path to them.我知道许多嵌套列表中都有 data.frame 类型,但我无法检索到它们的路径。

I had a faced a similar problem before with another type of class using the following code在使用以下代码使用另一种类型的 class 之前,我遇到过类似的问题

names(rapply(mget(ls(.GlobalEnv), envir=.GlobalEnv), length, classes="fluor.spectral.data", how="unlist"))

and while not the most elegant solution, it achieved what I needed and quickly.虽然不是最优雅的解决方案,但它很快就达到了我的需要。 returning names like "Fluor.Spec.WA.M12.SC.13" which then allows me to manipulate the object after formating the '.'返回诸如“Fluor.Spec.WA.M12.SC.13”之类的名称,然后允许我在格式化“。”后操纵 object into '$'.进入'$'。

Can someone help me retrieve the path to all data.frame class types, nested or otherwise in highly variable.rData files?有人可以帮我检索所有 data.frame class 类型的路径,嵌套或以其他方式在高度可变的 rData 文件中吗? Thanks in advance提前致谢

If you want to return all data.frames loaded in the global environment, either present as individual object or as element of a nested list, use rrapply in the rrapply -package (extension of base rrapply ).如果要返回全局环境中加载的所有 data.frame,无论是作为单独的 object 还是作为嵌套列表的元素,请在rrapply -package 中使用rrapply (基础rrapply的扩展)。

library(rrapply)

w <- data.frame(1)
x <- list(1, 2, 3)
y <- 5
z <- list(1, 2, list(1, df = data.frame(a = 1, b = 2)))

rrapply(as.list(.GlobalEnv), classes = "data.frame", how = "flatten")
#> $w
#>   X1
#> 1  1
#> 
#> $df
#>   a b
#> 1 1 2

Setting classes = "data.frame" avoids recursion into data.frame columns (as base rapply would do), and how = "flatten" will return the collected data.frames as a flattened list.设置classes = "data.frame"避免递归到 data.frame 列(就像基本rapply所做的那样),并且how = "flatten"将收集的 data.frames 作为扁平列表返回。

NB: If you want to return the complete object paths to the found data.frames, set how = "prune" instead of how ="flatten" :注意:如果要将完整的 object 路径返回到找到的 data.frames,请设置how = "prune"而不是how ="flatten"

rrapply(as.list(.GlobalEnv), classes = "data.frame", how = "prune")
#> $w
#>   X1
#> 1  1
#> 
#> $z
#> $z[[1]]
#> $z[[1]]$df
#>   a b
#> 1 1 2

Edit: In order to also return data.frames present in slots of some S4-class, a possible way to extend the above call would be:编辑:为了还返回存在于某些 S4 类插槽中的 data.frames,扩展上述调用的一种可能方法是:

## define S4-class with a data.frame in "df" slot
userClass <- setClass("user", slots = c(df = "data.frame"))
v <- userClass(df = data.frame(user = 1))

rrapply(as.list(.GlobalEnv), 
                   classes = c("data.frame", "user"), 
                   f = function(x) {
                     if(class(x) == "user") {
                       slot(x, "df")
                     } else {
                       x
                     }
                   },
                   how = "flatten")
#> $v
#>   user
#> 1    1
#> 
#> $w
#>   X1
#> 1  1
#> 
#> $df
#>   a b
#> 1 1 2

In this case, classes = c("data.frame", "user") will check for data.frames and S4-objects of class "user" .在这种情况下, classes = c("data.frame", "user")将检查 class "user"的 data.frames 和 S4 对象。 The f function applied to the object, returns the object itself if it is a data.frame or the "df" slot if it is an S4-object. f function 应用于 object,如果它是一个 data.frame,则返回 object 本身,如果它是一个 S4 对象,则返回"df"槽。

Note that this code assumes that the S4-class name is known as well as the slot(s) which contain the data.frame objects.请注意,此代码假定已知 S4 类名称以及包含 data.frame 对象的插槽。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM