Skip to contents

Check if an element of a pairlist is missing.

Usage

missing_arg(arg)

Arguments

arg

the object to test.

Details

The argument passed to missing_arg is typically an element of a pairlist or the list produced by alist(). missing_arg returns TRUE if it is missing and FALSE otherwise.

Objects of type pairlist come up at R level almost exclusively as the formal arguments of functions. missing_arg can be useful when they are manipulated programmatically.

Value

TRUE or FALSE

Author

Georgi N. Boshnakov

Examples

lmargs <- formals(lm)
class(lmargs) # pairlist
#> [1] "pairlist"
missing_arg(lmargs$data)
#> [1] TRUE
## which arguments of lm() have no (explicit) defaults?
sapply(lmargs, missing_arg)
#>     formula        data      subset     weights   na.action      method 
#>        TRUE        TRUE        TRUE        TRUE        TRUE       FALSE 
#>       model           x           y          qr singular.ok   contrasts 
#>       FALSE       FALSE       FALSE       FALSE       FALSE       FALSE 
#>      offset         ... 
#>        TRUE        TRUE 

## This gives an error:
## pairlist(x = 3, y = , z = 5)

## an example with alist()
pl2 <- alist(a = "", b = , c = 3)
class(pl2) # list
#> [1] "list"
## this shows that 'b' is missing, 'a' and 'c' are not:
sapply(pl2, missing_arg) # FALSE  TRUE FALSE
#>     a     b     c 
#> FALSE  TRUE FALSE 
## superficially, 'b' is equal to the empty string:
pl2[[2]]
#> 
sapply(pl2, function(x) x == "") # TRUE  TRUE FALSE
#>     a     b     c 
#>  TRUE  TRUE FALSE 

## with pairlist the results are the same:
g <- function(a = "", b, c = 3) NULL
a.g <- formals(g)
class(a.g) # pairlist
#> [1] "pairlist"
sapply(a.g, missing_arg) # FALSE  TRUE FALSE
#>     a     b     c 
#> FALSE  TRUE FALSE 
a.g[[2]]
#> 
sapply(a.g, function(x) x == "") # TRUE  TRUE FALSE
#>     a     b     c 
#>  TRUE  TRUE FALSE