我在用盒装Package.在我的项目中,我有一个Functions文件夹,其中保存了两个函数:hello.r和good bye.r
我可以使用box包加载它们,方法是:
box::use(functions/hello)
box::use(functions/goodbye)
问题是我可能在Functions文件夹中有几十个.R文件(每个函数一个.R文件).
有没有一种方法可以一次加载所有的.R文件(同时仍然使用box包),而不必多次重复box::use函数?
我在用盒装Package.在我的项目中,我有一个Functions文件夹,其中保存了两个函数:hello.r和good bye.r
我可以使用box包加载它们,方法是:
box::use(functions/hello)
box::use(functions/goodbye)
问题是我可能在Functions文件夹中有几十个.R文件(每个函数一个.R文件).
有没有一种方法可以一次加载所有的.R文件(同时仍然使用box包),而不必多次重复box::use函数?
由于box::use
使用非标准求值(NSE),因此必须将字符串转换为未求值的语言对象:
rm(list = ls())
ls()
# character(0)
rex <- "\\.[rR]$"
fld <- "functions"
fs::dir_tree(fld)
# functions
# ├── goodbye.R
# └── hello.R
fns <- list.files(fld, rex) |> ## read all files in the folder
gsub(rex, "", x = _) |> ## drop extension
paste(fld, . = _, sep = "/") |> ## pre-pend folder
lapply(str2lang) ## transform string into language object
## call box::use with the the newly creataed list
do.call(box::use, fns)
ls()
# [1] "fld" "fns" "goodbye" "hello" "rex"
Update based on Friede's tip to use 100