You are looking at historical revision 27637 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 (paste (c 1 ,NA))))
 => #(#f
 =>   #f)

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 (,(string->symbol "[") 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 an R-expression, but do not try to translate the result into a native Scheme object; this is useful when you don't need to manipulate the object directly in Scheme.

expression
The expression to evaluate
(define-syntax
  R
  (lambda (expression rename compare)
    (list 'apply 'R-eval (list 'quasiquote (cdr expression)))))
Examples

An example from ggplot2:

(R (library "ggplot2"))
 => #<tagged pointer sexp 1bb79d8>

(R
 (plot
  (qplot (factor ($ mtcars cyl))
         ($ mtcars wt)
         data:
         mtcars
         geom:
         (c "boxplot" "jitter"))))
 => #<tagged pointer sexp 1c099f0>

Another plotting example:

(let ((x (R (sort (rnorm 47)))))
  (R (plot ,x xlab: "x" type: "s" main: "plot(x, type = \"s\")"))
  (R (points ,x cex: 0.5 col: "dark red")))
 => #<tagged pointer sexp 17b90a8>

R*

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

Evaluate an R-expression and translate it into a Scheme object, where possible; use this (as opposed to R) when you need to manipulate the value in Scheme.

expression
The expression to evaluate
(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.0463194896194369  -0.0073337478838232 -0.0073337478838232
 =>    0.00148085390480537)

About this egg

Author

Peter Danenberg

Colophon

Documented by cock.