I'm using the package glmulti to fit models to several datasets. Everything works if I fit one dataset at a time.
我正在使用软件包glmultito fit模型到几个数据集。如果我一次只匹配一个数据集,那么一切都是可行的。
So for example:
举个例子:
output <- glmulti(y~x1+x2,data=dat,fitfunction=lm)
works just fine.
工作得很好。
However, if I create a wrapper function like so:
但是,如果我创建一个包装器函数:
analyze <- function(dat)
{
out<- glmulti(y~x1+x2,data=dat,fitfunction=lm)
return (out)
}
simply doesn't work. The error I get is
完全没有作用。我得到的错误是。
error in evaluating the argument 'data' in selecting a method for function 'glmulti'
Unless there is a data frame named dat, it doesn't work. If I use results=lapply(list_of_datasets, analyze)
, it doesn't work. So what gives? Without my said wrapper, I can't lapply a list of datasets through this function. If anyone has thoughts or ideas on why this is happening or how I can get around it, that would be great.
除非有一个名为dat的数据帧,否则它不起作用。如果我使用结果=lapply(list_of_datasets,分析),它就不起作用了。给什么?如果没有我所说的包装器,我就不能通过这个函数来应用一个数据集列表。如果有人对为什么会发生这样的事情或者我能如何解决这个问题有想法或想法,那就太好了。
example 2:
示例2:
dat=list_of_data[[1]]
analyze(dat)
works fine. So in a sense it is ignoring the argument and just literally looking for a data frame named dat. It behaves the same no matter what I call it.
工作很好。所以从某种意义上说,它忽略了这个参数,只是在寻找一个名为dat的数据帧。无论我怎么称呼它,它的行为都是一样的。
1 个解决方案
#1
8
I guess this is -yet another- problem due to the definition of environments in the parse tree of S4 methods (one of the resons why I am not a big fan of S4...)
我想这是另一个问题,因为S4方法解析树中环境的定义(我不是S4的超级粉丝的原因之一)。
It can be shown by adding quotes around the dat :
可以通过在dat周围添加引用来显示:
> analyze <- function(dat)
+ {
+ out<- glmulti(y~x1+x2,data="dat",fitfunction=lm)
+ return (out)
+ }
> analyze(test)
Initialization...
Error in eval(predvars, data, env) : invalid 'envir' argument
You should in the first place send this information to the maintainers of the package, as they know how they deal with the environments internally. They'll have to adapt the functions.
您应该首先将这些信息发送给包的维护者,因为他们知道如何在内部处理环境。他们必须适应这些功能。
A -very dirty- workaround for yourself, is to put "dat" in the global environment and delete it afterwards.
一个非常肮脏的工作,就是把“dat”放在全局环境中,然后删除它。
analyze <- function(dat)
{
assign("dat",dat,envir=.GlobalEnv) # put the dat in the global env
out<- glmulti(y~x1+x2,data=dat,fitfunction=lm)
remove(dat,envir=.GlobalEnv) # delete dat again from global env
return (out)
}
EDIT: Just for clarity, this is really about the worst solution possible, but I couldn't manage to find anything better. If somebody else gives you a solution where you don't have to touch your global environment, by all means use that one.
编辑:为了清晰起见,这实际上是最糟糕的解决方案,但我找不到更好的解决方案。如果别人给你一个解决方案,你不需要去接触你的全球环境,那就用它吧。
#1
8
I guess this is -yet another- problem due to the definition of environments in the parse tree of S4 methods (one of the resons why I am not a big fan of S4...)
我想这是另一个问题,因为S4方法解析树中环境的定义(我不是S4的超级粉丝的原因之一)。
It can be shown by adding quotes around the dat :
可以通过在dat周围添加引用来显示:
> analyze <- function(dat)
+ {
+ out<- glmulti(y~x1+x2,data="dat",fitfunction=lm)
+ return (out)
+ }
> analyze(test)
Initialization...
Error in eval(predvars, data, env) : invalid 'envir' argument
You should in the first place send this information to the maintainers of the package, as they know how they deal with the environments internally. They'll have to adapt the functions.
您应该首先将这些信息发送给包的维护者,因为他们知道如何在内部处理环境。他们必须适应这些功能。
A -very dirty- workaround for yourself, is to put "dat" in the global environment and delete it afterwards.
一个非常肮脏的工作,就是把“dat”放在全局环境中,然后删除它。
analyze <- function(dat)
{
assign("dat",dat,envir=.GlobalEnv) # put the dat in the global env
out<- glmulti(y~x1+x2,data=dat,fitfunction=lm)
remove(dat,envir=.GlobalEnv) # delete dat again from global env
return (out)
}
EDIT: Just for clarity, this is really about the worst solution possible, but I couldn't manage to find anything better. If somebody else gives you a solution where you don't have to touch your global environment, by all means use that one.
编辑:为了清晰起见,这实际上是最糟糕的解决方案,但我找不到更好的解决方案。如果别人给你一个解决方案,你不需要去接触你的全球环境,那就用它吧。