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