DEPRECATED: This project is no longer under maintenance. It still exist for the purpose of documentation on how to setup an own API for RScripts in child process using Docker, zeit.co/now, Jest, Travis, codeclimate, Koa, nodemon, husky, ts-node and Typescript for development. We still suggest using projects like opencpu instead of running your own system for a purpose like this.
Written with Koa (lightweight Express.js alternative. suitable for small APIs) in Typescript.
- docker (version 18.09.1)
- Node.js (version 10.5.0)
- R (version 3.5.2)
Currently the Docker container is a based in rocker/r-ver with R version 3.5.2 which is based on debian:stretch
git clone git@github.com:technologiestiftung/rrest.git
npm install
R needs the packrat installed to make a snapshot of all needed libraries. Please open an issue if there is the need to install additional global packages and tools. Run R
cd r-scripts
R
Then in R run:
install.packages("packrat")
packrat::restore("path/to/r-scripts")
q()
# runs nodemon with ts-node
npm run dev
Run R
cd r-scripts
R
If you need to install libraries in R - run:
install.packages('jsonlite')
# packrat should have auto snapshot enabled
# if the package doesn't show up in
# r-scripts/packrat/packrat.lock
# run
packrat::snapshot()
# all local dependencies will be saved into
# the packrat folder and can be restored
Some conventions need to be taken in account.
- In the docker container the
R
script gets executed by a subprocess called from thenode-app/dist/index.js
to make the whole setup work together theR
script needs to set its working directory to./r-scripts
when executed from the container by running as first statementsetwd("./r-scripts")
- To make packrat work it also needs to source the provided
.Rprofile
after thatsource(".Rprofile")
- After that libraries can be included
library("jsonlite")
. Remember to install them in an environment that haspackrat
available. - The should accept input in
stdin
and have one main function that returns the result. The results will be passed tostdout
and the Node server catches it.
This is currently Node.js only.
npm test
From the root of the repository run (omit the --build
if you did not change source files)
docker-compose up --build
This starts the container. It accepts POST
requests with JSON
payload on PORT 3000 on the endpoint http://localhost:8080/submit
Example curl POST
curl -X "POST" "http://localhost:8080/submit" \
-H 'Content-Type: application/json; charset=utf-8' \
-d $'{
"foo": "bahh"
}'
To stop the running containers
docker-compose down
Could be deployed to a AWS EB Docker setup