1 ;; What is this talk of 'release'? Klingons do not make software
2 ;; 'releases'. Our software 'escapes' leaving a bloody trail of
3 ;; designers and quality assurance people in it's wake.
9 ;; if needed...? Shouldn't be, since rclg.asd ought to take care of
10 ;; most of the issues that we have.
11 ;; (asdf:operate 'asdf:compile-op 'cffi :force t)
12 ;; (asdf:operate 'asdf:compile-op 'cffi)
13 ;; (asdf:operate 'asdf:compile-op 'rclg :force t)
14 ;; (asdf:operate 'asdf:compile-op 'rclg)
16 ;;(asdf:operate 'asdf:load-op 'cffi)
17 (asdf:operate
'asdf
:load-op
'rclg
)
19 ;;;#2 Go to where the functions are
21 (in-package :rclg-user
)
23 ;; Have we started yet?
24 rclg-init
::*r-started
*
25 rclg-init
::*r-interactive
*
27 ;;;#3 Start R within Lisp
29 (start-rclg) ; => 0 ; if works, nil otherwise.
32 ;; but if it fails, it could be related to...
35 ;; and now we make sure it's working
37 ;; rclg-init::*r-started*
38 (setf rclg-init
::*r-interactive
* 1) ; needed so that plot is interactive
40 (rclg-init::check-stack
)
44 ;; library problems can cause things to fail here. libR.so needs to
45 ;; be in the LD_LIBRARY_PATH prior to initialization of the common
48 ;; For example, on Debian, you will need to add "/usr/lib/R/lib" to
49 ;; the LD_LIBRARY_PATH environmental variable, i.e. for sh/bash/zsh:
50 ;; export LD_LIBRARY_PATH=/usr/lib/R/lib:$LD_LIBRARY_PATH
52 ;; setenv LD_LIBRARY_PATH "/usr/lib/R/lib:$LD_LIBRARY_PATH"
53 ;; (not sure about the above, but it's something like that).
56 ;;;#4 Demonstration of commands
60 ;; Basically, you now have three choices:
62 ;; r --- calls R, and converts the result back to CL as best as it
63 ;; can. If it can't convert, returns an unprotected sexp
64 ;; (probably a bug, probably should be protected)
66 ;; rnb --- R no backconvert. Calls R, and returns a protected
67 ;; unconverted R sexp. Useful when you want to manipulate
68 ;; something on the R side and give it a CL name
70 ;; rnbi --- R no backconvert internal. Calls R, returns a protected
71 ;; uncoverted R sexp. However, it's tagged differently, and
72 ;; as soon as you use this as an argument to a function, it
73 ;; unprotects the sexp. Useful for holding anonymous
74 ;; intermediate R results you don't want to backconvert.
76 ;; Protection/unprotection controls whether R can GC the sexp.
80 ;; CL-USER> (defparameter *x* (r seq 1 10))
82 ;; CL-USER> (defparameter *y* (rnbi rnorm 10))
85 ;; #<sexp at 0x89A0238, PROTECT=R-PROTECT-UNTIL-USED>
86 ;; CL-USER> (r plot *x* *y*)
90 ;; #<sexp at 0x89A0238, PROTECT=NIL>
93 (defparameter *x
* (r seq
1 10))
94 (defparameter *y
* (rnbi rnorm
10))
95 (defparameter *y
* (r rnorm
10))
100 ;; This is for illustrative purposes only. It is not a "good" use of rnbi.
101 ;; Really, you'll want rnbi to hold anonymous intermeditae results, like:
103 (r plot
*x
* (rnbi rnorm
10))
104 (r hist
(r rnorm
10))
105 (r plot
(list 1 2 3 2 3 4 5))
109 ;; If the user protects the result of a call with rnb, it is the
110 ;; user's responsibility to delete the sexp when it's no longer needed,
111 ;; using rclg-control:unprotect-sexp. (It might be better to use a
112 ;; modification of the old safe version that's lying around.)
114 ;; There is no way to ask R whether an sexp is protected or not.
115 ;; Therefore, there is no real way to enforce the protection. If the user
116 ;; goes around the API and calls %rf-unprotect-ptr or messes with the
117 ;; description slot (slot-value sexp-holder 'protected), things can easily
121 ;; Examples of function use:
124 (r "Sys.getenv" "LD_LIBRARY_PATH")
125 (r "Sys.getenv" "LD_PRELOAD")
135 ;; These don't work if we have library problems.
136 (r "library" "stats")
139 ;; (r "library" "Biobase") ;; Terminates if doesn't exists -- need to try/catch!
141 (setf my.lib
"Biobase")
147 (r "print.default" 3)
150 ;; Working in the R space
153 (r assign
"x2" (list 1 2 3 5))
154 (r plot
"x2" "x2") ; BOMBO.
155 (r assign
"x2" #(1 2 3 5 3 4 5))
156 (r assign
"z" "y") ;; unlike the above, this assigns character data
158 ;;(r "x2") ;halted "Error could not find function \"x2\""
160 (setf my.r.x2
(r get
"x2")) ;; moving data from R to CL
162 (r assign
"x2" my.r.x2
) ;; moving data from CL to R
171 (r assign
"my.x" (r rnorm
10))
172 (r assign
"my.x" (rnb rnorm
10))
179 ;; More sophisticated computation
181 (r "plot" #(2 3 3 2 1) #(3 5 7 3 2))
183 (r plot
(list 1 2 3 4 5) (list 1 2 3 4 5) :main
"My title")
184 (r plot
:x
(list 1 2 3 4 5) :y
(list 5 4 3 4 5) :main
"My title")
186 (r plot
:y
(list 5 4 3 4 5) :x
(list 1 2 3 4 5) :main
"My title")
188 (r plot
(rnb rnorm
10) (rnb rnorm
10)
189 :main
"silly" :xlab
"xlabel" :ylab
"ylabel")
191 (aref (r rnorm
10) 3) ;; pull out the 3rd value
194 ;; create a CL function r-hist that calls the R function hist on a
195 ;; sequence, returning no results. The keywords :main and :xlab are
196 ;; passed with default values nil, and the other keywords are passed with
197 ;; the chosen values.
198 (def-r-call (r-hist hist
:no-result sequence
) main
199 xlab
(breaks 50) (probability t
) (col "blue"))
200 ;; then the function can be called:
201 (r-hist (rnbi rnorm
1000))
205 ;;;#5 Here is the TO MAKE WORK list (really, applications/tasks) that
206 ;;; need to work (i.e. be do-able).
209 ;;; a. Need to be able to read in datasets and summarize
211 (r assign
"my.df" (r read.table
"testdata.csv"))
216 ;; however the following will work...
220 :y
(r rnorm
10)) ; fine
223 :y
(r rnorm
10)) ; fine
226 (r summary
(r t
(r data.frame
228 :y
(r rnorm
10)))) ; fine ; fine ; no.
230 ;;; b. Need to be able to work with formulas as objects
232 (rnb as.formula
"x ~ y") ; fine
233 (rnbi as.formula
"x ~ y") ; fine
234 (r as.formula
"x ~ y") ; barfs
236 ;;; c. and the last is important so that we can easily fit models, so
237 ;;; it needs to be fixed.
240 :formula
(rnb as.formula
"x ~ y")
241 :data
(rnb data.frame
245 ;;; d. How to handle connections?
246 ;;; e. How to handle S4 objects?
247 ;;; f. Hooks and finishing up conversion tools?
250 ;;; how do we terminate the R session?
251 (r "q" "y") ;; fails.
255 ;;; mode: outline-minor
256 ;;; outline-header-prefix: ";;;"