As of R 3.6 it’s possible to choose functions when loading a package with library(…, include.only=…)
.
But sometimes I’d need other functions on the fly, and stacking multiples library(…, include.only=…)
doesn’t work:
> library(stringr, include.only = 'str_length')
> str_length('a')
[1] 1
> library(stringr, include.only = 'str_sub')
> str_sub('a')
Error in str_sub("a") : could not find function "str_sub"
> str_length('a')
[1] 1
so far the workaround is to unload package then reload
> devtools::unload('stringr')
> library(stringr, include.only = c('str_length', 'str_sub'))
is there another way to load functions on demand without reloading package?
One option would be to assign individual functions into the global environment:
str_length <- stringr::str_length
str_length('a')
# 1
str_sub <- stringr::str_sub
str_sub('a')
# "a"
str_length('a')
# 1
Or you could use box:
box::use(stringr[str_length])
str_length('a')
# 1
box::use(stringr[str_sub])
str_sub('a')
# "a"
str_length('a')
# 1