Skip to content
This repository has been archived by the owner on Feb 17, 2020. It is now read-only.

Commit

Permalink
implement first ui binding: manipulating using a slider
Browse files Browse the repository at this point in the history
  • Loading branch information
markuskonk committed Mar 16, 2018
1 parent bf651f1 commit 4083922
Show file tree
Hide file tree
Showing 22 changed files with 2,156 additions and 50 deletions.
31 changes: 26 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
FROM node:8-alpine
FROM rocker/geospatial:latest

SHELL ["/bin/bash", "-c"]

RUN R -e 'install.packages(c("diveMove", "trip", "adehabitatLT", "plm", "cshapes", "plumber"))'
# based on https://gist.github.com/remarkablemark/aacf14c29b3f01d6900d13137b21db3a
RUN apt-get update \
&& apt-get install -y curl \
&& apt-get -y autoclean

ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 8.10.0
RUN curl --silent -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
RUN source $NVM_DIR/nvm.sh \
&& nvm install $NODE_VERSION \
&& nvm alias default $NODE_VERSION \
&& nvm use default

# add node and npm to path so the commands are available
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH

#WORKDIR /bindings

COPY package.json package.json

RUN apk add --no-cache
RUN npm install --production

COPY config config
COPY index.js index.js
COPY controllers controllers
COPY controllers/bindings.js controllers/bindings.js
COPY index.js index.js

CMD ["node", "index.js"]

CMD ["node", "index.js"]
80 changes: 66 additions & 14 deletions controllers/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
const express = require('express');
const bodyParser = require('body-parser');
const debug = require('debug')('bindings');
const rscript = require('r-script');
const path = require('path');
const fs = require('fs')

const fn = require('./generalFunctions');


let bindings = {};

bindings.start = (conf) => {
Expand All @@ -30,22 +35,43 @@ bindings.start = (conf) => {
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
debug('Receiving data to create bindings');
app.post('/api/v1/bindings', function(req, res) {
debug('Received the following data: %s', req.body.id);
switch (req.body.purpose) {
case 'showFigureCode':
bindings.showCode(req.body);
break;
case 'showResultCode':
bindings.showCode(req.body);
break;
default:
break;
}
app.post('/api/v1/bindings/discover/discoverNumber', function(req, res) {
res.send({
callback: 'ok',
data: req.body});
});
app.post('/api/v1/bindings/discover/discoverFigure', function(req, res) {
res.send({
callback: 'ok',
data: req.body});
});
app.post('/api/v1/bindings/inspect/inspectPaperDataCode', function(req, res) {
res.send({
callback: 'ok',
temp: 'bla',
data: req.body.id});
data: req.body});
});
app.post('/api/v1/bindings/inspect/inspectNumberDataCode', function(req, res) {
res.send({
callback: 'ok',
data: req.body});
});
app.post('/api/v1/bindings/inspect/inspectFigureDataCode', function(req, res) {
res.send({
callback: 'ok',
data: req.body});
});
app.post('/api/v1/bindings/manipulate/manipulateFigure', function(req, res) {
bindings.manipulateFigure(req.body);
res.send({
callback: 'ok',
data: req.body});
});
app.post('/api/v1/bindings/manipulate/run', function(req, res) {
debug('Start running plumber service for %s', req.body.id);
bindings.runR(req.body);
res.send({
callback: 'ok',
data: req.body});
});
let bindingsListen = app.listen(conf.port, () => {
debug('Bindings server listening on port %s', conf.port);
Expand All @@ -54,6 +80,17 @@ bindings.start = (conf) => {
});
};

bindings.manipulateFigure = function(binding) {
debug('Start creating the binding %s for the result %s for the compendium %s', binding.purpose, binding.figure, binding.id);
let fileContent = fn.readRmarkdown(binding.id, binding.mainfile);
fileContent = fn.replaceVariable(fileContent, binding.variable);
let codeLines = fn.handleCodeLines(binding.codeLines);
let code = fn.extractCode(fileContent, codeLines);
let wrappedCode = fn.wrapCode(code, binding.id, binding.figure);
fn.saveResult(wrappedCode, binding.id, binding.figure.replace(/\s/g, '').toLowerCase());
fn.createRunFile(binding.id, binding.figure.replace(/\s/g, '').toLowerCase());
};

bindings.showCode = function(binding) {
debug('Start creating the binding %s for the result %s', binding.purpose, binding.result);
let fileContent = fn.readRmarkdown(binding.id, binding.mainfile);
Expand All @@ -63,4 +100,19 @@ bindings.showCode = function(binding) {
fn.modifyMainfile(binding, fileContent);
};

bindings.createInteractiveFigure = function(binding) {
debug('Start creating manipulation binding for %s for figure %s', binding.id, binding.figure);
};

bindings.runR = function(binding) {
debug('Start running R script %s', binding.id);
// debug('File exists? %s', fs.existsSync(path.join('tmp', 'o2r', 'compendium', binding.id, 'figure4.R')));
// debug('File exists? %s', fs.existsSync(path.join('tmp', 'o2r', 'compendium', binding.id, 'main.Rmd')));
// debug('File exists? %s', fs.existsSync(path.join('tmp', 'o2r', 'compendium', binding.id, 'figure4run.R')));
let run = rscript(path.join(path.join('tmp', 'o2r', 'compendium', binding.id, binding.figure.replace(/\s/g, '').toLowerCase() + 'run.R')))
.data(binding.id)
.callSync();
//console.log(run);
};

module.exports = bindings;
59 changes: 42 additions & 17 deletions controllers/generalFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,45 @@ fn.modifyMainfile = function(binding, fileContent) {
};

fn.extractCode = function(fileContent, codeLines) {
let newContent = '---' + fileContent.split('---')[1] + '---\n' + '```{r}';
let newContent = '';
let splitFileContent = fileContent.split('\n');
codeLines.forEach(function(elem) {
newContent = newContent + '\n' + splitFileContent[elem] + '\n';
newContent += splitFileContent[elem] + '\n';
});
newContent += '```';
return newContent;
};

fn.wrapCode = function(sourcecode, compendiumId, result) {
let get = "#' @get /" + result.replace(/\s/g, '').toLowerCase() + '\n' +
"#' @png \n" +
'function(newValue){ \n' +
'newValue = as.numeric(newValue) \n';
//'newValue = as.numeric(newValue) \n' + 'library("xts") \n library("RColorBrewer") \n library("foreign") \n library("plm") \n library("sp") \n library("spacetime") \n library("gstat") \n library("mapdata") \n library("rgdal") \n library("maps") \n library("maptools") \n';
let code = sourcecode.split('\n');
code[code.length-2] = 'print(' + code[code.length-2] + ')';
let newCode = '';
code.forEach(function(elem) {
newCode += elem + '\n';
});
let newContent = get + newCode + '}';
return newContent;
};

fn.replaceVariable = function(code, variable) {
let newContent = code.replace(variable.text, variable.varName + ' = newValue');
return newContent;
};

fn.handleCodeLines = function(lines) {
let codeLines = [];
lines.forEach(function(elem) {
if (elem.toString().indexOf(/-/i) > -1) {
let range = elem.split('-');
for (let i = range[0]; i <= range[1]; i++) {
codeLines.push(Number(i)-1); // -1 is required as the code lines from the front end start counting at 1.
};
} else {
codeLines.push(Number(elem)-1);
}
for (let i = elem.start; i <= elem.end; i++) {
codeLines.push(Number(i)-1); // -1 is required as the code lines from the front end start counting at 1.
};
});
return codeLines.sort(function(a, b) {
return a-b;
});
return codeLines.sort();
};

fn.readRmarkdown = function(compendiumId, mainfile) {
Expand All @@ -82,14 +99,22 @@ fn.saveResult = function(data, compendiumId, fileName) {
fileName = fileName.replace(' ', '');
fileName = fileName.replace('.', '_');
fileName = fileName.replace(',', '_');
if (!fs.existsSync(path.join('tmp', 'o2r', 'compendium', compendiumId, 'bindings'))) {
fs.mkdirSync(path.join('tmp', 'o2r', 'compendium', compendiumId, 'bindings'));
if (!fs.existsSync(path.join('tmp', 'o2r', 'compendium', compendiumId))) {
fs.mkdirSync(path.join('tmp', 'o2r', 'compendium', compendiumId));
}
fileName = path.join('bindings', fileName + '.Rmd');
fn.saveRmarkdown(data, compendiumId, fileName);
fileName = path.join(fileName + '.R');
fn.saveRFile(data, compendiumId, fileName);
};

fn.createRunFile = function(compendiumId, result) {
let content = 'library("plumber")' + '\n' +
'path = paste("/tmp/o2r/compendium/' + compendiumId + '/' + result + '.R", sep = "")\n' +
'r <- plumb(path)\n' +
'r$run(host = "0.0.0.0", port=8000)';
fn.saveRFile(content, compendiumId, result+'run.R');
};

fn.saveRmarkdown = function(data, compendiumId, fileName) {
fn.saveRFile = function(data, compendiumId, fileName) {
let dir = path.join('tmp', 'o2r', 'compendium', compendiumId, fileName);
fs.writeFile(dir, data, 'utf8', function(err) {
debug(err);
Expand Down
Binary file added controllers/plumber/.RData
Binary file not shown.
134 changes: 134 additions & 0 deletions controllers/plumber/.Rhistory
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
file.exists("plumber.R")
file.exists("plumber.R")
print(file.exists("plumber.R"))
library(here)
print(file.exists(here("plumber.R"))
print(file.exists(here("plumber.R")))
print(file.exists(here("plumber.R")))
print(file.exists(here("plumber/plumber.R")))
print(file.exists(here("plumber","plumber.R")))
print(file.exists(here("plumber","plumber.R")))
print(file.exists(here("plumber/plumber.R")))
print(file.exists(here("plumber.R")))
here()
print(file.exists(here("plumber.R")))
setwd("~/o2r-platform/bindings/o2r-bindings/controllers/plumber")
print(file.exists(here("plumber.R")))
here()
print(file.exists(here("plumber","plumber.R")))
print(file.exists(here("controllers", "plumber","plumber.R")))
needs(magrittr)
set.seed(512)
do.call(rep, input) %>%
strsplit(NULL) %>%
sapply(sample) %>%
apply(2, paste, collapse = "")
install.packages("magrittr")
needs(magrittr)
library(magrittr)
set.seed(512)
do.call(rep, input) %>%
strsplit(NULL) %>%
sapply(sample) %>%
apply(2, paste, collapse = "")
do.call(rep) %>%
strsplit(NULL) %>%
sapply(sample) %>%
apply(2, paste, collapse = "")
do.call(rep) %>%
print(set.seed(512))
r <- plumb(here("controllers", "plumber","plumber.R")) # Where 'myfile.R' is the location of the file shown above
r$run(port=8000)
library(plumber)
library(here)
here()
r <- plumb(here("controllers", "plumber","plumber.R")) # Where 'myfile.R' is the location of the file shown above
r$run(port=8000)
install.package("plumber", repos = "http://cran.us.r-project.org")
install.packages("plumber", repos = "http://cran.us.r-project.org")
install.packages("plumber", repos = "http://cran.us.r-project.org")
install.packages("here", repos = "http://cran.us.r-project.org")
install.packages("here", repos = "http://cran.us.r-project.org")
install.packages('plumber', repos='http://cran.us.r-project.org')
2+2
install.packages('here',dependencies=TRUE, repos='http://cran.rstudio.com/')
install.packages("here", dependencies = TRUE, repos = "http://cran.rstudio.com/")
file.exists("plumber.R")
here()
#r <- plumb("indings/o2r-bindings/controllers/plumber/plumber.R") # Where 'myfile.R' is the location of the file shown above
r <- plumb("/home/markus/o2r-platform/bindings/o2r-bindings/controllers/plumber/plumber.R") # Where 'myfile.R' is the location of the file shown above
r$run(port=8000)
install.packages("plumber")
r$run(port=8000)
#install.packages('plumber', dependencies=TRUE, repos='http://cran.rstudio.com/')
library(plumber)
#r <- plumb("indings/o2r-bindings/controllers/plumber/plumber.R") # Where 'myfile.R' is the location of the file shown above
r <- plumb("/home/markus/o2r-platform/bindings/o2r-bindings/controllers/plumber/plumber.R") # Where 'myfile.R' is the location of the file shown above
r$run(port=8000)
#r <- plumb(here("bindings", "controllers", "plumber", "plumber.R")) # Where 'myfile.R' is the location of the file shown above
#r <- plumb("home", "markus", "o2r-platform", "bindings", "o2r-bindings", "controllers", "plumber", "plumber.R")
here()
library(here)
library(here)
#r <- plumb(here("bindings", "controllers", "plumber", "plumber.R")) # Where 'myfile.R' is the location of the file shown above
#r <- plumb("home", "markus", "o2r-platform", "bindings", "o2r-bindings", "controllers", "plumber", "plumber.R")
here()
#r <- plumb(here("bindings", "controllers", "plumber", "plumber.R")) # Where 'myfile.R' is the location of the file shown above
r <- plumb("home", "markus", "o2r-platform", "bindings", "o2r-bindings", "controllers", "plumber", "plumber.R")
#install.packages('plumber', dependencies=TRUE, repos='http://cran.rstudio.com/')
library(plumber)
#r <- plumb(here("bindings", "controllers", "plumber", "plumber.R")) # Where 'myfile.R' is the location of the file shown above
r <- plumb("home", "markus", "o2r-platform", "bindings", "o2r-bindings", "controllers", "plumber", "plumber.R")
#r <- plumb(here("bindings", "controllers", "plumber", "plumber.R")) # Where 'myfile.R' is the location of the file shown above
r <- plumb(here("home", "markus", "o2r-platform", "bindings", "o2r-bindings", "controllers", "plumber", "plumber.R"))
#r <- plumb(here("bindings", "controllers", "plumber", "plumber.R")) # Where 'myfile.R' is the location of the file shown above
r <- plumb(here("plumber.R"))
#r <- plumb("/home/markus/o2r-platform/bindings/o2r-bindings/controllers/plumber/plumber.R") # Where 'myfile.R' is the location of the file shown above
r$run(port=8000)
#r <- plumb(here("bindings", "controllers", "plumber", "plumber.R")) # Where 'myfile.R' is the location of the file shown above
here()
install.packages("magrittr")
# ex-async.R
needs(dplyr)
attach(input[[1]])
# output of final expression is returned to node
df %>%
mutate(group = cut(rating, nGroups, ordered = T)) %>%
group_by(group) %>%
summarize_each(funs_(fxn)) %>%
select(group, rating, advance) %>%
mutate(group = as.character(group))
getwd()
getwd() + "/test"
getwd()."/test"
paste(getwd(),"/test")
paste(getwd(),"/test", sep="")
file.exists(paste(getwd(),"/plumer.R", sep=""))
file.exists(paste(getwd(),"/plumber.R", sep=""))
#install.packages('plumber', dependencies=TRUE, repos='http://cran.rstudio.com/')
library(plumber)
r <- plumb("/bindings/controllers/plumber/plumber.R") # Where 'myfile.R' is the location of the file shown above
#install.packages('plumber', dependencies=TRUE, repos='http://cran.rstudio.com/')
library(plumber)
r <- plumb("bindings/controllers/plumber/plumber.R") # Where 'myfile.R' is the location of the file shown above
#r <- plumb("/home/markus/o2r-platform/bindings/o2r-bindings/controllers/plumber/plumber.R") # Where 'myfile.R' is the location of the file shown above
r$run(port=8000)
#r <- plumb("bindings/controllers/plumber/plumber.R") # Where 'myfile.R' is the location of the file shown above
r <- plumb("/home/markus/o2r-platform/bindings/o2r-bindings/controllers/plumber/plumber.R") # Where 'myfile.R' is the location of the file shown above
r$run(port=8000)
#install.packages('plumber', dependencies=TRUE, repos='http://cran.rstudio.com/')
library(plumber)
#r <- plumb("bindings/controllers/plumber/plumber.R") # Where 'myfile.R' is the location of the file shown above
r <- plumb("/home/markus/o2r-platform/bindings/o2r-bindings/controllers/plumber/plumber.R") # Where 'myfile.R' is the location of the file shown above
r$run(port=8000)
knitr::opts_chunk$set(echo = TRUE)
plot(pressure)
plot(pressure*2)
plot(pressure)
pressure
install.packages("rgdal")
installed.packages(c("dplyr"))
library("dplyr")
installed.packages(c("dplyr"))
installed.packages(c("dplyr"), repos='http://cran.rstudio.com/')
install.packages(c("dplyr"), repos='http://cran.rstudio.com/')
20 changes: 20 additions & 0 deletions controllers/plumber/plumber.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#' Plot out data from the iris dataset
#' @get /plot
#' @png

function (spec){
myData <- iris
title <- "All Species"

# Filter if the species was specified
if (!missing(spec)){
title <- paste0("Only the '", spec, "' Species")
myData <- subset(iris, Species == spec)
}


plot(myData$Sepal.Length, myData$Petal.Length,
main=title, xlab="Sepal Length", ylab="Petal Length")
}


5 changes: 5 additions & 0 deletions controllers/plumber/runPlumber.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#install.packages('plumber', repos='http://cran.rstudio.com/')
library("plumber")
#r <- plumb("/bindings/controllers/plumber/plumber.R") # Where 'myfile.R' is the location of the file shown above
r <- plumb("/home/markus/o2r-platform/bindings/o2r-bindings/controllers/plumber/plumber.R") # Where 'myfile.R' is the location of the file shown above
r$run(host = '0.0.0.0', port=8000)
Binary file added controllers/spacetime/.RData
Binary file not shown.
Loading

0 comments on commit 4083922

Please sign in to comment.