Manfredo - 4 months ago 15

R Question

I have a function like

`dbh2vol <- function(dbh,hgt,...){`

if (missing(hgt)){

hgt = 2

cat("hgt is missing. Set to 2")

}

vol = dbh * hgt

return(vol)

}

if I then call it with

`Vol = dbh2vol(dbh=10,hgt=4)`

#40

everything is fine. If I have something like

`Vol = dbh2vol(dbh=10,param=4)`

#hgt is missing. Set to 2[1] 20

as expected. But if then I do

`Vol = dbh2vol(dbh=10,h=4)`

#40

`h=4`

`hgt=4`

Answer

what you observe is partial matching of named function arguments and as far as I know you can't disable it. see the r language definition for more informations https://cran.r-project.org/doc/manuals/R-lang.html#Argument-matching

maybe it is possible for you to put the hgt argument into the three dots and check that? of course you then loose the positional matching.

```
dbh2vol <- function(dbh, ...){
hgt <- list(...)$hgt
if (is.null(hgt)) {
hgt <- sqrt(dbh)
cat("hgt has been calculated with some_other_function")
}
vol = dbh * hgt
return(vol)
}
```