[R-pkg-devel] Simple way to run code during package install

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Tue Nov 19 15:20:33 CET 2024


I think I have a solution.  Roxygen2 allows this option to be specified 
in the DESCRIPTION file:

   Roxygen: list(markdown = TRUE, load = "installed")

With the `load = "installed"` setting, it doesn't try to run the .R files.

Duncan Murdoch

On 2024-11-19 9:01 a.m., Duncan Murdoch wrote:
> I have some code in rgl and rgl2gltf that should be run during the
> package install process, and not later.  (It's a call to
> rgl::makeDependency, which does some stuff with Javascript files and
> writes into the package installation directory, saving the paths of the
> files it wrote.)
> 
> Currently rgl handles this by simply placing calls to makeDependency in
> one of the .R files, relying on the fact that they are run at install
> time and not later. It creates rgl:::rglDependency and another similar
> object.  That works fine.
> 
> Tomas just pointed out to me that rgl2gltf is trying to modify its
> installation location after installation.  This is happening because it
> calls makeDependency in a function the user can call, and that's bad, so
> I'm fixing it.
> 
> The problem is that rgl2gltf uses Roxygen for documentation, and Roxygen
> executes all the code in the package when it is producing Rd files.
> (Normally this just creates the functions in the package, it doesn't
> actually call them, so it's harmless.  But calling makeDependency from
> Roxygen fails because I wasn't expecting that.)
> 
> So I'd like to ask for suggestions for the simplest way to deal with
> this in rgl2gltf.
> 
> 1. One choice is to fix rgl::makeDependency so it can tell that it is
> being run after installation, and stop trying to write any files.  It
> should just return the object.  I can probably do this, but I've just
> had two rgl releases in the last few weeks, so I'd prefer to limit the
> changes to rgl2gltf.
> 
> 2. I can use pkgload::is_loading() to detect the load_all() call from
> Roxygen and not run makeDependency in that situation.  But that forces a
> dependence on pkgload, and that's not very appealing.
> 
> 3. Maybe I can mark the file that contains the makeDependency call so
> that Roxygen ignores it.  Is that possible?
> 
> 4. I can write a Makevars file that runs makeDependency.  That seems
> like a pretty heavyweight solution, especially since I need to save the
> result of the call so it is available later.
> 
> Does anyone else have this problem, and a nice solution for it?
> 
> Duncan Murdoch
>



More information about the R-package-devel mailing list