Skip to content

functions and strings

6 messages · robin hankin, Romain Francois, Dimitris Rizopoulos

#
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)"

?


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

Here what i'll do :

f <- function(x){
  sprintf("1 + sin(cos(%s)) + exp(%s^2)", x, x)
}

Cheers,

Romai
#
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, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Robin Hankin" <r.hankin at noc.soton.ac.uk>
To: <R-help at r-project.org>
Sent: Wednesday, September 13, 2006 9:10 AM
Subject: [R] functions and strings
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
#
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:

            
--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743
#
yes you're right, maybe this is better
[1] "sin(xyz) + exp(xyz)"


Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Robin Hankin" <r.hankin at noc.soton.ac.uk>
To: "Dimitris Rizopoulos" <dimitris.rizopoulos at med.kuleuven.be>
Cc: "Robin Hankin" <r.hankin at noc.soton.ac.uk>; <r-help at r-project.org>
Sent: Wednesday, September 13, 2006 9:54 AM
Subject: Re: [R] functions and strings
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
#
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:

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