Problem

You want to extract parts of a formula for further use.

Solution

You can index into the formula object as though it were a list, using the [[ operator.

f <- y ~ x1 + x2

# Take a look at f
str(f)
#> Class 'formula'  language y ~ x1 + x2
#>   ..- attr(*, ".Environment")=<environment: 0x1e46710>

# Get each part
f[[1]]
#> `~`
f[[2]]
#> y
f[[3]]
#> x1 + x2

# Or view the whole thing as a list
as.list(f)
#> [[1]]
#> `~`
#> 
#> [[2]]
#> y
#> 
#> [[3]]
#> x1 + x2
#> 
#> <environment: 0x1e46710>

For formulas that have nothing on the left side, there are only two elements:

f2 <- ~ x1 + x2
as.list(f2)
#> [[1]]
#> `~`
#> 
#> [[2]]
#> x1 + x2
#> 
#> <environment: 0x1e46710>

Each of the elements of the formula is an symbol or language object (which consists of multiple symbols:

str(f[[1]])
#>  symbol ~
str(f[[2]])
#>  symbol y
str(f[[3]])
#>  language x1 + x2

# Look at parts of the langage object
str(f[[3]][[1]])
#>  symbol +
str(f[[3]][[2]])
#>  symbol x1
str(f[[3]][[3]])
#>  symbol x2

You can use as.character() or deparse() to convert any of these to strings. deparse() can give a more natural-looking result:

as.character(f[[1]])
#> [1] "~"
as.character(f[[2]])
#> [1] "y"

# The language object gets coerced into a string that represents the parse tree:
as.character(f[[3]])
#> [1] "+"  "x1" "x2"

# You can use deparse() to get a more natural looking string
deparse(f[[3]])
#> [1] "x1 + x2"
deparse(f)
#> [1] "y ~ x1 + x2"

The formula object also captures the environment in which it was called, as we saw earlier when we ran str(f). To extract it, use environment():

environment(f)
#> <environment: 0x1e46710>