Manfredo Manfredo - 18 days ago 5
R Question

R missing arguments in function input not matched by name

I have a function like

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

if (missing(hgt)){
hgt = 2
cat("hgt is missing. Set to 2")
vol = dbh * hgt

if I then call it with

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

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)

is interpreted as
. Why and how can I avoid such a behavior, that is how can the arguments be matched exactly by their names?


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

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