1

When creating a Python package, you can create "commands" by using the entry_points argument of setup (see here). I am wondering if it is possible to do the same when creating an R package.

For example, after running

install.packages("mypackage")

it would be nice to be able to use

mypackage_clitool arg1 arg2

without needing to use Rscript. Is it possible to have an entry point like this in an R package?

I couldn't find the answer anywhere, but for reference, here are some resources that partially address this topic:

Answer about turning a CLI into an R package - the opposite direction

Tutorial explaining how to run R code from the command line, but not how to install a CLI tool with an R package

CLI library - I couldn't find any instructions for installing via a package here

user82841
  • 35
  • 3
  • R isn't meant to work that way. You'd have to write some external script interface layer to call package functions for you. You can pass a code chunk to Rscript from the command line. For example `Rscript -e "mypackage::run()"` to run a function from your package. That function can read additional command line arguments via `commandArgs()` should it choose to. But you are still going through `Rscript` – MrFlick Aug 01 '20 at 06:34
  • I see. If I do write an external script interface layer, is there any way I could have instructions triggered when `install.packages` is called - maybe to register the script interface with the OS? – user82841 Aug 01 '20 at 06:40
  • I don’t think CRAN would allow anything like that. But if you just to create your own private package that does something like that you could maybe do something in the Makefile that can run when a package is installed from source. But you really can’t do much with a prebuilt binary package when install.packages is called. R tries to isolate the stuff it does from the OS in a platform independent way. – MrFlick Aug 01 '20 at 08:08
  • What about Bioconductor? The package is mainly for biological applications, so that could be another place for the package – user82841 Aug 01 '20 at 12:09

1 Answers1

1

You could write scripts and put them in a inst/scripts directory in the package; they'll be installed with the package into scripts in the package directory. Your script could be something like this example from ?Rscript for Unix-alikes:

#! /path/to/Rscript --vanilla --default-packages=utils
args <- commandArgs(TRUE)
res <- try(install.packages(args))
if(inherits(res, "try-error")) q(status=1) else q()

On Windows, it would need to have a different format, because the usual shell there doesn't support #! scripts.

You could write code to put the scripts directory (i.e. the result of system.file("scripts", package = "mypackage")) on the user's PATH, but that would only last for the current R session, and would only be seen from commands launched from R. You could write instructions to the user to put it on the user's PATH themselves. Only the latter would be acceptable in a CRAN package.

user2554330
  • 37,248
  • 4
  • 43
  • 90