gpt4 book ai didi

c++ - 通过 RInside 运行时 abline 出错

转载 作者:行者123 更新时间:2023-11-28 05:34:03 25 4
gpt4 key购买 nike

我在使用 RInside 与控制台时遇到了问题。这一切都在 ubuntu 14.04 上运行,使用通过 apt-get 从 CRAN 安装的 R 3.2.4。这是 C++ 和 R 代码:

#include <RInside.h>                            // for the embedded R via RInside

int main(int argc, char *argv[]) {

RInside R(argc, argv); // create an embedded R instance
R.parseEval("source('abline.R')");
}

abline.R

bp <- data.frame(
age = c(28, 23, 52, 42, 27, 29, 43, 34, 40, 28),
systolic = c(70, 68, 90, 75, 68, 80, 78, 70, 80, 72))
str(bp)
attach(bp)

bp.lm <- lm(systolic ~ age)

plot(age, systolic)
abline(bp.lm)
lines(lowess(age, systolic, f=0.75), lty=2)

R 代码在控制台中运行良好,但在程序运行时出错。

mlilback@rc2x:/tmp/abtest$ ./abtest 
'data.frame': 10 obs. of 2 variables:
$ age : num 28 23 52 42 27 29 43 34 40 28
$ systolic: num 70 68 90 75 68 80 78 70 80 72
Error in if (noInt) { : argument is of length zero
terminate called after throwing an instance of 'std::runtime_error'
what(): Error evaluating: source('abline.R')
Aborted (core dumped)

if (noInt) { 来自 abline 的源代码(我的 R 版本中的第 18 行)。我完全不明白为什么这只通过 RInside 发生。

有什么想法吗?

最佳答案

对我来说没有任何问题。 Ubuntu 16.04。用完示例目录以免费获取基于 GNUmakefile 的构建:

~/git/rinside/inst/examples/standard(master)$ vi soquestion.cpp
~/git/rinside/inst/examples/standard(master)$ make soquestion
ccache g++ -I/usr/share/R/include -I/usr/local/lib/R/site-library/Rcpp/include \
-I/usr/local/lib/R/site-library/RInside/include -g -O3 -Wall -pipe \
-Wno-unused -Wall soquestion.cpp -Wl,--export-dynamic -fopenmp \
-L/usr/lib/R/lib -lR -lpcre -llzma -lbz2 -lz -lrt -ldl -lm -lblas -llapack \
-L/usr/local/lib/R/site-library/RInside/lib -lRInside \
-Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib -o soquestion
~/git/rinside/inst/examples/standard(master)$ vi abline.R
~/git/rinside/inst/examples/standard(master)$ ./soquestion
'data.frame': 10 obs. of 2 variables:
$ age : num 28 23 52 42 27 29 43 34 40 28
$ systolic: num 70 68 90 75 68 80 78 70 80 72
~/git/rinside/inst/examples/standard(master)$

我只是复制并粘贴了您的两个文件。还有:

~/git/rinside/inst/examples/standard(master)$ ls -1tr | tail -4
soquestion.cpp
soquestion
abline.R
Rplots.pdf
~/git/rinside/inst/examples/standard(master)$

您可能想通过 pdf()png() 打开设备文件 ...

关于c++ - 通过 RInside 运行时 abline 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38748245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com