您需要在您的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