1 2:- use_module(library(lists)). % member/2. 3:- use_module(library(readutil)). % read_line_to_codes/2. 4 5:- nl, nl. 6:- ( r_bin(Rbin) -> 7 write( 'Will be using the R found at: ' ), nl, 8 write( Rbin ), nl, nl 9 ; 10 write( 'This session cannot locate an R executable. Please register the location' ), nl, 11 write( 'of the R executalbe using r_bin/1 before you can run the demos.' ), nl, nl 12 ). 13 14:- write( 'Demo predicates for R (r_session) package.' ), nl. 15:- write( 'See r_demo_1/0,...,r_demo_10/0.' ), nl. 16:- write( 'The goal r_demo/0 is a shorthand for r_demo_1/0,...,r_demo_7/0 which are the main demos.' ), nl, nl. 17:- write( 'r_demo_all/0 and r_demo_clauses/0 for r_demo_1,...,r_demo_10.' ), nl. 18:- write( 'which include demos for some non-basic features.' ), nl. 19:- write( 'You need to look at the sources before running r_demo_8,9 and 10.' ). 20:- nl, nl. 21 22r_demo :- 23 nl, nl, 24 Rdemos = [r_demo_1,r_demo_2,r_demo_3,r_demo_4,r_demo_5,r_demo_6,r_demo_7], 25 r_demo( Rdemos, false ). 26 27r_demo_all:- 28 nl, nl, 29 Rdemos = [r_demo_1,r_demo_2,r_demo_3,r_demo_4,r_demo_5,r_demo_6,r_demo_7,r_demo_8,r_demo_9,r_demo_10], 30 r_demo( Rdemos, false ). 31 32r_demo_clauses :- 33 nl, nl, 34 Rdemos = [r_demo_1,r_demo_2,r_demo_3,r_demo_4,r_demo_5,r_demo_6,r_demo_7,r_demo_8,r_demo_9,r_demo_10], 35 r_demo( Rdemos, true ). 36 37r_demo( Rdemos, Clauses ) :- 38 member(Wh, Rdemos ), 39 write( doing-Wh ), nl, 40 ( Clauses == true -> 41 write( 'Clauses: ' ), nl, 42 findall( Wh-Body, (clause(Wh,Body), 43 portray_clause((Wh:-Body)), nl), _ ) 44 ; 45 true 46 ), 47 ( call(Wh) -> 48 true 49 ; 50 write( 'Demo ended with failure.' ), nl 51 ), 52 nl, nl, 53 fail. 54r_demo( _Rdemos, _ ) :- 55 write( done ), nl. 56 57r_demo_1 :- 58 write( 'Demo: basic vector interactions.' ), nl, nl, 59 r_open, 60 r_in( x <- c(10.4, 5.6, 3.1, 6.4, 21.7) ), 61 ( r_out( print(x), Lines ), r_lines_print( Lines ), fail; true ), 62 r_print( x ), 63 r_in( x ), 64 r_in( (y <- c(6,5,4,3,2,1); y) ), % The extra paranthesis are only 65 % needed for Yap. 66 r_in( Z <- c(10.4, 5.6, 3.1, 6.4, 21.7) ), 67 write( z(Z) ), nl, 68 r_close. 69 70r_demo_2 :- 71 write( 'Demo: plots (screen and postscript).' ), nl, nl, 72 r_open, 73 r_in( y <- rnorm(50) ), 74 r_print( y ), 75 r_in( Y <- y ), 76 write( y(Y) ), nl, 77 r_in( x <- rnorm(y) ), 78 r_print( x ), 79 r_in( X <- x ), 80 write( x(X) ), nl, 81 r_in( x11(width=5,height=3.5) ), 82 r_in( plot(x,y)), 83 write( 'Press Return to continue...' ), nl, 84 current_prolog_flag( version, V ), 85 ( integer(V) -> User = current_input % SWI Prolog 86 ; User = user ), 87 read_line_to_codes( User, _ ), 88 r_in( 'dev.off()' ), 89 r_in( 'postscript(file="x_vs_y.eps")' ), 90 r_in( plot(x,y)), 91 r_in( 'dev.off()' ), 92 r_close, 93 !, % Swi leaves a backtracking point at read_line_to_codes/2 94 write( 'Check that file x_vs_y.eps has been created.' ), nl. 95 96r_demo_3 :- 97 write( 'Demo: aliases.' ), nl, nl, 98 r_open( [alias(mamonaku)] ), 99 ( current_r_session(Alias),write(session(Alias)),nl, fail; true ), 100 r_in( mamonaku, x <- c(10.4, 5.6, 3.1, 6.4, 21.7) ), 101 r_print( mamonaku, x ), 102 r_close( mamonaku ). 103 104r_demo_4 :- 105 write( 'Demo: history.' ), nl, nl, 106 r_open, 107 ( r_history(A,B), write(history(A,B)), nl, fail; true ), 108 r_in( x <- c(10.4, 5.6, 3.1, 6.4, 21.7) ), 109 ( r_out( print(x), Lines ), r_lines_print( Lines ), fail; true ), 110 r_print( x ), 111 ( r_history(C,D), write(h(C,D)), nl, fail; true ), 112 r_close. 113 114r_demo_5 :- 115 write( 'Demo: calls to R functions.' ), nl, nl, 116 r_open, 117 r_in( i <- 0:14 ), 118 r_print( i ), 119 r_in( I <- i ), 120 write( 'I'(I) ), nl, 121 r_in( x <- i/10 ), 122 r_in( y <- c(176.547,194.2552,218.5462,441.3706,795.786,1190.8606,1321.0128,1326.4694,1437.3068,1364.6906,1343.768,1513.7298,1553.8264,1554.1748,1549.399) ), 123 r_print( (integrate(splinefun(x,y), 0.2, 0.4)) ), 124 r_close. 125 126r_demo_6 :- 127 write( 'Demo: copying output and error on to file.' ), nl, nl, 128 r_open( [copy('rec_both.txt',both)] ), 129 r_in( x <- c(10.4, 5.6, 3.1, 6.4, 21.7) ), 130 r_print( x ), 131 write( 'Check that file rec_both.txt has been created.' ), nl, 132 r_close. 133 134r_demo_7 :- 135 write( 'Demo: error on R.' ), nl, nl, 136 r_open( [at_r_halt(restart)] ), 137 r_in( x <- c(10.4, 5.6, 3.1, 6.4, 21.7) ), 138 r_print( x ), 139 r_print( y ), 140 r_print( x ), 141 r_close. 142 143%%% Cut-off 144 145r_demo_8 :- 146 write( 'Demo: reinstate on halt.' ), nl, 147 write( 'This is no longer valid.' ), nl, nl, 148 r_open( [at_r_halt(reinstate)] ), 149 r_in( x <- c(10.4, 5.6, 3.1, 6.4, 21.7) ), 150 r_print( y ), 151 % here slave dies 152 % and r_session tries to restar_demo it and replay all commands. 153 r_print( x ), 154 % succeeds 155 r_close. 156 157/* change 192.168.0.* to a host in your domain before running the following. */ 158r_demo_9 :- 159 write( 'Demo: ssh on a machine with R on a different location.' ), nl, nl, 160 r_open( [ssh('192.168.0.3')] ), 161 r_in( I <- 0:14 ), 162 write( 'I'(I) ), nl, 163 r_close. 164 165r_demo_10 :- 166 write( 'Demo: ssh on a machine with explicit set of the remote R location.' ), 167 nl, nl, 168 r_bin( '/usr/local/users/nicos/local/bin/R' ), 169 r_open( [ssh('192.168.0.3')] ), 170 r_in( I <- 0:14 ), 171 write( 'I'(I) ), nl, 172 r_close. 173 174/* 175% You can replace any of the above r_open/0,1, with one of the following 176r_open( [with(restore)] ). 177 % do not use --no-restore on the R flags 178r_open( [copy(copied,both)] ). 179 % copy both input and output to file copied 180r_open( [at_r_halt(restar_demo),alias(mamunaku),copy(copied_in,in)] ). 181 % copy input to file copied_in 182r_open( [at_r_halt(restar_demo),alias(mamunaku),copy(copy_out,out)] ). 183 % copy output to file copied_out 184 */ 185