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/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to