R

Interface to R

  1. R
    1. Overview
    2. Documentation
      1. NA
        1. Examples
      2. R-missing
        1. Examples
      3. R-null
        1. Examples
      4. R
        1. Examples
      5. R*
        1. Examples
    3. About this egg
      1. Author
      2. Repository
      3. License
      4. Dependencies
      5. Versions
      6. Colophon

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 4550d88>

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 2332458>

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.0376763802711702  -0.0056175779273358 -0.0056175779273358
 =>    0.0011402207030145)

About this egg

Author

Peter Danenberg

Repository

https://github.com/klutometis/R

License

BSD

Dependencies

Versions

0.1
Initial release
0.1.1
Add cock.
0.1.2
Clean up animation code.
0.1.3
Set R_HOME, if necessary.
0.2
R- and R*-forms.
0.2.1
Use the new R-eval forms.
0.2.2
With a note about cock-utils
0.3
NA, NaN, inf
0.3.1
Rid of aima; NA in lists
0.3.2
Some examples in the docs
0.3.3
Using sjamaan's idea about integer instead of int for 32-bit machines.
0.3.4
Multiple R-expressions
0.3.5
Blindly trying to fix NA for 32-bit.
0.3.6
Add test-exit.

Colophon

Documented by cock.