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@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@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@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.