File: Rcpp-quickref.R

package info (click to toggle)
rcpp 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 12,344 kB
  • sloc: ansic: 43,817; cpp: 39,947; sh: 51; makefile: 2
file content (54 lines) | stat: -rw-r--r-- 1,407 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
## ---- eval = FALSE-------------------------------------------------------
#  ## Note - this is R code.
#  ## cppFunction in Rcpp allows rapid testing.
#  require(Rcpp)
#  
#  cppFunction("
#  NumericVector exfun(NumericVector x, int i){
#      x = x*i;
#      return x;
#  }")
#  
#  exfun(1:5, 3)
#  
#  ## Use evalCpp to evaluate C++ expressions
#  evalCpp("std::numeric_limits<double>::max()")

## ---- eval = FALSE-------------------------------------------------------
#  # In R, create a package shell. For details,
#  # see the "Writing R Extensions" manual and
#  # the "Rcpp-package" vignette.
#  
#  Rcpp.package.skeleton("myPackage")
#  
#  # Add R code to pkg R/ directory. Call C++
#  # function. Do type-checking in R.
#  
#  myfunR <- function(Rx, Ry) {
#      ret = .Call("myCfun", Rx, Ry,
#                  package="myPackage")
#      return(ret)
#  }

## ---- eval=FALSE---------------------------------------------------------
#  require(myPackage)
#  
#  aa <- 1.5
#  bb <- 1.5
#  cc <- myfunR(aa, bb)
#  aa == bb
#  # FALSE, C++ modifies aa
#  
#  aa <- 1:2
#  bb <- 1:2
#  cc <-  myfunR(aa, bb)
#  identical(aa, bb)
#  # TRUE, R/C++ types don't match
#  # so a copy was made

## ---- eval=FALSE---------------------------------------------------------
#  Rcpp::sourceCpp("path/to/file/Rcpp_example.cpp")
#  x <- 1:5
#  all.equal(muRcpp(x), mean(x))
#  all.equal(var(x),varRcpp(x))