[R] [R-devel] Source Code for function

Golden, Shelby Go|denS @end|ng |rom NJHe@|th@org
Tue Sep 10 20:13:47 CEST 2019


Wow, this is great! Thank you, Richard, for going so in-depth with this explanation.

My ultimate goal is to create a vector that stores logical evaluations to be used later in a function that subsets data. I’ve coded multiple projects, now, that require this kind of sub-setting across multiple unique logical evaluations. So, streamlining the in-put and the out-put is ideal to minimize error and generalize my code.

I will look further into this information to see if it can help me get around using parse(text = )), which, to my understanding, is not a good coding method (https://stackoverflow.com/questions/1743698/evaluate-expression-given-as-a-string - comment left Martin Maechler).

Thank you again, and I hope you have a wonderful week!

Shelby


From: Richard O'Keefe <raoknz using gmail.com>
Date: Sunday, September 8, 2019 at 11:01 PM
To: "Golden, Shelby" <GoldenS using NJHealth.org>
Cc: "Richard M. Heiberger" <rmh using temple.edu>, Bert Gunter <bgunter.4567 using gmail.com>, "r-help using R-project.org" <r-help using r-project.org>, "Gillenwater, Lucas" <GILLENWATERL using NJHEALTH.ORG>
Subject: Re: [R] [R-devel] Source Code for function

> I am looking to understand why the keyword function can take a logical argument
> (eg: x<4) and use that later inside the function's definition for logical evaluations

The "function" keyword does not take a logical argument.
Let me show you some parallels:

f <- function (x, y) {x+y}          # R
(set! f (lambda (x y) (+ x y)))     ; Scheme
f = (x, y) => { return x+y; };      // Javascript
f = (x, y) => { return x+y; };      // C#, given a suitable declaration for f
f = (x, y) -> { return x+y; }       // Java, given a suitable declaration for f.
lambda x y; x+y end -> f;           // Pop-2, older than the others.

In all of these,
  - there is something ('function', 'lambda', '=>', '->') that
    says "here is an anonymous function"
  - there is a list of zero or more parameters
  - there is a body which may contain statements and may also
    return a result.
The keyword in itself does nothing.  The compiler recognises the
construction and generates code for a procedure that is bound to
the environment where it is created, so that it can find variables
other than those in its parameter list.

When it comes to passing parameters to a function, there is
nothing special about logical expressions in any of these languages.

Now there *is* something about functions in R that is special.
The S language (which R is based on) is the only one I am familiar
with that combines two properties:
 - it is an imperative language with side effects to variables
 - it does not evaluate function arguments when they are passed
   but when they are first *used*.
An obvious reason for this is to allow plotting methods to construct
labels from their arguments and to allow model fitting methods to
remember the form of the model.

If you want argument evaluation delayed for any other reason, it is
probably better to pass a function.  See
> ?integrate
 -- the first argument is a function, not a general expression
> ?optim
 -- the second argument is a function, not a general expression
(That is, the argument in question is an expression whose value must
be a function, not an expression to be manipulated *textually* or as
a formula.)



On Sat, 7 Sep 2019 at 08:07, Golden, Shelby <GoldenS using njhealth.org<mailto:GoldenS using njhealth.org>> wrote:
Thank you all for your reply. I should clarify, that I am looking to understand why the keyword function can take a logical argument (eg: x<4) and use that later inside the function's definition for logical evaluations.

Consider this example, which is a simplification of getAnywhere(subset.data.frame):
x = data.frame("Col1" = c(1, 2, 3, 4, 5), "Col2" = c(6, 7, 8, 9, 10))
test <- function(x, logic){
        e <- substitute(logic)
        r <- eval(e, x, parent.frame())
        r[r]
}


Shelby


On 9/6/19, 1:02 PM, "R-help on behalf of Richard M. Heiberger" <r-help-bounces using r-project.org<mailto:r-help-bounces using r-project.org> on behalf of rmh using temple.edu<mailto:rmh using temple.edu>> wrote:

    You might also want to look at the codetools package, for example the
    showTree function " Prints a Lisp-style representation of R
    expression."

    > library(codetools)

    > showTree(quote(x %*% x))
    (%*% x x)
    > showTree(quote(a+b))
    (+ a b)
    > showTree(quote(y ~ a+b))
    (~ y (+ a b))

    On Fri, Sep 6, 2019 at 2:30 PM Bert Gunter <bgunter.4567 using gmail.com<mailto:bgunter.4567 using gmail.com>> wrote:
    >
    > The following may be of use (it gives the parse tree of the text):
    >
    > > z <- as.list(parse(text = "function(x)x %*% x"))
    > > z[[1]]
    > function(x) x %*% x
    > > z[[c(1,1)]]
    > `function`
    > > z[[c(1,2)]]
    > $x
    > > z[[c(1,3)]]
    > x %*% x
    > > z[[c(1,3,1)]]
    > `%*%`
    > > z[[c(1,3,2)]]
    > x
    > > z[[c(1,3,3)]]
    > x
    >
    >
    > Bert Gunter
    >
    >
    >
    > On Fri, Sep 6, 2019 at 10:14 AM Wang Jiefei <szwjf08 using gmail.com<mailto:szwjf08 using gmail.com>> wrote:
    >
    > > If you are looking for an R code parser, I think the `parse` and `eval`
    > > function might be a good start point. See the example below.
    > >
    > > > parse(text="function(x)message(x)")
    > > expression(function(x)message(x))
    > > > eval(parse(text="function(x)message(x)"))
    > > function(x)message(x)
    > >
    > > Best,
    > > Jiefei
    > >
    > > On Fri, Sep 6, 2019 at 12:55 PM Golden, Shelby <GoldenS using njhealth.org<mailto:GoldenS using njhealth.org>>
    > > wrote:
    > >
    > >> Hello Bert,
    > >>
    > >> Thank you for the reply and your clarifications. Yes, it might be helpful
    > >> to look into R’s formal grammar to see how “function” parses input to
    > >> delegate correct syntax. Is that accessible online?
    > >>
    > >> Thank you,
    > >> Shelby
    > >>
    > >>
    > >> From: Bert Gunter <bgunter.4567 using gmail.com<mailto:bgunter.4567 using gmail.com>>
    > >> Date: Friday, September 6, 2019 at 10:44 AM
    > >> To: "Golden, Shelby" <GoldenS using NJHealth.org>
    > >> Cc: "r-help using R-project.org" <r-help using r-project.org<mailto:r-help using r-project.org>>, "Gillenwater, Lucas" <
    > >> GILLENWATERL using NJHEALTH.ORG<mailto:GILLENWATERL using NJHEALTH.ORG>>
    > >> Subject: Re: [R] [R-devel] Source Code for function
    > >>
    > >> 1. This is a plain text list; all html is stripped. So there is no red
    > >> highlighting.
    > >>
    > >> 2. There is no "source code" for "function" -- it is a reserved keyword.
    > >> Or are you looking for R's formal grammar -- e.g. how it parses input to
    > >> determine correct syntax?
    > >>
    > >>
    > >>
    > >> Bert Gunter
    > >>
    > >> "The trouble with having an open mind is that people keep coming along
    > >> and sticking things into it."
    > >> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
    > >>
    > >>
    > >> On Fri, Sep 6, 2019 at 8:51 AM Golden, Shelby <GoldenS using njhealth.org<mailto:GoldenS using njhealth.org>
    > >> <mailto:GoldenS using njhealth.org<mailto:GoldenS using njhealth.org>>> wrote:
    > >> Hi all,
    > >>
    > >> I have been attempting to access the source code for the keyword
    > >> “function” to better understand how it assigns and stores logical inputs,
    > >> like in the subset() [base] function. Does anyone know how I can access the
    > >> source code for this?
    > >>
    > >> For example, if I have
    > >> norm <- function(x){
    > >>       sqrt(x%*%x))
    > >> }
    > >> I am looking for the source code for the “function” portion, highlighted
    > >> in red.
    > >>
    > >> Thank you for your time and assistance,
    > >> Shelby Golden
    > >> Lab Researcher Technician
    > >> Dr. Russell Bowler’s Lab
    > >> Department of Medicine
    > >> National Jewish Health in Denver, CO
    > >> Phone: (303) 270-2598
    > >>
    > >> NOTICE: This email message is for the sole use of the intended
    > >> recipient(s) and may contain confidential and privileged information. Any
    > >> unauthorized review, use, disclosure or distribution is prohibited. If you
    > >> are not the intended recipient, please contact the sender by reply email
    > >> and destroy all copies of the original message.
    > >>         [[alternative HTML version deleted]]
    > >>
    > >> ______________________________________________
    > >> R-help using r-project.org<mailto:R-help using r-project.org><mailto:R-help using r-project.org<mailto:R-help using r-project.org>> mailing list -- To
    > >> UNSUBSCRIBE and more, see
    > >> https://stat.ethz.ch/mailman/listinfo/r-help<http://mx1.njhealth.org:32224/?dmVyPTEuMDAxJiY5MzIyNmQ5YTc2OTc0MDMzYT01RDc1RENCMl8zNTY3N183MjY1XzEmJjI4YTJmOGIzZjc0NWM4Mz0xMjMzJiZ1cmw9aHR0cHMlM0ElMkYlMkZzdGF0JTJFZXRoeiUyRWNoJTJGbWFpbG1hbiUyRmxpc3RpbmZvJTJGci1oZWxw><
    > >> http://mx2.njhealth.org:32224/?dmVyPTEuMDAxJiZmMWRiYmMxZjFmNmI5ZDBkMz01RDcyOEQwN18yMjk2OF8zOTk2XzEmJjljNzlmMDA4YWRmZTZjMz0xMjMzJiZ1cmw9aHR0cHMlM0ElMkYlMkZzdGF0JTJFZXRoeiUyRWNoJTJGbWFpbG1hbiUyRmxpc3RpbmZvJTJGci1oZWxw
    > >> >
    > >> PLEASE do read the posting guide
    > >> http://www.R-project.org/posting-guide.html<http://mx1.njhealth.org:32224/?dmVyPTEuMDAxJiY5NDMyMjY5YTZhOTY0MjIwYT01RDc1RENCMl8zNTY3N183MjY1XzEmJmNjOTMyODUyMjJmNTk4ND0xMjMzJiZ1cmw9aHR0cCUzQSUyRiUyRnd3dyUyRVItcHJvamVjdCUyRW9yZyUyRnBvc3RpbmctZ3VpZGUlMkVodG1s><
    > >> http://mx2.njhealth.org:32224/?dmVyPTEuMDAxJiZlMTkwYmMwMzFlNjk4ZTAzNz01RDcyOEQwN18yMjk2OF8zOTk2XzEmJmFkYTkxMWRkMWRhZTFkNz0xMjMzJiZ1cmw9aHR0cCUzQSUyRiUyRnd3dyUyRVItcHJvamVjdCUyRW9yZyUyRnBvc3RpbmctZ3VpZGUlMkVodG1s
    > >> >
    > >> and provide commented, minimal, self-contained, reproducible code.
    > >>
    > >>         [[alternative HTML version deleted]]
    > >>
    > >> ______________________________________________
    > >> R-help using r-project.org<mailto:R-help using r-project.org> mailing list -- To UNSUBSCRIBE and more, see
    > >> https://stat.ethz.ch/mailman/listinfo/r-help<http://mx1.njhealth.org:32224/?dmVyPTEuMDAxJiY5MzIyNmQ5YTc2OTc0MDMzYT01RDc1RENCMl8zNTY3N183MjY1XzEmJjI4YTJmOGIzZjc0NWM4Mz0xMjMzJiZ1cmw9aHR0cHMlM0ElMkYlMkZzdGF0JTJFZXRoeiUyRWNoJTJGbWFpbG1hbiUyRmxpc3RpbmZvJTJGci1oZWxw>
    > >> PLEASE do read the posting guide
    > >> http://www.R-project.org/posting-guide.html<http://mx1.njhealth.org:32224/?dmVyPTEuMDAxJiY5NDMyMjY5YTZhOTY0MjIwYT01RDc1RENCMl8zNTY3N183MjY1XzEmJmNjOTMyODUyMjJmNTk4ND0xMjMzJiZ1cmw9aHR0cCUzQSUyRiUyRnd3dyUyRVItcHJvamVjdCUyRW9yZyUyRnBvc3RpbmctZ3VpZGUlMkVodG1s>
    > >> and provide commented, minimal, self-contained, reproducible code.
    > >>
    > >
    >
    >         [[alternative HTML version deleted]]
    >
    > ______________________________________________
    > R-help using r-project.org<mailto:R-help using r-project.org> mailing list -- To UNSUBSCRIBE and more, see
    > https://stat.ethz.ch/mailman/listinfo/r-help<http://mx1.njhealth.org:32224/?dmVyPTEuMDAxJiY5MzIyNmQ5YTc2OTc0MDMzYT01RDc1RENCMl8zNTY3N183MjY1XzEmJjI4YTJmOGIzZjc0NWM4Mz0xMjMzJiZ1cmw9aHR0cHMlM0ElMkYlMkZzdGF0JTJFZXRoeiUyRWNoJTJGbWFpbG1hbiUyRmxpc3RpbmZvJTJGci1oZWxw>
    > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html<http://mx1.njhealth.org:32224/?dmVyPTEuMDAxJiY5NDMyMjY5YTZhOTY0MjIwYT01RDc1RENCMl8zNTY3N183MjY1XzEmJmNjOTMyODUyMjJmNTk4ND0xMjMzJiZ1cmw9aHR0cCUzQSUyRiUyRnd3dyUyRVItcHJvamVjdCUyRW9yZyUyRnBvc3RpbmctZ3VpZGUlMkVodG1s>
    > and provide commented, minimal, self-contained, reproducible code.

    ______________________________________________
    R-help using r-project.org<mailto:R-help using r-project.org> mailing list -- To UNSUBSCRIBE and more, see
    https://stat.ethz.ch/mailman/listinfo/r-help<http://mx1.njhealth.org:32224/?dmVyPTEuMDAxJiY5MzIyNmQ5YTc2OTc0MDMzYT01RDc1RENCMl8zNTY3N183MjY1XzEmJjI4YTJmOGIzZjc0NWM4Mz0xMjMzJiZ1cmw9aHR0cHMlM0ElMkYlMkZzdGF0JTJFZXRoeiUyRWNoJTJGbWFpbG1hbiUyRmxpc3RpbmZvJTJGci1oZWxw>
    PLEASE do read the posting guide http://www.R-project.org/posting-guide.html<http://mx1.njhealth.org:32224/?dmVyPTEuMDAxJiY5NDMyMjY5YTZhOTY0MjIwYT01RDc1RENCMl8zNTY3N183MjY1XzEmJmNjOTMyODUyMjJmNTk4ND0xMjMzJiZ1cmw9aHR0cCUzQSUyRiUyRnd3dyUyRVItcHJvamVjdCUyRW9yZyUyRnBvc3RpbmctZ3VpZGUlMkVodG1s>
    and provide commented, minimal, self-contained, reproducible code.



NOTICE: This email message is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
______________________________________________
R-help using r-project.org<mailto:R-help using r-project.org> mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help<http://mx1.njhealth.org:32224/?dmVyPTEuMDAxJiY5MzIyNmQ5YTc2OTc0MDMzYT01RDc1RENCMl8zNTY3N183MjY1XzEmJjI4YTJmOGIzZjc0NWM4Mz0xMjMzJiZ1cmw9aHR0cHMlM0ElMkYlMkZzdGF0JTJFZXRoeiUyRWNoJTJGbWFpbG1hbiUyRmxpc3RpbmZvJTJGci1oZWxw>
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html<http://mx1.njhealth.org:32224/?dmVyPTEuMDAxJiY5NDMyMjY5YTZhOTY0MjIwYT01RDc1RENCMl8zNTY3N183MjY1XzEmJmNjOTMyODUyMjJmNTk4ND0xMjMzJiZ1cmw9aHR0cCUzQSUyRiUyRnd3dyUyRVItcHJvamVjdCUyRW9yZyUyRnBvc3RpbmctZ3VpZGUlMkVodG1s>
and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]



More information about the R-help mailing list