在 R 中运行但不在 Rscript 中运行
runs in R but not Rscript
这是我的脚本
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
如何解决这个问题?
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
。
这是我的脚本
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
如何解决这个问题?
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
。