Function .C — argument setup fails compilation

前端 未结 3 534
伪装坚强ぢ
伪装坚强ぢ 2021-01-19 07:38

I\'m currently learning to call compiled C code in R. Yesterday I created a function for the infinite pi series that when run in R returns a length 1 numeric vector (pi), w

3条回答
  •  天涯浪人
    2021-01-19 07:44

    You could load Rcpp just to get access to its Rcpp Attributes functionality. It permits you to write this as (expanded to two lines for display; it really fits on one line):

    R> cppFunction('double fib(double n) { if (n<2) return(n); 
    +                                      else return fib(n-1) + fib(n-2);}')
    R> fib(10)
    [1] 55
    R> 
    

    If you run cppFunction(..., verbose=TRUE) you get to see the file it creates.

    In general, avoid the .C() interface and concentrate in .Call(). This is the very clear recommendation in the NEWS file of the most recent R release:

    * .C(DUP = FALSE) and .Fortran(DUP = FALSE) are now deprecated, and
      may be disabled in future versions of R.  As their help has long
      said, .Call() is much preferred.
    

    Let's stress the .Call() is much preferred one more time. See eg Hadley's Advanced R Programming draft for more on getting going with .Call(). His advice too is to straight to Rcpp.

提交回复
热议问题