Search code examples
javascriptrnpx

Run javascript command from within R package


I would like to generate identicons with Jdenticon from within an R package. So, given a set of arguments in R variables including an output file name, I'd like a function that will return a filename refering to the generated Jdenticon (either PNG or SVG) that I can use (eg to read into Rmarkdown documents).

So I'd like a function that does something like:

create_jdenticon('dj23idfsb', size = 100, format = 'svg')
# [1] "/var/folders/59/r7wjy0gn6yv59w19694x5gy80000gp/T//RtmpjDwDfq/jdenticon_dj23idfsb_100.svg"

I can write most of the R function, I'm just not sure how to setup and run the npx command required from within R (or even if this is the right approach).

I see that the packer package has an npx command, but I'm not sure whether this package really does what I want or how to start.

Any pointers would be appreciated.

[The reason I need it to be Jdenticons in particular is that I'm using Jdenticons elsewhere on the web, and they need to match (so solutions using R packages that do the same thing but generate different identicons will not work).]


Solution

  • Here's a minimally working version of Jdenticon for R:

    https://github.com/mattroumaya/jdenticon

    The main steps in creating this type of wrapper for a Node.js package are:

    1. Installing node dependencies in your package's inst folder.
    2. Writing any JavaScript scripts that you intend to run from your R package.
    3. Writing an R function that calls your JavaScript function(s) using the processx package.
    4. Finally, bundling everything to avoid the R user from having to run npm install to initialize node_modules.