Re: [R] functions and strings

2006-09-13 Thread Romain Francois
Robin Hankin wrote: Hi If string - xyz f - function(x){1 + sin(cos(x)) + exp(x^2)} How do I manipulate string and f() to give the string 1 + sin(cos(xyz)) + exp(xyz^2) ? Hi, Here what i'll do : f - function(x){ sprintf(1 + sin(cos(%s)) + exp(%s^2), x, x) } Cheers, Romai --

Re: [R] functions and strings

2006-09-13 Thread Dimitris Rizopoulos
one approach could be the following strng - gsub(x, xyz, deparse(body(f))[2]) sub('^[[:space:]]+', '', strng) I hope it helps. Best, Dimitris Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven,

Re: [R] functions and strings

2006-09-13 Thread Robin Hankin
Hi Dmitris thanks for this but it's not quite right: f - function(x){sin(x)+exp(x)} strng - gsub(x, xyz, deparse(body(f))[2]) sub('^[[:space:]]+', '', strng) [1] sin(xyz) + exyzp(xyz) and I would want sin(xyz) + exp(xyz) On 13 Sep 2006, at 08:45, Dimitris Rizopoulos wrote: strng

Re: [R] functions and strings

2006-09-13 Thread Dimitris Rizopoulos
Rizopoulos [EMAIL PROTECTED] Cc: Robin Hankin [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Wednesday, September 13, 2006 9:54 AM Subject: Re: [R] functions and strings Hi Dmitris thanks for this but it's not quite right: f - function(x){sin(x)+exp(x)} strng - gsub(x, xyz, deparse(body(f))[2

Re: [R] functions and strings

2006-09-13 Thread Robin Hankin
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

Re: [R] functions and strings

2006-09-13 Thread Robin Hankin
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

Re: [R] functions and strings

2006-09-13 Thread Rich FitzJohn
Hi, Perhaps try this (based on 'bquote'): rewrite.expression - function(expr, to, dep) { f - function(expr) { if ( length(expr) == 1 ) if ( expr == as.name(dep) ) as.name(to) else expr else as.call(lapply(expr, f)) } f(expr) } rewrite -

Re: [R] functions and strings

2006-09-13 Thread Robin Hankin
Rich that is swet and does exactly what I want. Thank you very much. best wishes rksh On 13 Sep 2006, at 10:54, Rich FitzJohn wrote: Hi, Perhaps try this (based on 'bquote'): rewrite.expression - function(expr, to, dep) { f - function(expr) { if ( length(expr) == 1

Re: [R] functions and strings

2006-09-13 Thread Peter Dalgaard
Robin Hankin [EMAIL PROTECTED] writes: Rich that is swet and does exactly what I want. Thank you very much. I just wonder whatever substitute() did to get ignored like that? substitute(1 + sin(cos(x)) + exp(x^2), list(x=quote(xyz))) 1 + sin(cos(xyz)) + exp(xyz^2)