Re: [R] functions and strings

From: Robin Hankin <>
Date: Wed 13 Sep 2006 - 09:09:27 GMT

Hello everyone

I know it looks like I'm making heavy weather of this, but I don't think I communicated my problem properly. I really appreciate you guys' help here.

I am writing a wrapper for a mathematical library to which I want to send character strings that it can execute, and then pass the answer back to R.

Now, I want a user to be able to type _any_ function and _any_ string. For example:

f <- function(i){sin(i) + cos(sin(i^2))} string <- "tti"

and then I want a function do() such that do(f,string) will return

"sin(tti) + cos(sin(tti^2))"

without worrying about whether f()'s arguments include or do not include a particular letter, and without insisting that "i" always appears as "(i)" .

Although thinking about it, it's not
actually that bad to require the user to use some otherwise rare sequence of letters, say "XxX" as
an argument, and then Dmitris's first method would work.

Having said that, this is not an ideal solution and it would be nicer to have some method that could detect what the argument to f() is, where it is in the body, and substitute those occurences for "string".

I want a method that is perfectly general; I posted my example of abcd...z(), not to be annoying and pedantic but to illustrate that a simple gsub approach wouldn't work: one has to know in advance which letters can and cannot be used, and this information isn't available.

I don't have a function so named (yet ;-).

best wishes


> Hi Dmitris, Thierry,
> I'm getting there but it's still not quite right if f() includes
> something like x^2:
> f <- function(x){exp(x^2)}
> gsub("(x)", "(xyz)", deparse(body(f))[2], fixed = TRUE)
> [1] " x^2"
> [I don't care about the spaces]
> also,
> I can't quite see how to implement Thierry's suggestion about
> changing the letter "x" into a letter that does not occur in f(),
> because of the
> following example:
> f <- function(x){abcdefghijklmnopqrstuvwxyz(x^2)}
> On 13 Sep 2006, at 09:08, Dimitris Rizopoulos wrote:
>> yes you're right, maybe this is better
>>> f <- function(x){sin(x)+exp(x)}
>>> strng <- gsub("(x)", "(xyz)", deparse(body(f))[2], fixed = TRUE)
>>> sub('^[[:space:]]+', '', strng)
>> [1] "sin(xyz) + exp(xyz)"
>> Best,
>> Dimitris

Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Wed Sep 13 19:12:19 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 13 Sep 2006 - 10:30:04 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.