简体   繁体   English

从node.js调用R脚本发出加载库

[英]Calling R script from node.js issue loading libraries

I am using 'r-script' npm package to run a R script from node.js. 我使用'r-script'npm包从node.js运行R脚本。 This R script calls another R script which loads a lot of libraries. 这个R脚本调用另一个R脚本,它加载了很多库。 I get an error 'there is no package 'zoo'' in the backend. 我得到一个错误'后端没有包'动物园''。 I have R version 3.5.3 and when I run these R files from RStudio, it runs seemlessly. 我有R版本3.5.3,当我从RStudio运行这些R文件时,它无缝地运行。 I have correctly defined PATH variable in environment setting. 我在环境设置中正确定义了PATH变量。 I am using React for frontend. 我正在使用React作为前端。

I have tried running the app from command line as administrator but it still gives the same error. 我尝试从命令行运行应用程序作为管理员,但它仍然给出相同的错误。

//Backend //后端

//server.js
router.post("/putData", (req, res) => {

    const {simid,delay,decay} = req.body;

    let rout = R('rscript.R')
                .data()
                .callSync();
    console.log(simid + delay + decay);
    console.log(rout);
    return res.json({success:true});

  });

//rscript.R //rscript.R

#rscript.R
    source("Main.R")

//Main.R //Main.R

#Main.R
require(zoo)
require(quantmod)
require(Rcpp)
(And a lot of other libraries)

I recieve the following error at the server end: 我在服务器端收到以下错误:

[0] POST /api/putData 500 12314.741 ms - 578 /n
[0] Loading required package: zoo
[0] Loading required package: quantmod
[0] Loading required package: xtable
[0] Loading required package: tidyr
[0] Loading required package: hashmap
[0] Loading required package: Quandl
[0] Loading required package: Rcpp
[0] Loading required package: readr
[0] Loading required package: roll
[0] Loading required package: gridExtra
[0] Error in R script rscript.R
[0]  'there is no package called 'zoo''

I found the solution. 我找到了解决方案。 As mentioned in the documentation of 'r-script' npm package on github, you should use needs() instead of library() or require() for loading a package. 正如github上'r-script'npm包的文档中所提到的,你应该使用needs()而不是library()或require()来加载包。 So I installed 'needs' package from RStudio. 所以我从RStudio安装了'needs'包。 After installtion, it gave a prompt that would you like to automatically load this package whenever a function from it is called. 安装完成后,它会提示您在调用函数时自动加载此包。 You won't be able to successfully respond from here as you will need write permission. 您将无法在此处成功回复,因为您需要写入权限。 So open command prompt as administrator, load R by typing (R), and then run library(needs). 因此,以管理员身份打开命令提示符,键入(R)加载R,然后运行库(需求)。 Now you get a prompt again, choose Yes and you are done. 现在您再次收到提示,选择是,您就完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM