You are looking at historical revision 27638 of this page. It may differ significantly from its current revision.

R

R interface for Chicken Scheme

Overview

R provides a simple way to call R-functions, consisting of the following two forms: R and R*. R evaluates an R-expression, returning an opaque R-pointer that can be passed to other R-functions. Use this, for instance, when you don't need to modify the object in Scheme.

R*, on the other hand, evaluates the expression and tries to translate it into Scheme; it understands NULL, lists, strings, reals, bools, complex numbers and symbols. Everything else is opaque.

Documentation

NA

[constant] NA → (make-NA)

NA corresponds to R's NA.

(define NA (make-NA))
Examples

Don't forget to quasiquote:

(R* (is.na (c 1 ,NA)))
 => #(#f
 =>   #t)

R-missing

[constant] R-missing → (foreign-value R_MissingArg SEXP)

R-constant for missing arguments

(define R-missing (foreign-value "R_MissingArg" SEXP))
Examples

Selecting columns of matrices; corresponds to sum(complete.cases(airquality[, -1])):

(R* (sum (complete.cases (|[| airquality ,R-missing 1))))
 => 116

R-null

[constant] R-null → (foreign-value R_NilValue SEXP)

NULL

(define R-null (foreign-value "R_NilValue" SEXP))
Examples

Empty list is not null:

(R* (is.null (list)))
 => #f

NULL, on the other hand:

(R* (is.null ,R-null))
 => #t

R

[syntax] (R expression ...) → R-object

Evaluate R-expressions, but do not try to translate the final result into a native Scheme object; this is useful when you don't need to manipulate the object directly in Scheme.

expression
An expression to evaluate
...
More expressions
(define-syntax
  R
  (lambda (expression rename compare)
    `(begin
       ,@(map (lambda (expression) `(R-eval ,(list 'quasiquote expression)))
              (cdr expression)))))
Examples

An example from ggplot2; see here:

(R (library "ggplot2")
   (plot
    (qplot (factor ($ mtcars cyl))
           ($ mtcars wt)
           xlab:
           "Number of cylinders"
           ylab:
           "Weight (lb/1000)"
           main:
           "1974 Motor Trend car-comparison"
           data:
           mtcars
           geom:
           (c "boxplot" "jitter"))))
 => #<tagged pointer sexp 1ef0dc0>

Another plotting example; see here:

(let ((x (R (sort (rnorm 47)))))
  (R
   (plot ,x xlab: "i" ylab: "Random normals" type: "s" main: "Primitive ECDF")
   (points ,x cex: 0.5 col: "dark red")))
 => #<tagged pointer sexp 9f90a8>

R*

[syntax] (R* expression ...) → Scheme-object

Evaluate R-expressions and translate the final result into a Scheme object, where possible (cf. overview); use this (as opposed to R) when you need to manipulate the value in Scheme.

expression
An expression to evaluate
...
More expressions
(define-syntax
  R*
  (lambda (expression rename compare) `(R->scheme (R ,@(cdr expression)))))
Examples

An example using classical statistics:

(let* ((x (R (runif 100 0 10))) (y (R (+ 2 (+ (* 3 ,x) (rnorm 100)))))
                                (df (R (data.frame x: ,x y: ,y)))
                                (d (R (lm (as.formula "y ~ x") data: ,df))))
  (R* ($ (summary ,d) "cov.unscaled")))
 => #(0.0372435687882782   -0.00576762230195307 -0.00576762230195307
 =>    0.00122103925798074)

About this egg

Author

Peter Danenberg

Colophon

Documented by cock.