Strange behaviour of R?

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

Strange behaviour of R?

Sigbert Klinke
Hi,

I wrote a function like

test <- function(FUN, args) {
   print(FUN)
   FUN(args)
}

When I call it lieke this

test(mean, 1:10)
test(NULL, 1:10)

then the second call still uses mean, although I set FUN to NULL. Is
that ok?

Actually, I used something like

test(mean, list(x=1:10, na.rm=TRUE))

which actually crashed R, but I can not reproduce it. Of course, when I
replaced FUN(args) with do.call(FUN, args) then everything works fine.

Sigbert

--
https://hu.berlin/sk
https://hu.berlin/mmstat3

______________________________________________
[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: Strange behaviour of R?

R help mailing list-2
Hi

> On 17 Jan 2020, at 08:33, Sigbert Klinke <[hidden email]> wrote:
>
> Hi,
>
> I wrote a function like
>
> test <- function(FUN, args) {
>  print(FUN)
>  FUN(args)
> }
>
> When I call it lieke this
>
> test(mean, 1:10)
> test(NULL, 1:10)
>
> then the second call still uses mean, although I set FUN to NULL. Is that ok?

Not for me - macOS, R 3.6.2


>
> Actually, I used something like
>
> test(mean, list(x=1:10, na.rm=TRUE))

Fails as expected,

Rainer

>
> which actually crashed R, but I can not reproduce it. Of course, when I replaced FUN(args) with do.call(FUN, args) then everything works fine.
>
> Sigbert
>
> --
> https://hu.berlin/sk
> https://hu.berlin/mmstat3
>
> ______________________________________________
> [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.

--
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany)

Orcid ID: 0000-0002-7490-0066

Department of Evolutionary Biology and Environmental Studies
University of Zürich
Office Y34-J-74
Winterthurerstrasse 190
8075 Zürich
Switzerland

Office: +41 (0)44 635 47 64
Cell:       +41 (0)78 630 66 57
email:      [hidden email]
                [hidden email]
Skype:     RMkrug

PGP: 0x0F52F982




        [[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: Strange behaviour of R?

Rainer M Krug-3
In reply to this post by Sigbert Klinke
Hi

> On 17 Jan 2020, at 08:33, Sigbert Klinke <[hidden email] <mailto:[hidden email]>> wrote:
>
> Hi,
>
> I wrote a function like
>
> test <- function(FUN, args) {
>  print(FUN)
>  FUN(args)
> }
>
> When I call it lieke this
>
> test(mean, 1:10)
> test(NULL, 1:10)
>
> then the second call still uses mean, although I set FUN to NULL. Is that ok?

Not for me - macOS, R 3.6.2


>
> Actually, I used something like
>
> test(mean, list(x=1:10, na.rm=TRUE))

Fails as expected,

Rainer

>
> which actually crashed R, but I can not reproduce it. Of course, when I replaced FUN(args) with do.call(FUN, args) then everything works fine.
>
> Sigbert
>
> --
> https://hu.berlin/sk <https://hu.berlin/sk>
> https://hu.berlin/mmstat3
>
> ______________________________________________
> [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.

--
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany)

Orcid ID: 0000-0002-7490-0066

Department of Evolutionary Biology and Environmental Studies
University of Zürich
Office Y34-J-74
Winterthurerstrasse 190
8075 Zürich
Switzerland

Office: +41 (0)44 635 47 64
Cell:       +41 (0)78 630 66 57
email:      [hidden email] <mailto:[hidden email]>
                [hidden email] <mailto:[hidden email]>
Skype:     RMkrug

PGP: 0x0F52F982





--
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany)

Orcid ID: 0000-0002-7490-0066

Department of Evolutionary Biology and Environmental Studies
University of Zürich
Office Y34-J-74
Winterthurerstrasse 190
8075 Zürich
Switzerland

Office: +41 (0)44 635 47 64
Cell:       +41 (0)78 630 66 57
email:      [hidden email]
                [hidden email]
Skype:     RMkrug

PGP: 0x0F52F982




        [[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: Strange behaviour of R?

Sigbert Klinke
Hi,

Am 17.01.20 um 08:42 schrieb Rainer M Krug:
 > Not for me - macOS, R 3.6.2

Sorry, I forgot to add: Ubuntu 18.04.3 LTS, R 3.6.2

Best Sigbert

--
https://hu.berlin/sk
https://hu.berlin/mmstat3

______________________________________________
[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: Strange behaviour of R?

K. Elo-2
Hi,

cannot reproduce, either, on my Linuxmint 19.3 + R 3.6.2.

Here the outputs:

--- snip ---

> test(mean, 1:10)
[1] 5.5
> test(NULL, 1:10)
NULL
Error in FUN(args) : could not find function "FUN"
> test(mean, list(x=1:10, na.rm=TRUE))
[1] NA
Warning message:
In mean.default(args) : argument is not numeric or logical: returning
NA

--- snip ---

Best,
Kimmo

pe, 2020-01-17 kello 09:21 +0100, Sigbert Klinke kirjoitti:
> Hi,
>
> Am 17.01.20 um 08:42 schrieb Rainer M Krug:
>  > Not for me - macOS, R 3.6.2
>
> Sorry, I forgot to add: Ubuntu 18.04.3 LTS, R 3.6.2
>
> Best Sigbert
>

______________________________________________
[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: Strange behaviour of R?

Martin Maechler
In reply to this post by Sigbert Klinke
>>>>> Sigbert Klinke
>>>>>     on Fri, 17 Jan 2020 09:21:59 +0100 writes:

    > Hi,
    > Am 17.01.20 um 08:42 schrieb Rainer M Krug:
    >> Not for me - macOS, R 3.6.2

    > Sorry, I forgot to add: Ubuntu 18.04.3 LTS, R 3.6.2

Sorry but that's very hard to believe, i.e.,
that such fundamental R behavior would depend on the exact OS
version ... unless you did compile (and link) R yourself *and*
are using somewhat peculiar compiler / linker / ... settings ?


Context :

        > I wrote a function like

        > test <- function(FUN, args) {
        >    print(FUN)
        >    FUN(args)
        > }

        > When I call it lieke this

        > test(mean, 1:10)
        > test(NULL, 1:10)

        > then the second call still uses mean, although I set FUN to NULL. Is
        > that ok?

definitely not ok, and also not reproducible for me.

______________________________________________
[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: Strange behaviour of R?

Duncan Murdoch-2
In reply to this post by Sigbert Klinke
On 17/01/2020 2:33 a.m., Sigbert Klinke wrote:

> Hi,
>
> I wrote a function like
>
> test <- function(FUN, args) {
>     print(FUN)
>     FUN(args)
> }
>
> When I call it lieke this
>
> test(mean, 1:10)
> test(NULL, 1:10)
>
> then the second call still uses mean, although I set FUN to NULL. Is
> that ok?

You probably have a function defined in your global environment that is
named FUN and acts like mean.

The general rule in R is that it only looks for objects of mode function
when trying to find something used as a function.  So in your second
case, when trying to evaluate FUN(args), R will look for a function
named FUN in the local evaluation frame, and won't find one:  FUN is
NULL there.  Then it will go to the environment of test, which is likely
the global environment, and look there.  That's where it probably found
the function.

For example, try this:

FUN <- function(...) print('FUN was called')


test <- function(FUN, args) {
    print(FUN)
    FUN(args)
}

test(NULL, 1:10)

Duncan Murdoch

>
> Actually, I used something like
>
> test(mean, list(x=1:10, na.rm=TRUE))
>
> which actually crashed R, but I can not reproduce it. Of course, when I
> replaced FUN(args) with do.call(FUN, args) then everything works fine.
>
> Sigbert
>

______________________________________________
[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: Strange behaviour of R?

Sigbert Klinke
Hi,

> You probably have a function defined in your global environment that is
> named FUN and acts like mean.

You are right.

Thanks Sigbert


--
https://hu.berlin/sk
https://hu.berlin/mmstat3

______________________________________________
[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: Strange behaviour of R?

Gabor Grothendieck
In reply to this post by Duncan Murdoch-2

Normally one uses match.fun to avoid such problems.
This will give the error shown even if FUN is defined in the global environment.

  test <- function(FUN, args) {
     FUN <- match.fun(FUN)
     print(FUN)
     FUN(args)
  }
  test(NULL, 1:10)
  ## Error in match.fun(FUN) : 'NULL' is not a function, character or symbol

On Fri, Jan 17, 2020 at 5:41 AM Duncan Murdoch <[hidden email]> wrote:

>
> On 17/01/2020 2:33 a.m., Sigbert Klinke wrote:
> > Hi,
> >
> > I wrote a function like
> >
> > test <- function(FUN, args) {
> >     print(FUN)
> >     FUN(args)
> > }
> >
> > When I call it lieke this
> >
> > test(mean, 1:10)
> > test(NULL, 1:10)
> >
> > then the second call still uses mean, although I set FUN to NULL. Is
> > that ok?
>
> You probably have a function defined in your global environment that is
> named FUN and acts like mean.
>
> The general rule in R is that it only looks for objects of mode function
> when trying to find something used as a function.  So in your second
> case, when trying to evaluate FUN(args), R will look for a function
> named FUN in the local evaluation frame, and won't find one:  FUN is
> NULL there.  Then it will go to the environment of test, which is likely
> the global environment, and look there.  That's where it probably found
> the function.
>
> For example, try this:
>
> FUN <- function(...) print('FUN was called')
>
>
> test <- function(FUN, args) {
>     print(FUN)
>     FUN(args)
> }
>
> test(NULL, 1:10)
>
> Duncan Murdoch
>
> >
> > Actually, I used something like
> >
> > test(mean, list(x=1:10, na.rm=TRUE))
> >
> > which actually crashed R, but I can not reproduce it. Of course, when I
> > replaced FUN(args) with do.call(FUN, args) then everything works fine.
> >
> > Sigbert
> >
>
> ______________________________________________
> [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.



--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[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.