Hi, i'm tryng to build a function that take some input from user and if the
user doesn't provide that inputs the function should set a deafult value. I
have taken as example a function that i found in the package dlm but with
the same code i receive an error (the component is missing...). According to
the code below R seems to set 1 to "phi" instead of "p" !
The only way to
solve this problem that i found is to change "p" to "P", but
this is clearly
the wrong solution ! There is some other way to set default value with that
function structure ? thanks
------------
Stem.Skeleton <- function(...) {
if (nargs() == 1)
x <- as.list(...)
else
x <- list(...)
## required components
comp <- c("phi", "p", "K")
if(is.null(list(x$p))) x$p = 1
compInd <- match(comp, names(x))
if (any(is.na(compInd)))
stop(paste("Component(s)", paste(comp[is.na(compInd)],
collapse=", "
),
"is (are) missing"))
....
---------
Marco
[[alternative HTML version deleted]]
<R-help@lists.r-project.org>Hi, i'm tryng to build a function that
take
some input from user and if the user doesn't provide that inputs the
function should set a deafult value. I have taken as example a function that
i found in the package dlm but with the same code i receive an error (the
component is missing...). According to the code below R seems to set 1 to
"phi" instead of "p" ! The only way to solve this problem
that i found is to
change "p" to "P", but this is clearly the wrong solution !
There is some
other way to set default value with that function structure ?
Thanks,
Marco
------------
Stem.Skeleton <- function(...) {
if (nargs() == 1)
x <- as.list(...)
else
x <- list(...)
## required components
comp <- c("phi", "p", "K")
if(is.null(list(x$p))) x$p = 1
compInd <- match(comp, names(x))
if (any(is.na(compInd)))
stop(paste("Component(s)", paste(comp[is.na(compInd)],
collapse=", "
),
"is (are) missing"))
....
---------
[[alternative HTML version deleted]]
You should consult "An Introduction to R", section 11, "Writing
your own
functions", along with any beginner books on R.
http://www.r-project.org/doc/bib/R-books.html
test <- function(x = 2) {
x^2
}
test() ## will give 4
test(3) ## will give 9
Marco Salvi wrote:> Hi, i'm tryng to build a function that take some input from user and if
the
> user doesn't provide that inputs the function should set a deafult
value. I
> have taken as example a function that i found in the package dlm but with
> the same code i receive an error (the component is missing...). According
to
> the code below R seems to set 1 to "phi" instead of "p"
! The only way to
> solve this problem that i found is to change "p" to
"P", but this is clearly
> the wrong solution ! There is some other way to set default value with that
> function structure ? thanks
>
> ------------
>
> Stem.Skeleton <- function(...) {
>
> if (nargs() == 1)
>
> x <- as.list(...)
>
> else
>
> x <- list(...)
>
>
> ## required components
>
> comp <- c("phi", "p", "K")
>
> if(is.null(list(x$p))) x$p = 1
>
> compInd <- match(comp, names(x))
>
> if (any(is.na(compInd)))
>
> stop(paste("Component(s)", paste(comp[is.na(compInd)],
collapse=", "
> ),
>
> "is (are) missing"))
>
> ....
> ---------
>
>
> Marco
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.