docopt.R icon indicating copy to clipboard operation
docopt.R copied to clipboard

Distributing docopt scripts

Open cysouw opened this issue 9 years ago • 3 comments

I'd like to add docopt scripts to my packages, so people working in different environments can use my R-implementations without needing to work inside R themselves. They can then access the routines via a terminal.

R offers a nice option to add executables inside the directory "exec" in an R package. On *nix platforms, these are made executable on install, so they can be immediately used. This seems the primary place to distribute docopt-scripts.

However, my question is how to link these scripts. Their location is rather hidden and difficult to find for non-R-users (within R, you can use the R-function find.package() ).

What would be best-practice?

cysouw avatar May 03 '16 11:05 cysouw

Some ideas on best-practices. I usually either ask my users to manually download executables files of interest from my github repos, mark them executable, and to then either use them locally from the repo folder and/or to copy them to $HOME/bin/ -- simple but clunky. I know some people have packaged Rscripts in the debian "apt" format so users using Debian/Ubuntu can install them using apt install program -- convenient for users but not for developers. Alternatively you can distribute them in R packages and ask your users to install and use a simple shell script abstraction like Rbin::

$ Rbinlist Rpackagename
$ Rbin Rpackagename execfilename [arguments_passed_to_execfilename]

Kind of klunky but you can also give them advice on your README on how add an alias in their .bashrc shell configuration files so that it appears the program is on their path so they can simply type myprogram in their shell::

alias myprogram="Rbin mypackage myprogram"

You can also tell them instructions on how to copy them over to somewhere on their path $HOME/bin/ after package installation::

> file.copy(system.file("exec/myprogram", package="mypackage"), "~/bin/myprogram")

trevorld avatar Apr 16 '18 19:04 trevorld

Thanks for the tip! My original comment was from almost two years ago: in the meantime I have settled for the following procedure:

The docopt scripts are added to the exec file in my packages (see for example https://github.com/cysouw/qlcData/). In this way, the whole install and update procedures work through CRAN.

Basically each individual script gives a bash-interface to single functions in the package (e.g. tokenize). In the help file of these functions I then add a note explaining that this function can also be accessed within bash with the following explanation:


There is a bash-executable distributed with this package (based on the docopt package) that let you use this function directly in a bash-terminal. The easiest way to use this executable is to softlink the executable to some directory in your bash PATH, for example /usr/local/bin. To softlink the function tokenize to this directory, use something like the following in your bash terminal:

ln -is `Rscript -e 'cat(file.path(find.package("qlcData"), "exec", "tokenize"))'` /usr/local/bin

then typing tokenize --help in the terminal should give help on this function.

cysouw avatar Apr 17 '18 06:04 cysouw

In case someone wants to give instructions in R the equivalent R command for the above is::

$ sudo R
> file.symlink(system.file("exec/tokenize", package="qlcData"), "/usr/local/bin")

For users without admin permissions (but were able to install your package locally) would be better to suggest::

> file.symlink(system.file("exec/tokenize", package="qlcData"), "~/bin")

Everything in the above commands should work cross-platform (for more recent versions of Windows) except the location of where to place the symbolic link (although the above two locations should work on most *Nix systems).

trevorld avatar Apr 17 '18 07:04 trevorld