简体   繁体   English

我如何要求R从用户定义的程序包运行特定功能以在每次加载该程序包时运行?

[英]How do I ask R to run a specific function from a user defined package to run everytime I load that package?

I've created a package mypack with only one r script in the R directory of the project, hello.R which basically prints our Hello World when loaded and run. 我创建了一个包mypack与项目,hello.R加载和运行时基本打印我们的Hello World中的R目录只有一个R脚本。 Now i need to add some lines of code to a script (ex- hide.R ) so that the contents of the function hello.R is not easily visible to a not so experience user. 现在,我需要在脚本中添加一些代码行(ex- e.R ),这样对于一个不太有经验的用户来说,不容易看到函数hello.R的内容。 I need this hide.R to run first each time the package mypack is loaded. 我需要这个hide.R到每个包时首先运行mypack被加载。 Can somebody give me a full working example for this? 有人可以给我一个完整的工作示例吗?

Note: outside a package, if I have a function foo, then the following makes it a little difficult for a normal user to get the function definition easily 注意:在包之外,如果我有一个函数foo,那么以下内容使普通用户难以轻松获得函数定义

srcfile <- srcfilecopy("", "Don't cheat")
srcref <- srcref(srcfile, c(1,1,1,12))
attr(foo, "srcref") <- srcref

Ultimately, once a user loads my package, he/she should not be easily able to get the contents of my function inside the package by typing out the function name. 最终,一旦用户加载了我的包,他/她应该不容易通过键入函数名称来获取我的函数的内容。 This is for educational purposes. 这是出于教育目的。

You can define a function called .onLoad inside your package. 您可以在包中定义一个名为.onLoad的函数。 See docs here: https://stat.ethz.ch/R-manual/R-devel/library/base/html/ns-hooks.html 在此处查看文档: https : //stat.ethz.ch/R-manual/R-devel/library/base/html/ns-hooks.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM