add *r-interactive* variable -- set to 1 if working interactively in Common Lisp
[rclg.git] / rclg-demo.lisp
blob7e3dc1f756f7282bc34baa210b5ee3b56594bdf7
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*
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)
42 (r "Cstack_info")
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
46 ;; lisp application.
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
51 ;; or for csh/tcsh
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:
61 ;;
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)
65 ;;
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
69 ;;
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.
75 ;;
76 ;; Protection/unprotection controls whether R can GC the sexp.
77 ;;
78 ;; Example:
79 ;;
80 ;; CL-USER> (defparameter *x* (r seq 1 10))
81 ;; *X*
82 ;; CL-USER> (defparameter *y* (rnbi rnorm 10))
83 ;; *Y*
84 ;; CL-USER> *y*
85 ;; #<sexp at 0x89A0238, PROTECT=R-PROTECT-UNTIL-USED>
86 ;; CL-USER> (r plot *x* *y*)
87 ;; NIL
88 ;; NIL
89 ;; CL-USER> *y*
90 ;; #<sexp at 0x89A0238, PROTECT=NIL>
92 ;; code used above
93 (defparameter *x* (r seq 1 10))
94 (defparameter *y* (rnbi rnorm 10))
95 (defparameter *y* (r rnorm 10))
96 *y*
97 (r plot *x* *y*)
98 *y*
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))
107 ;; Notes:
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
118 ;; get out of sync.
121 ;; Examples of function use:
124 (r "Sys.getenv" "LD_LIBRARY_PATH")
125 (r "Sys.getenv" "LD_PRELOAD")
127 (r "ls")
128 (r "search")
130 (elt (r "search") 3)
132 (r "geterrmessage")
135 ;; These don't work if we have library problems.
136 (r "library" "stats")
137 (r library "MASS")
139 ;; (r "library" "Biobase") ;; Terminates if doesn't exists -- need to try/catch!
141 (setf my.lib "Biobase")
142 my.lib
143 (r library my.lib)
145 (r "ls")
147 (r "print.default" 3)
148 (r "rnorm" 10)
150 ;; Working in the R space
152 (r assign "x" 5)
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
157 (r "ls")
158 ;;(r "x2") ;halted "Error could not find function \"x2\""
160 (setf my.r.x2 (r get "x2")) ;; moving data from R to CL
161 my.r.x2
162 (r assign "x2" my.r.x2) ;; moving data from CL to R
164 (r "get" "x")
165 (r get "x2")
166 (r "get" "z")
167 (r get "z")
168 (r "ls")
171 (r assign "my.x" (r rnorm 10))
172 (r assign "my.x" (rnb rnorm 10))
174 (r get "my.x")
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))
202 ;; for instance.
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"))
212 (r get "my.df")
213 (r summary "my.df")
216 ;; however the following will work...
218 (rnb data.frame
219 :x (r rnorm 10)
220 :y (r rnorm 10)) ; fine
221 (r data.frame
222 :x (r rnorm 10)
223 :y (r rnorm 10)) ; fine
226 (r summary (r t (r data.frame
227 :x (r rnorm 10)
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.
239 (r lm
240 :formula (rnb as.formula "x ~ y")
241 :data (rnb data.frame
242 :x (r rnorm 10)
243 :y (r rnorm 10)))
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.
254 ;;; Local variables:
255 ;;; mode: outline-minor
256 ;;; outline-header-prefix: ";;;"
257 ;;; End: