Q的替代措辞:哪些因素决定了R包安装的lazyload DB
步骤的壁时间
我正在开发一个R包,让用户更容易访问联盟数据。data
目录的大小为4.5G,其中所有对象都使用bzip2
进行压缩。有202个单独的.RData文件,压缩大小从133B到24MB不等。
当我安装软件包时,输出看起来像这样:
Downloading GitHub repo mypackage@HEAD
✓ checking for file ‘.../mypackage-1c6478a/DESCRIPTION’ ...
─ preparing ‘mypackage’:
✓ checking DESCRIPTION meta-information
─ checking for LF line-endings in source and make files and shell scripts
─ checking for empty or unneeded directories
─ building ‘mypackage_1.0.0.tar.gz’ (1.3s)
* installing *source* package ‘mypackage’ ...
** using staged installation
** R
** data
*** moving datasets to lazyload DB
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (mypackage)
*** moving datasets to lazyload DB
步骤耗时最长,约5分钟。是什么决定了这一步的暂停时间?对象数量?物体的大小?文件压缩?我能做些什么让它安装得更快吗?
EDIT:Ido希望所有的R对象都是惰性加载的,并且我希望它们都有附带的文档,所以我认为最好的做法是将.rda文件保存在data/
中。我特别想知道是否有一种方法可以在安装软件包时加快延迟加载步骤
我想您正在将数据添加到/data
文件夹中。另一种选择是将其置于inst/
之下;例如inst/extdata/
和,通过使用为system.file("extdata/mydataset.Rds", package="foo")
提供的路径,使可以通过包中的函数加载。然后,您需要调用那个helper函数来获取数据。
例如,对于一个特定的数据集,类似这样的东西:
loadPackageData <- function() {
readRDS(system.file("extdata/foo.Rds", package="bar"))
}
编辑:要加载多个数据集,可以执行以下操作:
bar_data_files <- list.files(system.file("extdata", package = "bar"),
pattern = "\.Rds$", full.names = TRUE)
barData <- setNames(lapply(bar_data_files, function(f) readRDS(f)),
tools::file_path_sans_ext(basename(bar_data_files)))
# Then to get the foo dataset:
barData$foo()
# Or view the dataset names:
names(barData)
自动完成也可以在这里工作。
一种更传统的方法可能是:
loadBarData <- function(dataset) {
bar_data_files <- list.files(".", #system.file("extdata", package = "bar"),
pattern = "\.Rds$", full.names = TRUE)
files_sans_ext <- tools::file_path_sans_ext(basename(bar_data_files))
if (missing(dataset)) {
print(files_sans_ext)
} else {
if (!(dataset %in% files_sans_ext)) {
stop("Could not match dataset", dataset)
} else {
readRDS(bar_data_files[match(dataset, files_sans_ext)])
}
}
}
loadBarData() # List all available datasets
loadBarData("foo") # Loads "foo" is foo is found
当然,如果你要求多个数据集(在向量中(等等,你可以对此进行扩展,并定义你想要发生什么(例如,在列表中组合,或者如果数据集相似,则将它们组合为一个(。
如果数据集被系统地命名,人们也可以想象替代方案。
在任何情况下,我们的想法都是让它加载在函数调用上。