包括“哈希表";在包装中 [英] Including a "Hash Table" in a package

查看:78
本文介绍了包括“哈希表";在包装中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在整理一个已经使用了将近一年的软件包.我有一个所谓的哈希表,它是一个音节查找功能所需要的.哈希表实际上只是一个查找表的环境(我想我不是计算机专家).您可以在下面看到我创建它的功能.我有一个数据集DICTIONARY(大约20,000个字),将在加载程序包时加载.我还将这个DICTIONARY传递给哈希函数以在加载包时创建一个新环境.像env <- hash(DICTIONARY)这样的htis是我现在如何加载环境.如何在加载程序包时使函数在启动时运行,以便为使用我的程序包的人创建这个新环境?

I am in the process of putting together a package I've been working on for almost a year now. I have what I call a hash table that a syllable look up function requires. The hash table is really just an environment (I think I'm not computer whiz) that's a look up table. You can see the function I create it with below. I have a data set DICTIONARY(about 20,000 words) that will load when the package is loaded. I also what this DICTIONARY to be passed to the hash function to create a new environment when the package is loaded; something like env <- hash(DICTIONARY) as htis is how I load the environment now. How do I make a function run on start up when the package is loaded so that this new environment is created for those using my package?

hash <- function(x, type = "character") {
    e <- new.env(hash = TRUE, size = nrow(x), parent = emptyenv())
    char <- function(col) assign(col[1], as.character(col[2]), envir = e)
    num <- function(col) assign(col[1], as.numeric(col[2]), envir = e)
    FUN <- if(type=="character") char else num
    apply(x, 1, FUN)
    return(e)
}

#currently how I load the environment with the DICTIONARY lookup table
env <- hash(DICTIONARY) 

如果有帮助,请担任DICTIONARY的负责人:

Here's the head of DICTIONARY if it's helpful:

    word syllables
1     hm         1
2    hmm         1
3   hmmm         1
4   hmph         1
5  mmhmm         2
6   mmhm         2
7     mm         1
8    mmm         1
9   mmmm         1
10   pff         1

许多人可能会想:这取决于用户确定他们是否要加载环境".有效的观点,但此计划的目标读者是扫盲领域的人们.在该领域中,R用户并不多,因此我必须使它尽可能易于使用.只是想弄清楚为什么我要这样做的理念,所以不要成为争论的焦点.

Many of you may be thinking "This is up to the user to determine if they want the environment loaded". Valid point but the intended audience of this package is people in the literacy field. Not many in that field are R users and so I have to make this thing as easy as possible to use. Just wanted to get out the philosophy of why I want to do this, out there so that it doesn't become a point of contention.

先谢谢您. (请注意,我查看了本手册( LINK ),但似乎找不到有关此主题的任何信息)

Thank you in advance. (PS I've looked at this manual (LINK) but can't seem to locate any info about this topic)

根据Andrei的建议,我认为会是这样吗?但是我不确定.是否在加载包中的所有其他功能和数据集之后加载?这些东西让我有些困惑.

Per Andrei's suggestion i think it will be something like this? But I'm not sure. Does this load after all the other functions and data sets in the package load? This stuff is a little confusing to me.

.onLoad <- function(){
   env <- hash(DICTIONARY)
}

推荐答案

如果哈希值很少更改(从您的问题描述来看,情况似乎如此),则将哈希值另存为您的软件包源代码树

If the hash is going to change infrequently (this seems like the case, from your problem description), then save the hash into your package source tree as

save(env, file="<my_pkg>/R/sysdata.rda")

安装软件包后,在名称空间my_pkg:::env中将提供env.请参见编写R扩展名"的1.1.3节.您可能有一个脚本,例如在"/inst/scripts/make_env.R"中创建了env,并且您作为开发人员在需要更新env的极少数情况下使用.

After installing the package, env will be available inside the name space, my_pkg:::env. See section 1.1.3 of "Writing R Extensions". You might have a script, say in "/inst/scripts/make_env.R" that creates env, and that you as the developer use on those rare occasions when env needs to be updated.

另一种可能性是散列发生变化,但仅在软件包安装上才发生变化.然后的解决方案是编写在软件包安装时评估的代码.因此,在文件/R/env.R中写一些类似的内容

Another possibility is that the hash changes, but only on package installation. Then the solution is to write code that is evaluated at package installation. So in a file /R/env.R write something along the lines of

env <- local({
    localenv <- new.env(parent=emptyenv())
    ## fill up localenv, then return it
    localenv[["foo"]] = "bar"
    localenv
})

.onLoad解决的可能性是每次加载软件包时数据都会更改,例如,因为它正在从某个在线源中检索更新.

The possibility solved by .onLoad is that the data changes each time the package is loaded, e.g., because it is retrieving an update from some on-line source.

env <- new.env(parent=emptyenv())

.onLoad <- function(libname, pkgname)
{
    ## fill up env
    env[["foo"]] = "bar"
}

这篇关于包括“哈希表";在包装中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆