DEV Community

Colin Fay
Colin Fay

Posted on • Originally published at colinfay.me on

2 1

Create a CLI for R with npm

How to build a CLI for R, with npm.

Background

This blog post was triggered by a discussion on Twitter with MartinSkarzynski, who was looking for a way to build a CLI that launches an RScript.Here’s a way to do this using npm.

Please note that this blog post won’t teach you how to build the commandline tool, it will quickly go over the way to create a system-widecommand line interface, using npm.

If you want to learn more about building the utility, see thisfantastic series of blogposts by Mark Sellor.

Now, the idea is to have a CLI, i.e. a way to launch your utility with:

$ mytool

Enter fullscreen mode Exit fullscreen mode

And that, system-wide.

What you’ll need

  • An R script (script.R) with in it, for example: <!-- end list -->
#!/usr/bin/env Rscript --vanilla
cli::cat_rule("yeay")
cli::cat_bullet(Sys.time())

Enter fullscreen mode Exit fullscreen mode
  • npm, which you can get from there.

Let’s go

Create a new folder, and go inside it.

mkdir cli && cd cli

Enter fullscreen mode Exit fullscreen mode

Create the R Script there.

echo '#!/usr/bin/env Rscript --vanilla' > script.R
echo 'cli::cat_rule("yeay")' >> script.R
echo 'cli::cat_bullet(Sys.time())' >> script.R

Enter fullscreen mode Exit fullscreen mode

Try your script to see if it works:

Rscript script.R

Enter fullscreen mode Exit fullscreen mode

Now launch an npm project:

npm init -y

Enter fullscreen mode Exit fullscreen mode

(You can also run it without the -y to interactively add informationto the package.json.)

Now the important part: add a "bin" value in the package.json, withthe name of the bin you want to create, and the path to the script,relatively to the package file. Here is an example of a package.json(I removed some elements).

{
  "name": "cli",
  "version": "1.0.0",
  "description": "CLI example with npm",
  "bin" : {
    "clir" : "./script.R"
  },
  "author": "Colin Fay",
  "license": "MIT"
}

Enter fullscreen mode Exit fullscreen mode

Install it globally (need sudo rights):

sudo npm link

Enter fullscreen mode Exit fullscreen mode

And, voilà! Open your terminal, and you’re done!

clir


## ── yeay ────────────────────────────────────────────────
## ● 2019-05-22 22:36:29

Enter fullscreen mode Exit fullscreen mode

Other way to go

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay