Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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

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

like image 463
Maiasaura Avatar asked Sep 07 '10 18:09

Maiasaura


People also ask

Can you pass a reference to a function from a function?

And in fact the answer is “You can!” Our call to f passes a reference to the derived class to a function that takes a reference to the base class. This is perfectly fine. When people ask this question, they are typically wondering about passing a reference to the base class by reference . There is a double indirection here.

What happens when you pass an array to a function?

The passing an array to a function is quite different from that a simple variable passing. we know that in the case of when a simple variable pass to a function, The called function create the copy of the simple variable and works on it.so by the called function any changes in the variable do not effect to the original variable.

What happens when you modify a parameter in a function?

When the f function modifies its formal parameter b , it’s actually modifying your variable d . What’s worse, it’s putting a Base in it! When f returns, your variable d , which is declared as being a reference to a Derived is actually a reference to the base class Base .

What happens when you put a base in a function parameter?

When the f function modifies its formal parameter b , it’s actually modifying your variable d . What’s worse, it’s putting a Base in it! When f returns, your variable d , which is declared as being a reference to a Derived is actually a reference to the base class Base . At this point everything falls apart.


1 Answers

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.

like image 185
Joris Meys Avatar answered Nov 14 '22 08:11

Joris Meys