Library/package development - message when loading

2019-01-22 22:03发布

问题:

is there any way to display a message when a user loads library(myCustomLibrary)? Upon loading, I want to display a message that tells the user how to run all the test functions.

回答1:

Yes. You can use the .onLoad, .onAttach, or .First.lib functions to do whatever you want when the package is loaded. I suggest looking at the help for those functions. You would use .onLoad with a namespace, and .First.lib without.

One convention is that people will frequently put these commands in a separate zzz.R file, which is just used for package related code.



回答2:

Quick points:

  • if your package has a NAMESPACE, then .onLoad() is where you do this

  • if your package does not have NAMESPACE, then .First.lib() is where you do this

  • either way, use packageStartupMessage() instead of cat() so that users have a choice of suppressing this.



标签: r package