1

I am trying to call R program from Node which will call hist function. I am using npm package r-script - https://www.npmjs.com/package/r-script

below is my R code

x <- c(1,2,3,4,5,6,6)

hist(x, col="lightblue")

and here is index.js


var express = require('express');
var R = require("r-script");

var app = express(); app.get('/', function (req, res) 
{      
    var out = R("example/sample.R")
  .data("hello world", 20)
  .callSync();
    console.log(out);

}) 
var server = app.listen(8081, function () {      var port = server.address().port   
   console.log(`Example app listening at http://localhost:${port}`)})

But when I execute this code, I am getting error as below

Error: No method asJSON S3 class: histogram

Execution halted

Am I missing anything?

Community
  • 1
  • 1
Prasad Gavande
  • 213
  • 1
  • 4
  • 19
  • 1
    Result is a histogram - an image. Maybe you need to output something else? – Roman Luštrik Dec 28 '19 at 07:16
  • Hi Roman, if I write print ('hello world') it is working fine. But I cannot execute any other R functions. E.g. summery (1,2,3) ,or hist () – Prasad Gavande Dec 28 '19 at 09:47
  • Maybe you could try outputting `as.data.frame(hist(x)[2:4])`? This will be a data.frame with fields counts, density and mids (see `?hist`). – Roman Luštrik Dec 28 '19 at 10:05
  • Hi @RomanLuštrik, `as.data.frame(hist(x)[2:4])` works fine. How can I run `summary(1,2,3,4)` ? if I run it directly , it is giving error as below ```Error: No method asJSON S3 class: table``` thank you for help. – Prasad Gavande Dec 28 '19 at 12:40
  • Try figuring out your script in R (you can run it interactively). Make sure the output is something that is coercible to JSON (such as data.frames, it would seem). – Roman Luštrik Dec 28 '19 at 14:33
  • @RomanLuštrik, thank you I will do it. – Prasad Gavande Dec 29 '19 at 04:56

0 Answers0