Hi folks, I am wondering if the following is possible: I want to control the arguments sent to a function by string variables. For example, instead of > heatmap.2( A, col=greenred(75) ) I would want to have something like: > heatmap.2 ( paste(A, "col=greenred(75)", sep=",") ) Is this possible to do that? Thanks, D.
On Thu, Feb 24, 2011 at 10:03 AM, Duke <duke.lists at gmx.com> wrote:> Hi folks, > > I am wondering if the following is possible: I want to control the arguments > sent to a function by string variables. For example, instead of >> heatmap.2( A, col=greenred(75) ) > I would want to have something like: >> heatmap.2 ( paste(A, "col=greenred(75)", sep=",") ) > > Is this possible to do that?Of course (this is R, after all :)). Here's one solution: fnc = "heatmap.2" arguments = "A, col=greenred(75)" eval(parse(text = paste(fnc, "(", arguments, ")"))) Example: f = function(a, b) { a+b } fnc= "f" arguments = "a = 2, b=3"; eval(parse(text = paste(fnc, "(", arguments, ")"))) [1] 5 Peter
On Feb 24, 2011, at 1:03 PM, Duke wrote:> Hi folks, > > I am wondering if the following is possible: I want to control the > arguments sent to a function by string variables. For example, > instead of > > heatmap.2( A, col=greenred(75) ) > I would want to have something like: > > heatmap.2 ( paste(A, "col=greenred(75)", sep=",") ) > > Is this possible to do that?It is possible (to do various things), but that would fail. You would need to pass a character argument to parse() and then eval() but probably not in the argument list. And there are possibly much cleaner ways of doing what you want to achieve. Can you describe "what you want to achieve" in more detail? An example that presents R objects is the accepted manner for posing such questions. If you plan to redefine heatmap.2, it might be as simple as: require(gplots) heatmap.2A.rg <- function(A, col, ...) {heatmap.2(x= A, col=greenred(75), ... )} -- David Winsemius, MD West Hartford, CT
On 2/24/11 1:21 PM, David Winsemius wrote:> > On Feb 24, 2011, at 1:03 PM, Duke wrote: > >> Hi folks, >> >> I am wondering if the following is possible: I want to control the >> arguments sent to a function by string variables. For example, >> instead of >> > heatmap.2( A, col=greenred(75) ) >> I would want to have something like: >> > heatmap.2 ( paste(A, "col=greenred(75)", sep=",") ) >> >> Is this possible to do that? > > It is possible (to do various things), but that would fail. You would > need to pass a character argument to parse() and then eval() but > probably not in the argument list. And there are possibly much cleaner > ways of doing what you want to achieve. Can you describe "what you > want to achieve" in more detail? An example that presents R objects is > the accepted manner for posing such questions.What I want to do is to control the options sent to heatmap function based on the argument input, and I dont want to have a lot of heatmap.2 commands such as if () heatmap.2(...) else if () heatmap.2(...) else if () heatmap.2(...) ... If I can control the *string* argument, then I can simply reducing the above commands to a much shorter and nicer code. Solution given by Peter and you using parse() and eval() was exactly what I asked for. Thanks so much to both of you :). D.