[R] Different output for "if else" and "ifelse" that I don't understand

William Dunlap wdunlap at tibco.com
Fri Apr 28 19:43:44 CEST 2017


Thank you - I did mean tryCatch.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, Apr 28, 2017 at 10:01 AM, Bert Gunter <bgunter.4567 at gmail.com>
wrote:

> Typo: last line should be
>
> Even better, use
>   p3p <- tryCatch(list(...), error=function(e) list())
>
>
> 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 Fri, Apr 28, 2017 at 8:34 AM, William Dunlap via R-help
> <r-help at r-project.org> wrote:
> > ifelse's vectorization messes this up.
> >
> > You could replace your original
> >   if (class(try(list(...), silent=TRUE))=="try-error") p3p <- list() else
> > p3p <- list(...)
> > with
> >   p3p <- if (class(try(list(...), silent=TRUE))=="try-error") list() else
> > list(...)
> > instead of using ifelse, since the return value of 'if ... else ...' is
> the
> > value of whichever branch was taken.
> >
> > Even better, use
> >   p3p <- try(list(...), error=function(e) list())
> >
> >
> > Bill Dunlap
> > TIBCO Software
> > wdunlap tibco.com
> >
> > On Fri, Apr 28, 2017 at 2:21 AM, Marc Girondot via R-help <
> > r-help at r-project.org> wrote:
> >
> >> Dear list-members,
> >>
> >> During the test phase of a function, I run it interactively (in Rstudio)
> >> and the ... produces an error. Then I use this to read it:
> >>
> >> if (class(try(list(...), silent=TRUE))=="try-error") p3p <- list() else
> >> p3p <- list(...)
> >>
> >> It works fine; interactively I will get
> >>
> >> > if (class(try(list(...), silent=TRUE))=="try-error") p3p <- list()
> else
> >> p3p <- list(...)
> >> > p3p
> >> list()
> >>
> >> and within a function I will get a list with the ... value. Perfect.
> >>
> >> I wanted to simplify the line by doing:
> >>
> >> p3p <- ifelse(class(try(list(...), silent=TRUE))=="try-error", list(),
> >> list(...))
> >>
> >> In interactive mode, it works but within a function, I don't get the
> names
> >> of the parameters. I don't understand the logic behind this difference.
> >> Have you an idea ?
> >>
> >> Thanks
> >>
> >> Marc
> >>
> >>
> >> > Try1 <- function(...) {
> >> +   if (class(try(list(...), silent=TRUE))=="try-error") p3p <- list()
> >> else p3p <- list(...)
> >> +   return(p3p)
> >> + }
> >> > Try1(k=100)
> >> $k
> >> [1] 100
> >>
> >> > Try1()
> >> list()
> >> > Try2 <- function(...) {
> >> +   p3p <- ifelse(class(try(list(...), silent=TRUE))=="try-error",
> list(),
> >> list(...))
> >> +   return(p3p)
> >> + }
> >> > Try2(k=100)
> >> [[1]]
> >> [1] 100
> >>
> >> > Try2()
> >> [[1]]
> >> NULL
> >>
> >> ______________________________________________
> >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide http://www.R-project.org/posti
> >> ng-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list