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