R: How to run some code on load of package?
There is usually a "processing function" (traditionally called zzz.R) with
tasks to be performed when the package is loaded, such as loading libraries
and compiled code. For example you can create a zzz.R file where you create this function:
.onLoad <- function(libname, pkgname){
x <- rnorm(10) ## dummy example
}
Automatically run a function when loading a R package
In case you want to run something when R starts:
Start RStudio, and run the following to create .Rprofile
file in your home directory:
file.edit("~/.Rprofile")
Put the following function inside that file:
.First <- function(){
cat("Hello!") # startup message
require(data.table)
# or whatever packages you want to load
# or if you want to run a function in a file
if(file.exists("~/myfile.r")){
source("~/myfile.r")
myfunc()
}
}
Save it. Done!
As to OP's edit
In case you want to run something when your package is loaded, you can use .onLoad
and .onAttach
functions. For example:
.onAttach <- function(libname, pkgname) {
# to show a startup message
packageStartupMessage("This is my package, enjoy it!")
}
.onLoad <- function(libname, pkgname) {
# something to run
}
How to load packages in R automatically?
Put library(foo)
in your .Rprofile
file or set R_DEFAULT_PACKAGES
: see ?Rprofile
...
In particular (because ?Rprofile
is long and potentially intimidating):
If you want a different set of packages than the default ones when you
start, insert a call to ‘options’ in the ‘.Rprofile’ or
‘Rprofile.site’ file. For example, ‘options(defaultPackages =
character())’ will attach no extra packages on startup (only the
‘base’ package) (or set ‘R_DEFAULT_PACKAGES=NULL’ as an environment
variable before running R). Using ‘options(defaultPackages = "")’ or
‘R_DEFAULT_PACKAGES=""’ enforces the R system default.
Since you probably do want all of the default packages loaded, and then extra ones in addition (rather than, say, not loading some of the default packages), you can either put
library("mypackage1")
library("mypackage2")
[etc.]
or using options(defaultPackages=...)
:
options(defaultPackages=c(getOption("defaultPackages"),
"mypackage1","mypackage2", ... [etc.]))
in your .Rprofile
to append your desired packages to the standard defaults.
edit (copied from comment) re getting this to work in Rstudio:
http://rstudio.org/docs/using/workspaces suggests that Rstudio executes .Rprofile
and then "Performs the other actions described in R Startup [ http://stat.ethz.ch/R-manual/R-patched/library/base/html/Startup.html ]" (which is the same as ?Rprofile
). It is ambiguous whether it looks at Rprofile.site
or not.
edit #2: according to comment below, it does work with a recent version of Rstudio.
How to make variable available to namespace at loading time
Following the approach in this answer to a related question, you can change your .onLoad()
function like so:
.onLoad <- function(libname, pkgname) {
variable <- some_function()
assign("variable", variable, envir = parent.env(environment()))
}
Then you can access variable
without attaching the package using MyRPackage:::variable
. I don't know what you do with some_function()
, so I tried the following with a dummy package:
.onLoad <- function(libname, pkgname) {
variable <- 42
assign("variable", variable, envir = parent.env(environment()))
}
And in a fresh R session the result was
> MyRPackage:::variable
[1] 42
Further explanation
From Hadley Wickham's Advanced R:
There are four special environments:
...
- The environment() is the current environment.
...
You can list the bindings in the environment’s frame with ls() and
see its parent with parent.env().
So, if we modify the .onLoad()
function further, we can see this in action:
.onLoad <- function(libname, pkgname) {
print(environment()) # For demonstration purposes only;
print(parent.env(environment())) # Don't really do this.
variable <- 42
assign("variable", variable, envir = parent.env(environment()))
}
Then starting an R session results in
<environment: 0x483da88>
<environment: namespace:MyRPackage>
being printed to the console at the start of the session. This allows you to assign variable
in the environment namespace:MyRPackage
even though trying assign("variable", variable, envir = namespace:MyRPackage)
would result in the error
Error: package or namespace load failed for ‘MyRPackage’:
.onLoad failed in loadNamespace() for 'MyRPackage', details:
call: get(name, envir = ns, inherits = FALSE)
error: object 'namespace' not found
when installing the package.
Load a package only when needed in R package
You can use Suggests, and in the function that needs the package you can add code to require()
said package and if not available throw an error. An example I am familiar with, the vegan package, has, in its DESCRIPTION
Depends: permute
Suggests: MASS, mgcv, lattice, cluster, scatterplot3d, rgl, tcltk
and on loading the package we have:
R> require(vegan)
Loading required package: vegan
Loading required package: permute
This is vegan 1.90-0
and sessionInfo()
reports that none of the Suggested packages has yet been loaded/attached etc:
R> sessionInfo()
R version 2.13.1 Patched (2011-07-29 r56550)
Platform: x86_64-unknown-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_GB.utf8 LC_NUMERIC=C
[3] LC_TIME=en_GB.utf8 LC_COLLATE=en_GB.utf8
[5] LC_MONETARY=C LC_MESSAGES=en_GB.utf8
[7] LC_PAPER=en_GB.utf8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] vegan_1.90-0 permute_0.5-0
loaded via a namespace (and not attached):
[1] tools_2.13.1
Loading R package inside R script
The solution is loading the package by require()
require(dplyr)
Related Topics
Difference Between If() and Ifelse() Functions
Counting Number of Instances of a Condition Per Row R
How to Stack Error Bars in a Stacked Bar Plot Using Geom_Errorbar
Ggplot2: Connecting Points in Polar Coordinates with a Straight Line 2
Aggregate and Reshape from Long to Wide
Stepwise Regression Using P-Values to Drop Variables with Nonsignificant P-Values
R Package That Automatically Uses Several Cores
Return Data Subset Time Frames Within Another Timeframes
How to Copy and Paste Data into R from the Clipboard
Moving Columns Within a Data.Frame() Without Retyping
How to Maintain Size of Ggplot with Long Labels
R Scatter Plot: Symbol Color Represents Number of Overlapping Points
How to Extract Just the Number from a Named Number (Without the Name)
Dplyr Issues When Using Group_By(Multiple Variables)