Why can't I pass a dataset to a function?

前端 未结 1 620
长情又很酷
长情又很酷 2021-01-02 17:26

I\'m using the package glmulti to fit models to several datasets. Everything works if I fit one dataset at a time.

So for example:

output <- glmul         


        
相关标签:
1条回答
  • 2021-01-02 18:03

    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...)

    It can be shown by adding quotes around the 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.

    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.

    0 讨论(0)
提交回复
热议问题