【问题标题】:Nodejs cannot find R for r-integration package [Ubuntu]Nodejs 找不到 R 的 r 集成包 [Ubuntu]
【发布时间】:2023-01-20 06:45:29
【问题描述】:

我已经通过 npm 安装了r-integration。我已将我的 R 脚本放在位于 srv/nodeserver/ 的 node-server 目录中。我的机器上安装了 R。

> whereis R
R: /usr/bin/R /usr/lib/R /etc/R /usr/local/lib/R /usr/share/R /usr/share/man/man1/R.1.gz

nodejs REST API 代码如下所示:

app.get('/getRresult',(req,res)=>{
        let result = R.executeRScript(
                   'server.R',"circumference",{r:2});
        console.log(result)
});

当我点击 RESTAPI url 时,出现以下错误:

Error: R not found, maybe not installed.

但是 R 已安装,因为如果我在终端中键入 R,R shell 就会打开。我尝试在~/.bashrc中导出usr/bin/R的路径。它似乎仍然不起作用。我很困惑哪条路径将实际工作或如何设置 ubuntu PATH 变量的路径。

【问题讨论】:

    标签: r node.js ubuntu


    【解决方案1】:

    您需要在您的ubuntu 上安装 R。

    这是步骤

    #1 安装必要的依赖

    通过 HTTPS 添加新的存储库:

    $ sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common
    

    #2 将 CRAN 存储库添加到系统源列表中

    $ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
    $ sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'
    

    #3 安装 R

    $ sudo apt install r-base
    

    #4 验证 R 版本

    R --version
    

    终端输出

    $ R --version
    R version 3.6.3 (2020-02-29) -- "Holding the Windsock"
    Copyright (C) 2020 The R Foundation for Statistical Computing
    Platform: x86_64-pc-linux-gnu (64-bit)
    
    R is free software and comes with ABSOLUTELY NO WARRANTY.
    You are welcome to redistribute it under the terms of the
    GNU General Public License versions 2 or 3.
    For more information about these matters see
    https://www.gnu.org/licenses/.
    

    #5 最后运行简单的例子

    test.js 将通过 R.executeRScript() 调用 test.R 它会将值返回到test.js 然后它会 console.log()

    文件名另存为test.js

    const R = require('r-integration');
    
    let result = R.executeRScript("./test.R");
    console.log(result);
    

    文件名另存为test.R

    x <- 5
    y <- 6
    z <- 3
    result <- max(x, y, z)
    
    print(result)
    

    安装r-integration库

    npm install r-integration
    

    运行它并查看结果

    $ node test.js
    [ 6 ]
    

    参考

    R-integration - Node JS API

    How to Install R on Ubuntu 20.04

    【讨论】:

      猜你喜欢
      • 2016-02-06
      • 2017-06-19
      • 2021-02-05
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 2021-03-24
      • 2020-10-13
      • 1970-01-01
      相关资源
      最近更新 更多