debugging aids.
[rclg.git] / rclg-demo.lisp
blob6ec587baa222866f8b0298a5449ea150599f6357
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.
7 ;;;#1 Load everything
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*
26 ;;;#3 Start R within Lisp
28 (start-rclg)
30 ;; but if it fails, it could be related to...
33 ;; and now we make sure it's working
35 ;; rclg-init::*r-started*
37 (rclg-init::check-stack)
39 (r "Cstack_info")
41 ;; library problems can cause things to fail here. libR.so needs to
42 ;; be in the LD_LIBRARY_PATH prior to initialization of the common
43 ;; lisp application.
45 ;; For example, on Debian, you will need to add "/usr/lib/R/lib" to
46 ;; the LD_LIBRARY_PATH environmental variable, i.e. for sh/bash/zsh:
47 ;; export LD_LIBRARY_PATH=/usr/lib/R/lib:$LD_LIBRARY_PATH
48 ;; or for csh/tcsh
49 ;; setenv LD_LIBRARY_PATH "/usr/lib/R/lib:$LD_LIBRARY_PATH"
50 ;; (not sure about the above, but it's something like that).
53 ;;;#4 Demonstration of commands
57 ;; Basically, you now have three choices:
58 ;;
59 ;; r --- calls R, and converts the result back to CL as best as it
60 ;; can. If it can't convert, returns an unprotected sexp
61 ;; (probably a bug, probably should be protected)
62 ;;
63 ;; rnb --- R no backconvert. Calls R, and returns a protected
64 ;; unconverted R sexp. Useful when you want to manipulate
65 ;; something on the R side and give it a CL name
66 ;;
67 ;; rnbi --- R no backconvert internal. Calls R, returns a protected
68 ;; uncoverted R sexp. However, it's tagged differently, and
69 ;; as soon as you use this as an argument to a function, it
70 ;; unprotects the sexp. Useful for holding anonymous
71 ;; intermediate R results you don't want to backconvert.
72 ;;
73 ;; Protection/unprotection controls whether R can GC the sexp.
74 ;;
75 ;; Example:
76 ;;
77 ;; CL-USER> (defparameter *x* (r seq 1 10))
78 ;; *X*
79 ;; CL-USER> (defparameter *y* (rnbi rnorm 10))
80 ;; *Y*
81 ;; CL-USER> *y*
82 ;; #<sexp at 0x89A0238, PROTECT=R-PROTECT-UNTIL-USED>
83 ;; CL-USER> (r plot *x* *y*)
84 ;; NIL
85 ;; NIL
86 ;; CL-USER> *y*
87 ;; #<sexp at 0x89A0238, PROTECT=NIL>
89 ;; code used above
90 (defparameter *x* (r seq 1 10))
91 (defparameter *y* (rnbi rnorm 10))
92 *y*
93 (r plot *x* *y*)
94 *y*
96 ;; This is for illustrative purposes only. It is not a "good" use of rnbi.
97 ;; Really, you'll want rnbi to hold anonymous intermeditae results, like:
99 (r plot *x* (rnbi rnorm 10))
101 ;; Notes:
103 ;; If the user protects the result of a call with rnb, it is the
104 ;; user's responsibility to delete the sexp when it's no longer needed,
105 ;; using rclg-control:unprotect-sexp. (It might be better to use a
106 ;; modification of the old safe version that's lying around.)
108 ;; There is no way to ask R whether an sexp is protected or not.
109 ;; Therefore, there is no real way to enforce the protection. If the user
110 ;; goes around the API and calls %rf-unprotect-ptr or messes with the
111 ;; description slot (slot-value sexp-holder 'protected), things can easily
112 ;; get out of sync.
115 ;; Examples of function use:
118 (r "Sys.getenv" "LD_LIBRARY_PATH")
119 (r "Sys.getenv" "LD_PRELOAD")
121 (r "ls")
122 (r "search")
124 (r "geterrmessage")
127 ;; These don't work if we have library problems.
128 (r "library" "stats")
129 (r library "MASS")
130 (r "library" "Biobase")
132 (setf my.lib "Biobase")
133 my.lib
134 (r library my.lib)
136 (r "ls")
138 (r "print.default" 3)
139 (r "rnorm" 10)
141 ;; Working in the R space
143 (r assign "x" 5)
144 (r assign "x2" (list 1 2 3 5))
146 (r assign "x2" #(1 2 3 5 3 4 5))
147 (r assign "z" "y") ;; unlike the above, this assigns character data
148 (r "ls")
150 (setf my.r.x2 (r get "x2")) ;; moving data from R to CL
151 my.r.x2
152 (r assign "x2" my.r.x2) ;; moving data from CL to R
154 (r "get" "x")
155 (r get "x2")
156 (r "get" "z")
157 (r get "z")
158 (r "ls")
161 (r assign "my.x" (r rnorm 10))
162 (r assign "my.x" (rnb rnorm 10))
164 (r get "my.x")
169 ;; More sophisticated computation
171 (r "plot" #(2 3 3 2 1) #(3 5 7 3 2))
173 (r plot (list 1 2 3 4 5) (list 1 2 3 4 5) :main "My title")
174 (r plot :x (list 1 2 3 4 5) :y (list 5 4 3 4 5) :main "My title")
176 (r plot :y (list 5 4 3 4 5) :x (list 1 2 3 4 5) :main "My title")
178 (r plot (rnb rnorm 10) (rnb rnorm 10)
179 :main "silly" :xlab "xlabel" :ylab "ylabel")
181 (aref (r rnorm 10) 3) ;; pull out the 3rd value
184 ;; create a CL function r-hist that calls the R function hist on a
185 ;; sequence, returning no results. The keywords :main and :xlab are
186 ;; passed with default values nil, and the other keywords are passed with
187 ;; the chosen values.
188 (def-r-call (r-hist hist :no-result sequence) main
189 xlab (breaks 50) (probability t) (col "blue"))
190 ;; then the function can be called:
191 (r-hist (rnbi rnorm 1000))
192 ;; for instance.
195 ;;;#5 Here is the TO MAKE WORK list (really, applications/tasks) that
196 ;;; need to work (i.e. be do-able).
199 ;;; a. Need to be able to read in datasets and summarize
201 (r assign "my.df" (r read.table "testdata.csv"))
202 (r get "my.df")
203 (r summary "my.df")
206 ;; however the following will work...
208 (rnb data.frame
209 :x (r rnorm 10)
210 :y (r rnorm 10)) ; fine
211 (r data.frame
212 :x (r rnorm 10)
213 :y (r rnorm 10)) ; fine
216 (r summary (r t (r data.frame
217 :x (r rnorm 10)
218 :y (r rnorm 10)))) ; fine ; fine ; no.
220 ;;; b. Need to be able to work with formulas as objects
222 (rnb as.formula "x ~ y") ; fine
223 (rnbi as.formula "x ~ y") ; fine
224 (r as.formula "x ~ y") ; barfs
226 ;;; c. and the last is important so that we can easily fit models, so
227 ;;; it needs to be fixed.
229 (r lm
230 :formula (rnb as.formula "x ~ y")
231 :data (rnb data.frame
232 :x (r rnorm 10)
233 :y (r rnorm 10)))
235 ;;; d. How to handle connections?
236 ;;; e. How to handle S4 objects?
237 ;;; f. Hooks and finishing up conversion tools?
240 ;;; how do we terminate the R session?
241 (r "q" "y") ;; fails.
244 ;;; Local variables:
245 ;;; mode: outline-minor
246 ;;; outline-header-prefix: ";;;"
247 ;;; End: