Why don't the comments appear in the function?

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

Why don't the comments appear in the function?

bickis
I have made a list in which each element is a function.   If I print
individual elements of the list, then the function code is shown along
with embedded comments.  However, if I print the list or sublist, then the
function code is shown without comments.   Why (and how) are the comments
hidden?

>flist[[3]]
function(y,brackets,rates){
        # Calculates before-tax income required to realized value y
        ints<-c(0,cumsum(diff(brackets)*rates[1:(length(rates)-1)]))-brackets*rates
        x<-(y+ints)/(1-rates)
        x[sum(x>brackets)]
}


>flist[3]
$btv
function (y, brackets, rates)
{
    ints <- c(0, cumsum(diff(brackets) * rates[1:(length(rates) -
        1)])) - brackets * rates
    x <- (y + ints)/(1 - rates)
    x[sum(x > brackets)]
}

I am running R 3.3.2 on Mac OS X  10.10.5

Mik Bickis

______________________________________________
[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: Why don't the comments appear in the function?

Bert Gunter-2
I do not see this behavior.

f <- function(x){
   ## a comment
   2
}

g <- list(a =2, fun =f)

> g
$a
[1] 2

$fun
function(x){
   ## a comment
   2
}

> g[[2]]
function(x){
   ## a comment
   2
}
> g[2]
$fun
function(x){
   ## a comment
   2
}

Cheers,
Bert

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 Mon, Nov 25, 2019 at 7:03 AM <[hidden email]> wrote:

> I have made a list in which each element is a function.   If I print
> individual elements of the list, then the function code is shown along
> with embedded comments.  However, if I print the list or sublist, then the
> function code is shown without comments.   Why (and how) are the comments
> hidden?
>
> >flist[[3]]
> function(y,brackets,rates){
>         # Calculates before-tax income required to realized value y
>
> ints<-c(0,cumsum(diff(brackets)*rates[1:(length(rates)-1)]))-brackets*rates
>         x<-(y+ints)/(1-rates)
>         x[sum(x>brackets)]
> }
>
>
> >flist[3]
> $btv
> function (y, brackets, rates)
> {
>     ints <- c(0, cumsum(diff(brackets) * rates[1:(length(rates) -
>         1)])) - brackets * rates
>     x <- (y + ints)/(1 - rates)
>     x[sum(x > brackets)]
> }
>
> I am running R 3.3.2 on Mac OS X  10.10.5
>
> Mik Bickis
>
> ______________________________________________
> [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: Why don't the comments appear in the function?

bickis
Different version of R perhaps?   Or is there something in my environment
or "preferences" that is different?

Here's what I get with your example.

>  f <- function(x){
+     ## a comment
+     2
+  }
> g <- list(a =2, fun =f)
> g
$a
[1] 2

$fun
function (x)
{
    2
}

> g[[2]]
function(x){
    ## a comment
    2
 }
> g[1]
$a
[1] 2




Mik


> I do not see this behavior.
>
> f <- function(x){
>    ## a comment
>    2
> }
>
> g <- list(a =2, fun =f)
>
>> g
> $a
> [1] 2
>
> $fun
> function(x){
>    ## a comment
>    2
> }
>
>> g[[2]]
> function(x){
>    ## a comment
>    2
> }
>> g[2]
> $fun
> function(x){
>    ## a comment
>    2
> }
>
> Cheers,
> Bert
>
> 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 Mon, Nov 25, 2019 at 7:03 AM
> <[hidden email]<mailto:[hidden email]>> wrote:
> I have made a list in which each element is a function.   If I print
> individual elements of the list, then the function code is shown along
> with embedded comments.  However, if I print the list or sublist, then the
> function code is shown without comments.   Why (and how) are the comments
> hidden?
>
>>flist[[3]]
> function(y,brackets,rates){
>         # Calculates before-tax income required to realized value y
>         ints<-c(0,cumsum(diff(brackets)*rates[1:(length(rates)-1)]))-brackets*rates
>         x<-(y+ints)/(1-rates)
>         x[sum(x>brackets)]
> }
>
>
>>flist[3]
> $btv
> function (y, brackets, rates)
> {
>     ints <- c(0, cumsum(diff(brackets) * rates[1:(length(rates) -
>         1)])) - brackets * rates
>     x <- (y + ints)/(1 - rates)
>     x[sum(x > brackets)]
> }
>
> I am running R 3.3.2 on Mac OS X  10.10.5
>
> Mik Bickis
>
> ______________________________________________
> [hidden email]<mailto:[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: Why don't the comments appear in the function?

Bert Gunter-2
Dunno.

> R.Version()
$platform
[1] "x86_64-apple-darwin15.6.0"

$arch
[1] "x86_64"

$os
[1] "darwin15.6.0"

$system
[1] "x86_64, darwin15.6.0"

$status
[1] ""

$major
[1] "3"

$minor
[1] "6.1"

$year
[1] "2019"

$month
[1] "07"

$day
[1] "05"

$`svn rev`
[1] "76782"

$language
[1] "R"

$version.string
[1] "R version 3.6.1 (2019-07-05)"

$nickname
[1] "Action of the Toes"

You should upgrade to the latest version (which I have not done yet) if you
haven't already done so.

Additional notes:
I saw the same results at the command line from R -- vanilla that I got
from the RStudio console(what I showed you). Have you done this? This
should indicate whether it might have something to do with your
environment, etc..

However, executing the file with Rscript stripped the comments from both
g[[2]] and g[2].

Cheers,
Bert



On Mon, Nov 25, 2019 at 9:40 AM <[hidden email]> wrote:

> Different version of R perhaps?   Or is there something in my environment
> or "preferences" that is different?
>
> Here's what I get with your example.
>
> >  f <- function(x){
> +     ## a comment
> +     2
> +  }
> > g <- list(a =2, fun =f)
> > g
> $a
> [1] 2
>
> $fun
> function (x)
> {
>     2
> }
>
> > g[[2]]
> function(x){
>     ## a comment
>     2
>  }
> > g[1]
> $a
> [1] 2
>
>
>
>
> Mik
>
>
> > I do not see this behavior.
> >
> > f <- function(x){
> >    ## a comment
> >    2
> > }
> >
> > g <- list(a =2, fun =f)
> >
> >> g
> > $a
> > [1] 2
> >
> > $fun
> > function(x){
> >    ## a comment
> >    2
> > }
> >
> >> g[[2]]
> > function(x){
> >    ## a comment
> >    2
> > }
> >> g[2]
> > $fun
> > function(x){
> >    ## a comment
> >    2
> > }
> >
> > Cheers,
> > Bert
> >
> > 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 Mon, Nov 25, 2019 at 7:03 AM
> > <[hidden email]<mailto:[hidden email]>> wrote:
> > I have made a list in which each element is a function.   If I print
> > individual elements of the list, then the function code is shown along
> > with embedded comments.  However, if I print the list or sublist, then
> the
> > function code is shown without comments.   Why (and how) are the comments
> > hidden?
> >
> >>flist[[3]]
> > function(y,brackets,rates){
> >         # Calculates before-tax income required to realized value y
> >
>  ints<-c(0,cumsum(diff(brackets)*rates[1:(length(rates)-1)]))-brackets*rates
> >         x<-(y+ints)/(1-rates)
> >         x[sum(x>brackets)]
> > }
> >
> >
> >>flist[3]
> > $btv
> > function (y, brackets, rates)
> > {
> >     ints <- c(0, cumsum(diff(brackets) * rates[1:(length(rates) -
> >         1)])) - brackets * rates
> >     x <- (y + ints)/(1 - rates)
> >     x[sum(x > brackets)]
> > }
> >
> > I am running R 3.3.2 on Mac OS X  10.10.5
> >
> > Mik Bickis
> >
> > ______________________________________________
> > [hidden email]<mailto:[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: Why don't the comments appear in the function?

Duncan Murdoch-2
In reply to this post by bickis
On 24/11/2019 6:06 p.m., [hidden email] wrote:
> I have made a list in which each element is a function.   If I print
> individual elements of the list, then the function code is shown along
> with embedded comments.  However, if I print the list or sublist, then the
> function code is shown without comments.   Why (and how) are the comments
> hidden?

Hi Mik.  I see the same behaviour as Bert, not what you see.

Generally the comments will be shown if the "srcref" attribute is
attached to the function, and it is a valid one.  That shouldn't change
between looking at flist[[3]] and flist[3].  If it's not there or not
valid, you'll see a deparsed version of the function; that's what your
flist[3]$btv looks like.

It's possible this is R 3.3.2-specific; that's a relatively old version
now, but I don't know of any change that would cause this.  Can you show
us the code that you used to create flist, or enough of it to show this
behaviour?  Showing us (or me privately) dput(flist) might be enough to
see what's going on.

Duncan Murdoch

>
>> flist[[3]]
> function(y,brackets,rates){
> # Calculates before-tax income required to realized value y
> ints<-c(0,cumsum(diff(brackets)*rates[1:(length(rates)-1)]))-brackets*rates
> x<-(y+ints)/(1-rates)
> x[sum(x>brackets)]
> }
>
>
>> flist[3]
> $btv
> function (y, brackets, rates)
> {
>      ints <- c(0, cumsum(diff(brackets) * rates[1:(length(rates) -
>          1)])) - brackets * rates
>      x <- (y + ints)/(1 - rates)
>      x[sum(x > brackets)]
> }
>
> I am running R 3.3.2 on Mac OS X  10.10.5
>
> Mik Bickis
>
> ______________________________________________
> [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: Why don't the comments appear in the function?

Neal Fultz-2
Hi Mik,

Echoing Bert and Duncan's suggestions, please see also ?srcref and
?getSrcref
and consider upgrading to a currently supported version of R.

I'd also call out the "keep.source" installation option as important if
your functions
are inside a package - there is some discussion around this at
https://github.com/DeclareDesign/DesignLibrary/issues/50 and linked issues
from
the last time I encountered this weird corner of R.

If anyone finds a better solution (or for the related problem of preserving
whitespace),
I would be interested as well.

Very respectfully,

Neal Fultz

On Mon, Nov 25, 2019 at 10:49 AM Duncan Murdoch <[hidden email]>
wrote:

> On 24/11/2019 6:06 p.m., [hidden email] wrote:
> > I have made a list in which each element is a function.   If I print
> > individual elements of the list, then the function code is shown along
> > with embedded comments.  However, if I print the list or sublist, then
> the
> > function code is shown without comments.   Why (and how) are the comments
> > hidden?
>
> Hi Mik.  I see the same behaviour as Bert, not what you see.
>
> Generally the comments will be shown if the "srcref" attribute is
> attached to the function, and it is a valid one.  That shouldn't change
> between looking at flist[[3]] and flist[3].  If it's not there or not
> valid, you'll see a deparsed version of the function; that's what your
> flist[3]$btv looks like.
>
> It's possible this is R 3.3.2-specific; that's a relatively old version
> now, but I don't know of any change that would cause this.  Can you show
> us the code that you used to create flist, or enough of it to show this
> behaviour?  Showing us (or me privately) dput(flist) might be enough to
> see what's going on.
>
> Duncan Murdoch
>
> >
> >> flist[[3]]
> > function(y,brackets,rates){
> >       # Calculates before-tax income required to realized value y
> >
>  ints<-c(0,cumsum(diff(brackets)*rates[1:(length(rates)-1)]))-brackets*rates
> >       x<-(y+ints)/(1-rates)
> >       x[sum(x>brackets)]
> > }
> >
> >
> >> flist[3]
> > $btv
> > function (y, brackets, rates)
> > {
> >      ints <- c(0, cumsum(diff(brackets) * rates[1:(length(rates) -
> >          1)])) - brackets * rates
> >      x <- (y + ints)/(1 - rates)
> >      x[sum(x > brackets)]
> > }
> >
> > I am running R 3.3.2 on Mac OS X  10.10.5
> >
> > Mik Bickis
> >
> > ______________________________________________
> > [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.
>

        [[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: Why don't the comments appear in the function?

Duncan Murdoch-2
On 25/11/2019 2:27 p.m., Neal Fultz wrote:

> Hi Mik,
>
> Echoing Bert and Duncan's suggestions, please see also ?srcref and
> ?getSrcref
> and consider upgrading to a currently supported version of R.
>
> I'd also call out the "keep.source" installation option as important if
> your functions
> are inside a package - there is some discussion around this at
> https://github.com/DeclareDesign/DesignLibrary/issues/50 and linked issues
> from
> the last time I encountered this weird corner of R.
>
> If anyone finds a better solution (or for the related problem of preserving
> whitespace),
> I would be interested as well.

Preserving comments and whitespace are definitely the same issue.  I'm
not sure what solution you're trying to improve, but if you want source
kept in packages, run

Sys.setenv(R_KEEP_PKG_SOURCE="yes")

before doing the install using install.packages(), or use the command
line option with R CMD INSTALL.

If you want the source kept for your own function regardless of the
user's wishes, I think you'll have to do some weird things in your
source files.  For example, if pkg::fn is defined in the file
pkg/R/fn.R, you could put this into your zzz.R file (any name is fine,
as long as it collates after fn.R):

  sys.source("R/fn.R", keep.source = TRUE, envir = getNamespace("pkg"))

This will replace fn with a new copy that has source references.  It
assumes that the namespace has been created at this point in
installation; I think that's true, but there may be cases where it's not
true, in which case you'll need to use something like environment(fn) as
the envir argument.

Duncan Murdoch

>
> Very respectfully,
>
> Neal Fultz
>
> On Mon, Nov 25, 2019 at 10:49 AM Duncan Murdoch <[hidden email]>
> wrote:
>
>> On 24/11/2019 6:06 p.m., [hidden email] wrote:
>>> I have made a list in which each element is a function.   If I print
>>> individual elements of the list, then the function code is shown along
>>> with embedded comments.  However, if I print the list or sublist, then
>> the
>>> function code is shown without comments.   Why (and how) are the comments
>>> hidden?
>>
>> Hi Mik.  I see the same behaviour as Bert, not what you see.
>>
>> Generally the comments will be shown if the "srcref" attribute is
>> attached to the function, and it is a valid one.  That shouldn't change
>> between looking at flist[[3]] and flist[3].  If it's not there or not
>> valid, you'll see a deparsed version of the function; that's what your
>> flist[3]$btv looks like.
>>
>> It's possible this is R 3.3.2-specific; that's a relatively old version
>> now, but I don't know of any change that would cause this.  Can you show
>> us the code that you used to create flist, or enough of it to show this
>> behaviour?  Showing us (or me privately) dput(flist) might be enough to
>> see what's going on.
>>
>> Duncan Murdoch
>>
>>>
>>>> flist[[3]]
>>> function(y,brackets,rates){
>>>        # Calculates before-tax income required to realized value y
>>>
>>   ints<-c(0,cumsum(diff(brackets)*rates[1:(length(rates)-1)]))-brackets*rates
>>>        x<-(y+ints)/(1-rates)
>>>        x[sum(x>brackets)]
>>> }
>>>
>>>
>>>> flist[3]
>>> $btv
>>> function (y, brackets, rates)
>>> {
>>>       ints <- c(0, cumsum(diff(brackets) * rates[1:(length(rates) -
>>>           1)])) - brackets * rates
>>>       x <- (y + ints)/(1 - rates)
>>>       x[sum(x > brackets)]
>>> }
>>>
>>> I am running R 3.3.2 on Mac OS X  10.10.5
>>>
>>> Mik Bickis
>>>
>>> ______________________________________________
>>> [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.
>>
>
> [[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.