英文:
stacking multiple calls of library(…, include.only=…) doesn’t work
问题
自R 3.6版本开始,可以在加载包时使用library(…, include.only=…)
选择要加载的函数。
但有时我需要临时加载其他函数,而堆叠多个library(…, include.only=…)
不起作用:
> 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') : 找不到函数“str_sub”
> str_length('a')
[1] 1
到目前为止,解决方法是卸载包,然后重新加载:
> devtools::unload('stringr')
> library(stringr, include.only = c('str_length', 'str_sub'))
是否有其他方法可以在不重新加载包的情况下按需加载函数?
英文:
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?
答案1
得分: 2
一种选择是将各个函数分配到全局环境中:
str_length <- stringr::str_length
str_length('a')
# 1
str_sub <- stringr::str_sub
str_sub('a')
# "a"
str_length('a')
# 1
或者您可以使用 box:
box::use(stringr[str_length])
str_length('a')
# 1
box::use(stringr[str_sub])
str_sub('a')
# "a"
str_length('a')
# 1
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论