Skip to content

Commit

Permalink
adjusted options and environment options
Browse files Browse the repository at this point in the history
  • Loading branch information
fridgerator committed Jul 18, 2019
1 parent 7febbbe commit 39dcc4c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ In node:
```javascript
const { R } = require('@fridgerator/r-script')

// optionally pass an environment object if Rscript is not in your system PATH
// `process.env` will be used as default
let r = new R('./add.R', {PATH: '/bin:/location/to/R/bin'})
let r = new R('./add.R')

// optionally pass an options object used in child_process spawn calls
let r = new R('./add.R', {env: {PATH: '/bin:/location/to/R/bin'}})

// data is converted to a list variable `input` in the R script
r.data(2, 3)
Expand Down
2 changes: 1 addition & 1 deletion dist/bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ declare module '@fridgerator/r-script' {
}

export class R {
constructor(path: string, env?: Options);
constructor(path: string, opts?: Options);
data (...args: any[]): R;
call (_opts?: Options): Promise<object>;
callSync (_opts?: Options): object | undefined;
Expand Down
10 changes: 4 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ export class R {

constructor(scriptPath: string, opts?: Options) {
let _opts = opts || {}
let _env = _opts.env || process.env
this.rData = {}
this.path = scriptPath
this.options = Object.assign({}, {
env: {DIRNAME: __dirname},
env: Object.assign({}, {DIRNAME: __dirname}, _env),
encoding: 'utf8'
}, _opts)
this.idCounter = 0
Expand All @@ -39,15 +40,12 @@ export class R {
return new Promise((resolve: any, reject: any) => {
let opts = _opts || {}
this.setInputEnv(opts)
console.log('args : ', this.args)
console.log('opts : ', this.options)
console.log('call : ', 'Rscript', this.args, this.options)
let child = spawn('Rscript', this.args, this.options)
let body = ""
child.stderr.on('data', d => {
let msg = d.toString()
if (msg.includes('Warning message')) {
console.log(msg)
console.warn(msg)
return
}
reject(msg)
Expand All @@ -71,7 +69,7 @@ export class R {
if (child.stderr) {
let msg = child.stderr.toString()
if (msg.includes('Warning message')) {
console.log(msg)
console.warn(msg)
} else {
throw msg
}
Expand Down

0 comments on commit 39dcc4c

Please sign in to comment.