Aimee Jones
2011-Nov-10 02:11 UTC
[R] How does once import a function from an imported script?
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). Is there any way to do this? Thank you for your assistance and patience, yours sincerely, Aimee Jones
Jeff Newmiller
2011-Nov-10 07:39 UTC
[R] How does once import a function from an imported script?
TeX is not R. You need to (manually) extract whatever R syntax is in the TeX file into an R file. Then you can use the source function to load those definitions into your working environment. I highly recommend reading the posting guide as well. Including the R statements you run and a sample of the data you are working with to arrive at the point where you are stuck in your email is rather important to getting the most useful assistance on this mailing list. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Aimee Jones <alj27 at hoyamail.georgetown.edu> 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). > > >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
2011-Nov-10 13:55 UTC
[R] How does once import a function from an imported script?
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