Search code examples
rinlinercpp

runs in R but not Rscript


Here is my script

library(Rcpp)
library(inline)

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

If I source this script from R it runs just fine

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

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

registerPlugin

[1] 10

In batch mode

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

How to fix this?


Solution

  • The signature function is in methods, which apparently is not loaded by default with Rscript. Try this:

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

    where the only change was to use methods::signature.