Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R package with code that only runs once (per installation)

Tags:

r

analytics

I'd like to create an R package that, upon installation, displays contact information for the maintainer and ask the user for permission to count them in our list of installations. It would also be acceptable to have the code run the first time the user calls one of our functions, instead of immediately on installation. Either way, this message should only appear once ever (unless the user reinstalls / updates the package).

What I've considered:

  • I know how to include a dataset for internal use, but I don't know how to change that data permanently.
  • We could set an environment variable / app setting, but I don't know if there's a way to make that persist after the end of the session.
  • Using an external service / server would be excessively heavyweight, and wouldn't allow users who don't want to be tracked to turn off the message.

Is there a good way to do this?

like image 349
octern Avatar asked Dec 06 '25 19:12

octern


1 Answers

This can run more than once but only within a limited time window so perhaps it is good enough.

Add this code to your package and it will issue the message any time the package is loaded within 7 days of installation and thereafter it will not issue the message again until the package is updated.

It works by comparing the time the install files were created to the current time. It does not require write permissions to any directory, only read, so it should work generally.

.onLoad <- function(libname, pkgname) {
   ctime <- file.info(find.package(pkgname, libname))$ctime
   if (difftime(Sys.time(), ctime, unit = "day") < 7)
      packageStartupMessage("This msg will go away one week after installing this package")
}
like image 57
G. Grothendieck Avatar answered Dec 08 '25 11:12

G. Grothendieck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!