joshkatz / r-script

A simple little module for passing data from NodeJS to R (and back again).
MIT License
161 stars 67 forks source link

installing packages:njsonlite SyntaxError: Unexpected token i #3

Open Giackgamba opened 8 years ago

Giackgamba commented 8 years ago

Hi there, I'm loving your module, and I got it working like a charm on others machines. However I'm having problems using it on a Windows machine. It's probably a PATH issue, or R library related issue, but I can't get it work.

I'm using r-script inside a node (v4.2.4) application with the latest r-script npm release (0.0.3) and R 3.3.1

This is the relevant excerpt from my routing file: index.js

console.log(input);
R('test.R')
 .data(input)
 .call(function(err, d) {
     if (err) res.send('error: ' + err);
     console.log(d)
 })

and this is the console output:

demo_r_frate2
undefined:1
installing packages:njsonlite
^

SyntaxError: Unexpected token i
    at Object.parse (native)
    at Socket.<anonymous> ([.....]\app\node_modules\r-script\index.js:34:25)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at readableAddChunk (_stream_readable.js:146:16)
    at Socket.Readable.push (_stream_readable.js:110:10)
    at Pipe.onread (net.js:523:20)

Any thought on how to solve this? What I am doing wrong?

njp16330 commented 7 years ago

I am facing a similar issue. Though I am using R 3.0.1

achakhalyan commented 7 years ago

Hi. I will doing sores the R code for use it, how can do it? thanks

MattDekinder commented 7 years ago

I am experiencing an identical error output. It looks to be resulting from calling JSON.parse() on a javascript object when it expects a JSON string.

letrape commented 6 years ago

Me too does any of you guys has solved the issue?

bol83 commented 6 years ago

I have the same issue. Has anyone solved this yet?

letrape commented 6 years ago

Actually I have solved the issue, the thing is not parsing from node to R, instead is about parsing the response from R to Node, if you watch in the poorly documented samples the operator %>% is used to return the info. If you don't return any thing from R but your callback in node js is expecting to return data, then it will crash in Json.Parse(d) because d is something invalid

bol83 commented 6 years ago

I cant see why the %>% is doing anything other than chaining data with dplyr package. As I understand the last value is allways returned by the parser. Can you give an example of how to explicit return something from R other than the last value in R scrip?

letrape commented 6 years ago

There are several reasons for this error as you can see in the error message it comes from the line:

app\node_modules\r-script\index.js:34:25

If you check the source code for r-script library, this error is happening in the callback when it tries to parse the result of the child process to a JSON format.

You can try to edit the index,js file in the r-script library in order to find the real problem.

I was able to fix, three diffrent errors causing this generic message:

1) No permission for the R libraries: When you are trying to run needs, or install.package in an R script you need to specify a lib folder if not R will use the default folder for packages and libraries, that folder needs to have write permissions in order to work properly

2) Missed packages, not all the packages are installed

3) Needs problem: Well i've change needs('package') to library('package') in my R file that solved my problem, my environment does not need to be portable so it can use library with no problem

bol83 commented 6 years ago

Its still a problem for me. Even if I run a empty R file I will get this problem. Or just an R file with TRUE.

bol83 commented 6 years ago

It works fine when I run the node file directly from console: node myfile.js. The problem happens when my r-script is inside a module.export and i later call the script from there

PedroS11 commented 6 years ago

I gave full control permissions to the R folder and it solved the issue