[R-devel] Source Code for function

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

[R-devel] Source Code for function

Golden, Shelby
______________________________________________
[hidden email] 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_Function.pdf (76K) Download Attachment
getAnywhere(subset.data.frame).png (192K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

[R-devel] Source Code for function

Golden, Shelby
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R-devel] Source Code for function

Bert Gunter-2
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R-devel] Source Code for function

Golden, Shelby
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 <[hidden email]>
Date: Friday, September 6, 2019 at 10:44 AM
To: "Golden, Shelby" <[hidden email]>
Cc: "[hidden email]" <[hidden email]>, "Gillenwater, Lucas" <[hidden email]>
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 <[hidden email]<mailto:[hidden email]>> 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]]

______________________________________________
[hidden email]<mailto:[hidden email]> 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R-devel] Source Code for function

Wang Jiefei
In reply to this post by Golden, Shelby
Hi Shelby,

Not quite sure what you are trying to do. Mine might be off-topic but have
you seen this document?

http://adv-r.had.co.nz/Functions.html#function-components

It illustrates the components of a function.

Best,
Jiefei

On Fri, Sep 6, 2019 at 11:52 AM Golden, Shelby <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R-devel] Source Code for function

Wang Jiefei
In reply to this post by Golden, Shelby
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 <[hidden email]> 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 <[hidden email]>
> Date: Friday, September 6, 2019 at 10:44 AM
> To: "Golden, Shelby" <[hidden email]>
> Cc: "[hidden email]" <[hidden email]>, "Gillenwater, Lucas" <
> [hidden email]>
> 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 <[hidden email]
> <mailto:[hidden email]>> 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]]
>
> ______________________________________________
> [hidden email]<mailto:[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R-devel] Source Code for function

Bert Gunter-2
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 <[hidden email]> 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 <[hidden email]>
> 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 <[hidden email]>
>> Date: Friday, September 6, 2019 at 10:44 AM
>> To: "Golden, Shelby" <[hidden email]>
>> Cc: "[hidden email]" <[hidden email]>, "Gillenwater, Lucas" <
>> [hidden email]>
>> 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 <[hidden email]
>> <mailto:[hidden email]>> 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]]
>>
>> ______________________________________________
>> [hidden email]<mailto:[hidden email]> 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]]
>>
>> ______________________________________________
>> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R-devel] Source Code for function

Richard M. Heiberger
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]>
> > 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 <[hidden email]>
> >> Date: Friday, September 6, 2019 at 10:44 AM
> >> To: "Golden, Shelby" <[hidden email]>
> >> Cc: "[hidden email]" <[hidden email]>, "Gillenwater, Lucas" <
> >> [hidden email]>
> >> 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 <[hidden email]
> >> <mailto:[hidden email]>> 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]]
> >>
> >> ______________________________________________
> >> [hidden email]<mailto:[hidden email]> 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]]
> >>
> >> ______________________________________________
> >> [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R-devel] Source Code for function

Golden, Shelby
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" <[hidden email] on behalf of [hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]>
    > > 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 <[hidden email]>
    > >> Date: Friday, September 6, 2019 at 10:44 AM
    > >> To: "Golden, Shelby" <[hidden email]>
    > >> Cc: "[hidden email]" <[hidden email]>, "Gillenwater, Lucas" <
    > >> [hidden email]>
    > >> 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 <[hidden email]
    > >> <mailto:[hidden email]>> 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]]
    > >>
    > >> ______________________________________________
    > >> [hidden email]<mailto:[hidden email]> 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]]
    > >>
    > >> ______________________________________________
    > >> [hidden email] 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]]
    >
    > ______________________________________________
    > [hidden email] 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.
   
    ______________________________________________
    [hidden email] 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.
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R-devel] Source Code for function

Golden, Shelby
In reply to this post by Wang Jiefei
Hello Jiefei,

Missed this in the subsequent emails. That is a very intriguing article, but I do not believe that it quite answers my question. Regardless, I will review it, so thank you for sending it my way!

Respectfully,
Shelby


From: Wang Jiefei <[hidden email]>
Date: Friday, September 6, 2019 at 11:10 AM
To: "Golden, Shelby" <[hidden email]>
Cc: "[hidden email]" <[hidden email]>, "Gillenwater, Lucas" <[hidden email]>
Subject: Re: [R] [R-devel] Source Code for function

Hi Shelby,

Not quite sure what you are trying to do. Mine might be off-topic but have you seen this document?

http://adv-r.had.co.nz/Functions.html#function-components<http://mx1.njhealth.org:32224/?dmVyPTEuMDAxJiYzZDQ2Y2Y3YzhmYmIwZmMzMz01RDcyOTJGNl81MzQ2N180NjY0XzEmJjBjMTRiYjE5NTI5MjI2MD0xMjMzJiZ1cmw9aHR0cCUzQSUyRiUyRmFkdi1yJTJFaGFkJTJFY28lMkVueiUyRkZ1bmN0aW9ucyUyRWh0bWwlMjNmdW5jdGlvbi1jb21wb25lbnRz>

It illustrates the components of a function.

Best,
Jiefei

On Fri, Sep 6, 2019 at 11:52 AM Golden, Shelby <[hidden email]<mailto:[hidden email]>> 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]]

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

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R-devel] Source Code for function

David Winsemius
In reply to this post by Golden, Shelby

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" <[hidden email] on behalf of [hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]>
>      > > 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 <[hidden email]>
>      > >> Date: Friday, September 6, 2019 at 10:44 AM
>      > >> To: "Golden, Shelby" <[hidden email]>
>      > >> Cc: "[hidden email]" <[hidden email]>, "Gillenwater, Lucas" <
>      > >> [hidden email]>
>      > >> 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 <[hidden email]
>      > >> <mailto:[hidden email]>> 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]]
>      > >>
>      > >> ______________________________________________
>      > >> [hidden email]<mailto:[hidden email]> 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]]
>      > >>
>      > >> ______________________________________________
>      > >> [hidden email] 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]]
>      >
>      > ______________________________________________
>      > [hidden email] 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.
>      
>      ______________________________________________
>      [hidden email] 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.
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R-devel] Source Code for function

Golden, Shelby
Afternoon, David,

Thank you for your suggestions and insight. I have previously utilized parse, but in my exploration to improve my coding technique I came across this comment in stackoverflow.com (https://stackoverflow.com/questions/1743698/evaluate-expression-given-as-a-string - comment left Martin Maechler). In it, he suggests that usage parse is not a good method, saying that it is "rarely an efficient or safe means to construct expressions (or calls)".

Perhaps you, or others in this community, disagree with what Martin has to say about parse in this application?

Shelby
 

On 9/6/19, 3:10 PM, "David Winsemius" <[hidden email]> 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" <[hidden email] on behalf of [hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]>
    >      > > 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 <[hidden email]>
    >      > >> Date: Friday, September 6, 2019 at 10:44 AM
    >      > >> To: "Golden, Shelby" <[hidden email]>
    >      > >> Cc: "[hidden email]" <[hidden email]>, "Gillenwater, Lucas" <
    >      > >> [hidden email]>
    >      > >> 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 <[hidden email]
    >      > >> <mailto:[hidden email]>> 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]]
    >      > >>
    >      > >> ______________________________________________
    >      > >> [hidden email]<mailto:[hidden email]> 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]]
    >      > >>
    >      > >> ______________________________________________
    >      > >> [hidden email] 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]]
    >      >
    >      > ______________________________________________
    >      > [hidden email] 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.
    >      
    >      ______________________________________________
    >      [hidden email] 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.
    > ______________________________________________
    > [hidden email] 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.
   

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R-devel] Source Code for function

Peter Dalgaard-2
In reply to this post by David Winsemius
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 <[hidden email]> 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" <[hidden email] on behalf of [hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]>
>>     > > 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 <[hidden email]>
>>     > >> Date: Friday, September 6, 2019 at 10:44 AM
>>     > >> To: "Golden, Shelby" <[hidden email]>
>>     > >> Cc: "[hidden email]" <[hidden email]>, "Gillenwater, Lucas" <
>>     > >> [hidden email]>
>>     > >> 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 <[hidden email]
>>     > >> <mailto:[hidden email]>> 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]]
>>     > >>
>>     > >> ______________________________________________
>>     > >> [hidden email]<mailto:[hidden email]> 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]]
>>     > >>
>>     > >> ______________________________________________
>>     > >> [hidden email] 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]]
>>     >
>>     > ______________________________________________
>>     > [hidden email] 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.
>>          ______________________________________________
>>     [hidden email] 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.
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R-devel] Source Code for function

Golden, Shelby
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" <[hidden email]> 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 <[hidden email]> 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" <[hidden email] on behalf of [hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]>
    >>     > > 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 <[hidden email]>
    >>     > >> Date: Friday, September 6, 2019 at 10:44 AM
    >>     > >> To: "Golden, Shelby" <[hidden email]>
    >>     > >> Cc: "[hidden email]" <[hidden email]>, "Gillenwater, Lucas" <
    >>     > >> [hidden email]>
    >>     > >> 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 <[hidden email]
    >>     > >> <mailto:[hidden email]>> 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]]
    >>     > >>
    >>     > >> ______________________________________________
    >>     > >> [hidden email]<mailto:[hidden email]> 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]]
    >>     > >>
    >>     > >> ______________________________________________
    >>     > >> [hidden email] 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]]
    >>     >
    >>     > ______________________________________________
    >>     > [hidden email] 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.
    >>          ______________________________________________
    >>     [hidden email] 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.
    >> ______________________________________________
    >> [hidden email] 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.
    >
    > ______________________________________________
    > [hidden email] 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: [hidden email]  Priv: [hidden email]
   
   
   
   
   
   
   
   
   
   

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R-devel] Source Code for function

Bert Gunter-2
It is time to end these queries and start your homework by consulting the
references you have already been given. Wickham's goes into all of this;
the R Language Definition that ships with R covers all of your questions
and more in detail; and numerous online references and tutorials -- A
search on "what is a promise in R" brings up numerous resources, for
example -- are available.

This list is for help. It is not a substitute for studying on your own.

Cheers,
Bert

On Fri, Sep 6, 2019 at 3:15 PM Golden, Shelby <[hidden email]> wrote:

> 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" <[hidden email]> 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 <[hidden email]>
> 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" <
> [hidden email] on behalf of [hidden email]> 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 <
> [hidden email]> 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 <
> [hidden email]> 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 <
> [hidden email]>
>     >>     > > 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 <[hidden email]>
>     >>     > >> Date: Friday, September 6, 2019 at 10:44 AM
>     >>     > >> To: "Golden, Shelby" <[hidden email]>
>     >>     > >> Cc: "[hidden email]" <[hidden email]>,
> "Gillenwater, Lucas" <
>     >>     > >> [hidden email]>
>     >>     > >> 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 <
> [hidden email]
>     >>     > >> <mailto:[hidden email]>> 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]]
>     >>     > >>
>     >>     > >> ______________________________________________
>     >>     > >> [hidden email]<mailto:[hidden email]> 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]]
>     >>     > >>
>     >>     > >> ______________________________________________
>     >>     > >> [hidden email] 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]]
>     >>     >
>     >>     > ______________________________________________
>     >>     > [hidden email] 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.
>     >>          ______________________________________________
>     >>     [hidden email] 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.
>     >> ______________________________________________
>     >> [hidden email] 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.
>     >
>     > ______________________________________________
>     > [hidden email] 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: [hidden email]  Priv: [hidden email]
>
>
>
>
>
>
>
>
>
>
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R-devel] Source Code for function

Richard O'Keefe-2
In reply to this post by Golden, Shelby
> 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 <[hidden email]> 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" <
> [hidden email] on behalf of [hidden email]> 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 <[hidden email]>
> 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 <[hidden email]>
> 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 <
> [hidden email]>
>     > > 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 <[hidden email]>
>     > >> Date: Friday, September 6, 2019 at 10:44 AM
>     > >> To: "Golden, Shelby" <[hidden email]>
>     > >> Cc: "[hidden email]" <[hidden email]>, "Gillenwater,
> Lucas" <
>     > >> [hidden email]>
>     > >> 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 <
> [hidden email]
>     > >> <mailto:[hidden email]>> 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]]
>     > >>
>     > >> ______________________________________________
>     > >> [hidden email]<mailto:[hidden email]> 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]]
>     > >>
>     > >> ______________________________________________
>     > >> [hidden email] 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]]
>     >
>     > ______________________________________________
>     > [hidden email] 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.
>
>     ______________________________________________
>     [hidden email] 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.
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R-devel] Source Code for function

Golden, Shelby
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 <[hidden email]>
Date: Sunday, September 8, 2019 at 11:01 PM
To: "Golden, Shelby" <[hidden email]>
Cc: "Richard M. Heiberger" <[hidden email]>, Bert Gunter <[hidden email]>, "[hidden email]" <[hidden email]>, "Gillenwater, Lucas" <[hidden email]>
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 <[hidden email]<mailto:[hidden email]>> 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" <[hidden email]<mailto:[hidden email]> on behalf of [hidden email]<mailto:[hidden email]>> 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 <[hidden email]<mailto:[hidden email]>> 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 <[hidden email]<mailto:[hidden email]>> 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 <[hidden email]<mailto:[hidden email]>>
    > > 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 <[hidden email]<mailto:[hidden email]>>
    > >> Date: Friday, September 6, 2019 at 10:44 AM
    > >> To: "Golden, Shelby" <[hidden email]>
    > >> Cc: "[hidden email]" <[hidden email]<mailto:[hidden email]>>, "Gillenwater, Lucas" <
    > >> [hidden email]<mailto:[hidden email]>>
    > >> 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 <[hidden email]<mailto:[hidden email]>
    > >> <mailto:[hidden email]<mailto:[hidden email]>>> 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]]
    > >>
    > >> ______________________________________________
    > >> [hidden email]<mailto:[hidden email]><mailto:[hidden email]<mailto:[hidden email]>> 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]]
    > >>
    > >> ______________________________________________
    > >> [hidden email]<mailto:[hidden email]> 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]]
    >
    > ______________________________________________
    > [hidden email]<mailto:[hidden email]> 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.

    ______________________________________________
    [hidden email]<mailto:[hidden email]> 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.
______________________________________________
[hidden email]<mailto:[hidden email]> 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R-devel] Source Code for function

David Winsemius

On 9/10/19 11:13 AM, Golden, Shelby wrote:
> 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).


It appears you have in mind keeping around a set of "off-the-shelf"
expressions that will be chosen for varying situations. It seems to me
that you should instead be storing complex predefined logical
constructions in functions that can be applied to multi-column objects
like dataframes, data.tables, or matrices:


 > A_is_GT_B <- function(A, B){ A > B}
 > set.seed(123)
 > test <- data.frame(x=sample(10),y=sample(10), z=sample(10) )
 > test$x_GT_y <- with(test, A_is_GT_B(x,y))
 > test
     x  y  z x_GT_y
1   3 10  8  FALSE
2  10  5  7   TRUE
3   2  3  2  FALSE
4   8  8  1  FALSE
5   6  1  6   TRUE
6   9  4  3   TRUE
7   1  6  4  FALSE
8   7  9 10  FALSE
9   5  7  9  FALSE

10  4  2  5   TRUE

The expressions then are encapsulated inside the function and you can
make lists that carry multiple functions.

It does remain possible to have expression vectors:

exprs <- expression( x > y, x >+ z)

eval( exprs[[2]], test)

 > exprs <- expression( x > y, x >+ z)
 >
 > eval( exprs[[2]], test)
  [1] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE


This however is considered poor practice. Notice that you are
constrained to know ahead of time the column names of the arguments to
be passed, whereas with the function-approach, the names are not
required and you can use positional matching.

--

David.

>
> Thank you again, and I hope you have a wonderful week!
>
> Shelby
>
>
> From: Richard O'Keefe <[hidden email]>
> Date: Sunday, September 8, 2019 at 11:01 PM
> To: "Golden, Shelby" <[hidden email]>
> Cc: "Richard M. Heiberger" <[hidden email]>, Bert Gunter <[hidden email]>, "[hidden email]" <[hidden email]>, "Gillenwater, Lucas" <[hidden email]>
> 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 <[hidden email]<mailto:[hidden email]>> 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" <[hidden email]<mailto:[hidden email]> on behalf of [hidden email]<mailto:[hidden email]>> 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 <[hidden email]<mailto:[hidden email]>> 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 <[hidden email]<mailto:[hidden email]>> 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 <[hidden email]<mailto:[hidden email]>>
>      > > 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 <[hidden email]<mailto:[hidden email]>>
>      > >> Date: Friday, September 6, 2019 at 10:44 AM
>      > >> To: "Golden, Shelby" <[hidden email]>
>      > >> Cc: "[hidden email]" <[hidden email]<mailto:[hidden email]>>, "Gillenwater, Lucas" <
>      > >> [hidden email]<mailto:[hidden email]>>
>      > >> 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 <[hidden email]<mailto:[hidden email]>
>      > >> <mailto:[hidden email]<mailto:[hidden email]>>> 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]]
>      > >>
>      > >> ______________________________________________
>      > >> [hidden email]<mailto:[hidden email]><mailto:[hidden email]<mailto:[hidden email]>> 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]]
>      > >>
>      > >> ______________________________________________
>      > >> [hidden email]<mailto:[hidden email]> 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]]
>      >
>      > ______________________________________________
>      > [hidden email]<mailto:[hidden email]> 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.
>
>      ______________________________________________
>      [hidden email]<mailto:[hidden email]> 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.
> ______________________________________________
> [hidden email]<mailto:[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.