18.8 Subsetting

  • Calls generally behave like lists.
  • Since they are list like, you can use standard subsetting tools.
  • The first element of the call object is the function to call, which is usually a symbol:
x[[1]]
#> read.table
is.symbol(x[[1]])
#> [1] TRUE
  • The remainder of the elements are the arguments:
as.list(x[-1])
#> [[1]]
#> [1] "important.csv"
#> 
#> $row.names
#> [1] FALSE
  • We can extract individual arguments with [[ or, if named, $:
x[[2]]
#> [1] "important.csv"
x$row.names
#> [1] FALSE
  • We can determine the number of arguments in a call object by subtracting 1 from its length:
length(x) - 1
#> [1] 2
  • Extracting specific arguments from calls is challenging because of R’s flexible rules for argument matching:
    • It could potentially be in any location, with the full name, with an abbreviated name, or with no name.
  • To work around this problem, you can use rlang::call_standardise() which standardises all arguments to use the full name:
rlang::call_standardise(x)
#> Warning: `call_standardise()` is deprecated as of rlang 0.4.11
#> This warning is displayed once every 8 hours.
#> read.table(file = "important.csv", row.names = FALSE)
  • But If the function uses … it’s not possible to standardise all arguments.
  • Calls can be modified in the same way as lists:
x$header <- TRUE
x
#> read.table("important.csv", row.names = FALSE, header = TRUE)