Outdated egg!
This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.
If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.
R
Interface to R
TOC »
R
[module] R
The R-module provides functions for applying and evaluating R-expressions.
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
- NAconstant
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
- R-missingconstant
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
- R-nullconstant
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
- (R expression ...) → R-objectsyntax
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 3951738>
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 1510958>
R*
- (R* expression ...) → Scheme-objectsyntax
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.0423305190964232 -0.0064350404228672 -0.0064350404228672 => 0.00128082525122574)
About this egg
Author
Repository
https://github.com/klutometis/R
License
BSD
Dependencies
- big-chicken
- call-with-environment-variables
- define-record-and-printer
- hahn
- matchable
- moremacros
- numbers
- shell
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.
- 0.3.7
- Remove dependency on setup-helper-cock.
- 0.3.8
- Remove the dependency on debug.
- 0.3.9
- Remove the `debug?' parameter.
- 0.3.10
- Evaluate the examples.
- 0.3.11
- Use hahn.
- 0.3.12
- Specify hahn-version.
Colophon
Documented by hahn.