[R] passing list of parameters to a function

25 views
Skip to first unread message

Sinead English

unread,
Oct 22, 2012, 10:35:26 AM10/22/12
to egi-progra...@googlegroups.com
Hi all

I think this is a really simple problem but I am struggling to find the best solution. I have a list of parameters to be used across a range of functions, and I'd like to write these functions without repeating that general list (with each function including additional parameters). For example:

# list of parameters
params <- list(x=1,y=2,z=3)

# new function on variables a and b, using constants x, y and z from the params list:
newfun <- function(a,b)
    {
        return(a*x + y*b + z)
    }
   
# apply new function to input values of a and b, using constant values for x, y and z
newfun(a=10, b=100, params) # error
do.call("newfun", c(list(a=10,b=100),params)) # error
with(params, newfun(a=10, b=100))

I think there's something wrong either with how I'm describing the new function (newfun) or calling the function. After reading this
http://stackoverflow.com/questions/9129673/passing-list-of-named-parameters-to-function
I thought do.call might be the solution, but I get an error message.
Any suggestions?

Thanks! Sinead



Cam Allen

unread,
Oct 22, 2012, 10:48:37 AM10/22/12
to egi-progra...@googlegroups.com
Hey Sinead,

From what you describe it seems there is nothing wrong with your idea. 

It looks like a scope issue in R, see point 3 here, http://cran.r-project.org/doc/contrib/Fox-Companion/appendix-scope.pdf

I have a couple of questions: 
  1. Are the params list values const or will they change during execution? 
  2. Do you really need global scope (params available in other methods?
    1. You can pass the params object around to avoid polluting the global namespace.

I guess this answer doesn't solve your issue but by the sound it's really just a scoping issue (but i maybe wrong).

cheers,
Cam

Sinead English

unread,
Oct 22, 2012, 10:57:17 AM10/22/12
to egi-progra...@googlegroups.com
Hi Cam

Thanks - I need to think carefully about what I want to be global and local to functions it seems...

I have a couple of questions: 
  1. Are the params list values const or will they change during execution? 
At the moment they are constant but ultimately I'll be wanting to vary (some of) them. 

  1. Do you really need global scope (params available in other methods?
    1. You can pass the params object around to avoid polluting the global namespace.
No I don't think it really needs to be global if I can pass the object/list around. I'm just not sure how to do the latter efficiently.

This works:
params <- list(x=1,y=2,z=3)

newfun <- function(a,b, params)
    {
        return(a*params$x + b*params$y + params$z)
    }
   

newfun(a=10, b=100, params)

... but it doesn't seem very elegant and I was wanting to avoid typing params$ too many times in the functions. Another option is attach(params) but I hate using attach().

Cheers, Sinead

 

I guess this answer doesn't solve your issue but by the sound it's really just a scoping issue (but i maybe wrong).

cheers,
Cam


On Monday, October 22, 2012 3:36:07 PM UTC+1, sinead wrote:
Hi all

I think this is a really simple problem but I am struggling to find the best solution. I have a list of parameters to be used across a range of functions, and I'd like to write these functions without repeating that general list (with each function including additional parameters). For example:

# list of parameters
params <- list(x=1,y=2,z=3)

# new function on variables a and b, using constants x, y and z from the params list:
newfun <- function(a,b)
    {
        return(a*x + y*b + z)
    }
   
# apply new function to input values of a and b, using constant values for x, y and z
newfun(a=10, b=100, params) # error
do.call("newfun", c(list(a=10,b=100),params)) # error
with(params, newfun(a=10, b=100))

I think there's something wrong either with how I'm describing the new function (newfun) or calling the function. After reading this
http://stackoverflow.com/questions/9129673/passing-list-of-named-parameters-to-function
I thought do.call might be the solution, but I get an error message.
Any suggestions?

Thanks! Sinead



--
You received this message because you are subscribed to the Google Groups "EGI programming and R" group.
To view this discussion on the web visit https://groups.google.com/d/msg/egi-programming-and-r/-/LiN9j7SJWAkJ.
To post to this group, send email to egi-progra...@googlegroups.com.
To unsubscribe from this group, send email to egi-programming-...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/egi-programming-and-r?hl=en.

Cam Allen

unread,
Oct 22, 2012, 12:09:34 PM10/22/12
to egi-progra...@googlegroups.com
Hi Sinead,

This is where object oriented programming comes in and you start encapsulating functionality and data into objects for reuse / access. 

Not much you can do apart from reference global variables in functions (considered bad practice as incorrect use of the data in a function can cause problems to the global space of program). Passing local parameters is a better way of dealing with the problem because they values are normally copied and passed so changes in the locally scoped function don't cascade out to the global values. 

In summary you are seeing the encapsulation problems which create object oriented software design. Happy to chat about OO design if you want.

Let me know if any of the above does not make sense.

Cheers,
cam
Reply all
Reply to author
Forward
0 new messages