我想在使用attach()之后识别绘图上的数据,但是在退出函数之前我有分离的问题:
f = function(x, y, data) {
attach(data)
plot(x, y)
ids = identify(x, y)
detach(data)
return ids
}
返回值:
<environment: 0xf785ed8>
attr(,"name")
[1] "data"
> class(i1)
[1] "environment"
> str(i1)
<environment: 0xf785ed8>
- attr(*, "name")= chr "data"
如何在函数中使用附加和标识并返回附加对象的ID?
答案 0 :(得分:2)
您正在尝试进行非标准评估。这总是有点棘手。我可以推荐一下这样的东西:
f <- function(x, y, data) {
dx <- deparse(substitute(x))
dy <- deparse(substitute(y))
plot(reformulate(dx,response=dy),data)
ids <- identify(data[,dx], data[,dy])
return(ids)
}
f(Population,Income,state.x77)
或
f2 <- function(x, y, data) {
x <- eval(substitute(x),envir=as.data.frame(data))
y <- eval(substitute(y),envir=as.data.frame(data))
plot(x,y)
ids <- identify(x,y)
return(ids)
}
f2(Population,Income,state.x77)
您可能需要查看Hadley Wickham's notes on non-standard evaluation以获取更多信息......