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