[R] [R-devel] Source Code for function
Golden, Shelby
Go|denS @end|ng |rom NJHe@|th@org
Sat Sep 7 00:15:32 CEST 2019
Afternoon, Peter,
Thank you for your concise but informative reply, and for a link to the source code. These complex concepts do answer my question spot on, so thank you for taking the time to put them all together and summarizing them for me.
Is the standard nomenclature for arguments in function calls "promises"? I would like to look into this concept further. If there are other concepts you think I should look into further too, I would be happy to include those as well.
Regards,
Shelby
On 9/6/19, 4:02 PM, "peter dalgaard" <pdalgd using gmail.com> wrote:
Um... Let's get the concepts straight:
The "function" function doesn't evaluate anything. It just takes the list of formal arguments (including default expressions), the function body, and the current evaluation environment, and stiches them together into a function object, known as a "closure".
The action happens when a function is _called_. Then the actual arguments are combined with the formals, and the body expression is evaluated. You will find the source code for this in src/main/eval.c. It is a pretty complex beast, but the essential point for the present discussion is that actual arguments in function calls are passed in the form of so-called promises. These contain the expression passed, so that substitute() can extract it. It also enables lazy evaluation: putting off argument evaluation until the value is actually needed (possibly never).
-pd
> On 6 Sep 2019, at 23:10 , David Winsemius <dwinsemius using comcast.net> wrote:
>
>
> On 9/6/19 1:07 PM, Golden, Shelby 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]
>> }
>
> x<4 is not really a logical argument in that context. It is rather an expression and will remain an expression until it needs to be evaluated. See this even simpler example:
>
>
> test <- function(x, logic, ... ){
> e <- deparse( substitute(logic))
> #r <- eval(e, x, parent.frame())
> e
> }
> test(4, x<4)
> #[1] "x < 4"
>
>
> test(4, is.logical(x < 4) )
> [1] "is.logical(x < 4)"
>
>
> Some of this you have already been told, but appears necessary to repeat. Expressions given to `function` are not necessarily evaluated. They will be evaluated if assigned names.
>
>
> test(4, zed = is.logical(x < 4) )
> #[1] ""
>
> The function()-function will parse the contents of the parentheses for number of arguments and for parse()-ability. It will evaluate named arguments created with "=". In the context of parsing the formals of a function the "=" operator is different than the "<-" function.
>
> The substitute function will not evaluate (since in the language of R operations it is "special"), but rather checks that the expression can be parsed by R's rules, i.e. is a valid parse tree. `deparse` returns the original character representation.
>
> --
>
> David
>
>
>>
>> Shelby
>>
>> On 9/6/19, 1:02 PM, "R-help on behalf of Richard M. Heiberger" <r-help-bounces using r-project.org on behalf of 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> 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> 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>
>> > > 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>
>> > >> 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>, "Gillenwater, Lucas" <
>> > >> 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>> 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> mailing list -- To
>> > >> UNSUBSCRIBE and more, see
>> > >> https://stat.ethz.ch/mailman/listinfo/r-help<
>> > >> http://mx2.njhealth.org:32224/?dmVyPTEuMDAxJiZmMWRiYmMxZjFmNmI5ZDBkMz01RDcyOEQwN18yMjk2OF8zOTk2XzEmJjljNzlmMDA4YWRmZTZjMz0xMjMzJiZ1cmw9aHR0cHMlM0ElMkYlMkZzdGF0JTJFZXRoeiUyRWNoJTJGbWFpbG1hbiUyRmxpc3RpbmZvJTJGci1oZWxw
>> > >> >
>> > >> PLEASE do read the posting guide
>> > >> http://www.R-project.org/posting-guide.html<
>> > >> http://mx2.njhealth.org:32224/?dmVyPTEuMDAxJiZlMTkwYmMwMzFlNjk4ZTAzNz01RDcyOEQwN18yMjk2OF8zOTk2XzEmJmFkYTkxMWRkMWRhZTFkNz0xMjMzJiZ1cmw9aHR0cCUzQSUyRiUyRnd3dyUyRVItcHJvamVjdCUyRW9yZyUyRnBvc3RpbmctZ3VpZGUlMkVodG1s
>> > >> >
>> > >> and provide commented, minimal, self-contained, reproducible code.
>> > >>
>> > >> [[alternative HTML version deleted]]
>> > >>
>> > >> ______________________________________________
>> > >> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> > >> 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.
>> > >>
>> > >
>> >
>> > [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> > 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.
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>>
>> 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 mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk Priv: PDalgd using gmail.com
More information about the R-help
mailing list