【问题标题】:runs in R but not Rscript [duplicate]在 R 但不在 Rscript 中运行 [重复]
【发布时间】:2018-07-19 16:13:24
【问题描述】:

这是我的脚本

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

如何解决这个问题?

【问题讨论】:

    标签: r inline rcpp


    【解决方案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

    【讨论】:

    • 它在 R 版本 3.5.0 及更高版本中加载。这种变化早就该发生了。
    • @DirkEddelbuettel,很好的骗局,我还没有看到。
    • 哦,关于Rscript 的第二个问题都是关于这个的:)
    猜你喜欢
    • 2019-01-08
    • 2012-07-25
    • 1970-01-01
    • 2017-03-31
    • 2016-03-26
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 2015-03-10
    相关资源
    最近更新 更多