如何找到所有附加的数据框? [英] How to find all attached data frames?

查看:23
本文介绍了如何找到所有附加的数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 R studio 中附加 data.frame 时,我收到以下消息:

When I attach a data.frame in R studio, I get this message:

以下对象被屏蔽了......

The following objects are masked from......

我忘记分离 data.frame

I forgot to detach the data.frame

data<-read.table(file.choose(),header=TRUE)
View(data)
attach(data) 
## The following objects are masked from vih (pos = 3):
## edad, edadg, id, numpares, numparg, sifprev, udvp, vih 
## The following objects are masked from vih (pos = 4):
## edad, edadg, id, numpares, numparg, sifprev, udvp, vihhere

有没有办法知道附加了哪些 data.frames?

Is there a way of knowing which data.frames are attached?

有没有办法用一个命令或函数分离所有的 data.frames?

Is there a way of detaching ALL the data.frames with one command or function?

推荐答案

首先,我建议你停止使用 attach().这确实是一个糟糕的做法,因为几乎总是有更好的替代方案(例如 with()data= 参数)

First, I suggest you stop using attach(). It's really a bad practice since there are almost always better alternatives (with() or data= parameters for example)

但是你可以用

search()

如果您假设所有的 data.frame 名称都不以."开头.并且不包含:",您可以使用

If you assume all your data.frame names don't start with a "." and don't contain a ":", you could detach them all with

detach_dfs1 <- function() {
    dfs <- grep("^\.|.*[:].*|Autoloads", search(), invert=T)
    if(length(dfs)) invisible(sapply(dfs, function(x) detach(pos=x)))
}

或者如果你假设 data.frames 在全局环境中,你可以这样做

or if you assume that the data.frames are in the global environment, you could do

detach_dfs2 <- function() {
    dfs <- Filter(function(x) exists(x) && is.data.frame(get(x)), search())
    if(length(dfs)) invisible(sapply(dfs, function(x) detach(x, character.only=T)))
}

这篇关于如何找到所有附加的数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆