[R] How does once import a function from an imported script?

David Winsemius dwinsemius at comcast.net
Thu Nov 10 14:55:14 CET 2011


On Nov 9, 2011, at 9:11 PM, Aimee Jones wrote:

> Hi all,
> I am trying to import some functions into a script and am having
> difficulty in doing so. I am able to import a series of functions from
> a .tex file into my script, and call on each function by column name,
> however R reads them as data rather than as functions and I am
> struggling with the syntax to make R read them as a function instead.
>
> Below is a small set of the things I have tried:
>
> parms["B1"]
>
> returns the character string {2*dnorm(T1(t),mean=32.5,sd=7)} . This is
> from the imported script "parms".
>
> when I write B1<-function(T1,t) (parms["B1"]) and then enter B1 to see
> what the workspace returns, I get
>
> function(T1,t) (parms["B1"])
>
> I am trying to get B1 to return as function(T1,t)
> {2*dnorm(T1(t),mean=32.5,sd=7)}, so that I can plot B1(T1,t).

You need to use the proper functions to transmute character values to  
language elements. The body of functions are "expressions", a  
particular R class.

Here's another failed attempt:
 > body(B1) <- "{2*dnorm(T1(t),mean=32.5,sd=7)}"
 > B1
function (T1, t)
"{2*dnorm(T1(t),mean=32.5,sd=7)}"

I knew that the quotes around the expression were NOT going to work.  
After successfully creating a function in the manner illustrated below  
I realized that you had not indicated what T1 was (and it appears to  
be a function), so it couldn't be tested. So I made a slightly  
simplified version:

 > B1<-function(t) (parms["B1"])   # starting with something like  
where you are ...
 > body(B1) <- expression({2*dnorm(t, mean=32.5, sd=7)})
 > B1(32.5)
[1] 0.1139835

This might be a more clean method if you were starting all over:

 > B1 <- function(T1,t) { }
 > body(B1) <- expression({2*dnorm(T1(t), mean=32.5, sd=7)})
 > B1
function (T1, t)
{
     2 * dnorm(T1(t), mean = 32.5, sd = 7)
}
 >

>
>
> Is there any way to do this?
>
> Thank you for your assistance and patience,
> yours sincerely,
>
> Aimee Jones
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list