在R中运行,但不在Rscript中运行

时间:2018-07-19 16:13:25

标签: r inline rcpp

这是我的剧本

library(Rcpp)
library(inline)

myroot2 <- rcpp(signature(xs="numeric"), body='double x=as<double>(xs); return wrap(::sqrt(x));')
print(myroot2(100))

如果我从R中获取此脚本,那么它运行得很好

>source('test.R')
Attaching package: ‘inline’

The following object is masked from ‘package:Rcpp’:

registerPlugin

[1] 10

以批处理模式

Rscript --verbose test.R
running
'/usr/local/share/bcbio/anaconda/lib/R/bin/R --slave --no-restore -- 
file=test.R'


Attaching package: ‘inline’

The following object is masked from ‘package:Rcpp’:

registerPlugin
Error in signature(xs = "numeric") : could not find function 
"signature"
Calls: rcpp -> cxxfunction
Execution halted

该如何解决?

1 个答案:

答案 0 :(得分:1)

signature函数位于methods中,显然默认情况下Rscript未加载该函数。试试这个:

library(Rcpp)
library(inline)

myroot2 <- rcpp(methods::signature(xs="numeric"), body='double x=as<double>(xs); return wrap(::sqrt(x));')
print(myroot2(100))

唯一的更改是使用methods::signature