/usr/share/Yap/r_session.yap is in yap 6.2.2-6+b2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 | :- module( r_session,
[
r_open/0, r_open/1,
r_close/0, r_close/1,
r_in/1, r_in/2,
r_push/1, r_push/2,
r_out/2, r_out/3,
r_err/3, r_err/4,
r_print/1, r_print/2,
r_lines_print/1, r_lines_print/2, r_lines_print/3,
r_lib/1, r_lib/2,
r_flush/0, r_flush/1,
r_flush_onto/2, r_flush_onto/3,
current_r_session/1, current_r_session/3,
default_r_session/1,
r_session_data/3, r_streams_data/3,
r_history/0, r_history/1, r_history/2,
r_session_version/1,
r_bin/1,
r_bin_version/1, r_bin_version/2,
r_verbosity/1,
op( 950, xfx, (<-) )
] ).
:- use_module( library(lists) ).
:- use_module( library(readutil) ). % read_line_to_codes/2.
:- ( current_predicate(r_verbosity_level/1) -> true;
assert(r_verbosity_level(0)) ).
:- dynamic( r_bin_location/1 ).
:- dynamic( r_session/3 ).
:- dynamic( r_session_history/2 ).
:- dynamic( r_old_bin_warning_issued/1 ).
:- dynamic( r_bin_takes_interactive/2 ).
% Yap declaration:
:- ensure_loaded( library(system) ). % exec/3, file_exists/1, directory_files/2.
:- ensure_loaded( library(apply_macros) ). % maplist/3
% end of Yap declaration.
/** <module> R session
This library facilitates interaction with the R system for statistical
computing. It assumes an R executable in $PATH or can be given a location
to a functioning R executable (see r_bin/1 and r_open/1 for details on how
R is located). R is ran as a slave with Prolog writing on and reading from
the associated streams. Multiple sessions can be managed simultaneously.
Each has 3 main components: a name or alias, a term structure holding the
communicating streams and a number of associated data items.
The library attempts to ease the translation between prolog terms and R
inputs. Thus, Prolog term =|x <- c(1,2,3)|= is translated to atomic =|'x
<- c(1,2,3)'|= which is then passed on to R. That is, =|<-|= is a
defined/recognised operator. =|X <- c(1,2,3)|=, where X is a variable,
instantiates X to the list =|[1,2,3]|=. Also 'Atom' <- [x1,...,xn]
translates to R code: Atom <- c(x1,...,xn). Currently only vectors can be
translated in this fashion.
Although the library is primarily meant to be used as a research tool,
it still provides access to many functions of the R system that may render it
useful to a wider audience. The library provides access to R's plethora of vector and scalar
functions. We adicipate that of particular interest to Prolog programmers might be the fact
that the library can be used to create plots from Prolog objects.
Notably creating plots from lists of numbers.
These capabilities are illustrated in the following example :
==
rtest :-
r_open,
r_in( y <- rnorm(50) ),
r_print( y ),
r_in( x <- rnorm(y) ),
r_in( x11(width=5,height=3.5) ),
r_in( plot(x,y)),
write( 'Press Return to continue...' ), nl,
read_line_to_codes( user, _ ),
r_print( 'dev.off()' ),
r_in( Y <- y ),
write( y(Y) ), nl,
Z = [1,2,3,4,5,6,7,8,9],
r_in( z <- Z ),
r_print( z ),
r_close.
==
@author Nicos Angelopoulos
@version 0:0:4
@copyright Nicos Angelopoulos
@license YAP: Artistic
@see ensure_loaded(library('../doc/packages/examples/R/r_demo.pl'))
@see http://www.r-project.org/
@author Windows-compatibility is based on work by `JAB'
*/
%%% Section: Interface predicates
%% r_bin( ?Rbin )
%
% Register the default R location, +Rbin, or interrogate the current location: -Rbin.
% When interrogating Rbin is bound to the R binary that would be used by an r_open/0.
% The order of search is: registered location, environment variable 'R_BIN' and path defined.
% On unix systems path defined is the first R executable in $PATH. On MS wins it is the latest
% Rterm.exe found by expand_file_name( 'C:/Program Files/R/R-*/bin/Rterm.exe', Candidates ).
% The value Rbin == =retract= retracts the current registered location.
% Rbin == =test=, succeeds if an R location has been registered.
%
r_bin( Rbin ) :-
var( Rbin ),
!,
( r_bin_location(Rbin) ->
true
;
( locate_rbin_file(Rbin) ->
M = 'There is no registered R executable. Using the one found by searching.',
r_verbose( M, 1 )
;
M = 'There is no registered or default R executatble. Use, r_bin(+Rbin).',
fail_term( M )
)
).
r_bin( retract ) :-
!,
retractall( r_bin_location(_) ).
r_bin( test ) :-
!,
r_bin_location(_).
r_bin( Rbin ) :-
retractall( r_bin_location(_) ),
assert( r_bin_location(Rbin) ).
%% r_open
%
% Open a new R session. Same as r_open( [] ).
%
r_open :-
r_open( [] ).
%% r_open( +Opts )
%
% Open a new R session with optional list of arguments. Opts should be
% a list of the following
%
% * alias(Alias)
% Name for the session. If absent or a variable an opaque term is
% generated.
%
% * assert(A)
% Assert token. By default session opened last is the default
% session (see default_r_session/1). Using A = =z= will push the
% session to the bottom of the pile.
%
% * at_r_halt(RHAction)
% R slaves used to halt when they encounter an error.
% This is no longer the case but this option is still present in case
% it is useful in the future. This option provides a handle to changing
% the behaviour of the session when a halt of the R-slave occurs.
% RHAction should be one of =abort=, =fail=, call/1,
% call_ground/1, =reinstate= or =restart=. Default is =fail=. When
% RHAction is =reinstate=, the history of the session is used to
% roll-back all the commands sent so far. At `restart' the session
% is restarted with same name and options, but history is not
% replayed.
%
% * copy(CopyTo,CopyWhat)
% Records interaction with R to a file/stream. CopyTo should be
% one of =null=, stream(Stream), OpenStream, AtomicFile,
% once(File) or many(File). In the case of many(File), file is
% opened and closed at each write operation. CopyWhat should be
% one of =both=, =in=, =out= or =none=. Default is no recording
% (CopyTo = =null=).
%
% * ssh(Host)
% * ssh(Host,Dir)
% Run R on Host with start directory Dir. Dir defaults to /tmp.
% Not supported on MS Windows.
%
%
% * rbin(Rbin)
% R executable location to use for this open operation.
% If the option is not present binary registered with r_bin/1 and
% environment variable R_BIN are examined for the full location of
% the R binary. In MS windows Rbin should point to Rterm.exe. Also see r_bin/1.
%
% * with(With)
% With is in [environ,restore,save]. The default behaviour is to
% start the R executable with flags =|--no-environ --no-restore
% --no-save|=. For each With value found in Opts the corresponding
% =|--no-|= flag is removed.
%
r_open( Opts ) :-
r_open_1( Opts, _R, false ).
%% r_close
%
% Close the default R session.
%
r_close :-
( default_r_session( Alias ) ->
r_close( Alias )
;
fail_term( no_default_open_r_session_could_be_found_to_close )
).
%% r_close(+R)
%
% Close the named R session.
%
r_close( All ) :-
All == all,
!,
findall( Alias, ( retract( r_session(Alias,Streams,Data) ),
r_close_session( Alias, Streams, Data ) ), _AllAls ).
% write( closed_all(All) ), nl.
r_close( Alias ) :-
( retract( r_session(Alias,Streams,Data) ) ->
r_close_session( Alias, Streams, Data )
;
fail_term( no_open_r_session_could_be_found_to_close_at:Alias )
).
%% r_in(+Rcmd)
%
% Push Rcmd to the default R session. Output and Errors will be
% printed to the terminal.
%
r_in( This ) :-
default_r_session( R ),
r_in( R, This, _ ).
%% r_in(+R,+Rcmd)
%
% As r_in/1 but for session R.
%
r_in( R, PrvThis ) :-
r_in( R, PrvThis, _ ).
%% r_push(+Rcmd)
%
% As r_in/1 but does not consume error or output streams.
%
r_push( This ) :-
default_r_session( R ),
r_push( R, This ).
%% r_push(+R,+Rcmd)
%
% As r_push/1 but for named session.
%
r_push( R, RCmd ) :-
current_r_session( R, Streams, Data ),
r_session_data( copy_to, Data, CopyTo ),
r_session_data( copy_this, Data, CopyThis ),
r_streams( input, Streams, Ri ),
r_input_normative( RCmd, RNrm ),
write( Ri, RNrm ), nl( Ri ),
r_record_term( CopyThis, CopyTo, RNrm ).
%% r_out(+Rcmd,-Lines)
%
% Push Rcmd to default R session and grab output lines Lines as
% a list of code lists.
%
r_out( This, Read ) :-
default_r_session( R ),
r_out( R, This, Read ).
%% r_out(+R,+Rcmd,-Lines)
%
% As r_out/2 but for named session R.
%
r_out( R, RCmd, RoLns ) :-
r_push( R, RCmd, Rplc, RoLns, ReLns, Halt, HCall ),
r_lines_print( ReLns, error, user_error ),
r_record_history( Halt, R, RCmd ),
r_out_halted_record( Halt, R, RoLns ),
replace_variables( Rplc ),
call( HCall ).
%% r_err(+Rcmd,-Lines,-ErrLines)
%
% Push Rcmd to default R session and grab output lines Lines as
% a list of code lists. Error lines are in ErrLines.
%
r_err( This, Read, ErrRead ) :-
default_r_session( R ),
r_err( R, This, Read, ErrRead ).
%% r_err(+R,+Rcmd,-Lines, -ErrLines)
%
% As r_err/3 but for named session R.
%
r_err( R, RCmd, RoLns, ReLns ) :-
r_push( R, RCmd, Rplc, RoLns, ReLns, Halt, HCall ),
r_lines_print( ReLns, error, user_error ),
r_record_history( Halt, R, RCmd ),
r_out_halted_record( Halt, R, RoLns ),
replace_variables( Rplc ),
call( HCall ).
%% r_print(+X)
%
% A shortcut for r_in( print(X) ).
%
r_print( This ) :-
default_r_session( R ),
r_print( R, This ).
%% r_print(+R,+X)
%
% As r_print/1 but for named session R.
%
r_print( R, This ) :-
r_out( R, This, Read ),
r_lines_print( Read, output ).
%% r_lines_print( +Lines )
%
% Print a list of code lists (Lines) to the user_output.
% Lines would normally be read of an R stream.
%
r_lines_print( Lines ) :-
r_lines_print( Lines, output, user_output ).
%% r_lines_print( +Lines, +Type )
%
% As r_lines_print/1 but Type declares whether to treat lines
% as output or error response. In the latter case they are written
% on user_error and prefixed with '!'.
%
r_lines_print( Lines, Type ) :-
r_lines_print_type_stream( Type, Stream ),
r_lines_print( Lines, Type, Stream ).
%% r_lines_print( +Lines, +Type, +Stream )
%
% As r_lines_print/3 but Lines are written on Stream.
%
r_lines_print( [], _Type, _Stream ).
r_lines_print( [H|T], Type, Stream ) :-
atom_codes( Atm, H ),
r_lines_print_prefix( Type, Stream ),
write( Stream, Atm ), nl( Stream ),
r_lines_print( T, Type, Stream ).
%% r_lib(+L)
%
% A shortcut for r_in( library(X) ).
%
r_lib( Lib ) :-
default_r_session( R ),
r_lib( R, Lib ).
%% r_lib(+R,+L)
%
% As r_lib/1 but for named session R.
%
r_lib( R, Lib ) :-
r_in( R, library(Lib) ).
%% r_flush
%
% Flush default R's output and error on to the terminal.
%
r_flush :-
default_r_session( R ),
r_flush( R ).
%% r_flush(+R)
%
% As r_flush/0 but for session R.
%
r_flush( R ) :-
r_flush_onto( R, [output,error], [Li,Le] ),
r_lines_print( Li, output ),
r_lines_print( Le, error ).
%% r_flush_onto(+SAliases,-Onto)
%
% Flush stream aliases to code lists Onto. SAliases
% should be one of, or a list of, [output,error].
%
r_flush_onto( RinStreamS, OntoS ) :-
default_r_session( R ),
r_flush_onto( R, RinStreamS, OntoS ).
%% r_flush_onto(+R,+SAliases,-Onto)
%
% As r_flush_onto/2 for specified session R.
%
r_flush_onto( R, RinStreams, Ontos ) :-
( is_list(RinStreams) -> RStreams = RinStreams; RStreams=[RinStreams] ),
% to_list( RinStreamS, RinStreams ),
r_input_streams_list( RStreams ),
r_flush_onto_1( RStreams, R, ROntos ),
( is_list(RinStreams) -> Ontos = ROntos; Ontos=[ROntos] ).
%% current_r_session(?R)
% True if R is the name of current R session.
% Can be used to enumerate all open sessions.
%
current_r_session( R ) :-
var( R ),
!,
r_session( R, _Session, _Data ).
current_r_session( R ) :-
r_session( R, _Session, _Data ),
!.
current_r_session( R ) :-
fail_term( 'Could not find session':R ).
%% current_r_session(?R,?S,?D)
%
% True if R is an open session with streams S
% and data D (see introduction to the library).
%
current_r_session( Alias, R, Data ) :-
r_session( Alias, R, Data ).
%% default_r_session(?R)
%
% True if R is the default session.
%
default_r_session( R ) :-
( var(R) ->
( r_session(R,_Cp1,_Wh1) ->
true
;
fail_term( no_default_open_r_session_was_found )
)
;
( r_session(R,_Cp2,_Wh2) ->
true
;
fail_term( no_open_r_session_at(R) )
)
).
%% r_streams_data(+SId,+Streams,-S)
% True if Streams is an R session streams
% structure and S is its stream corresponding
% to identifier SId, which should be one of
% [input,output,error].
%
r_streams_data( input, r(Ri,_,_), Ri ).
r_streams_data( output, r(_,Ro,_), Ro ).
r_streams_data( error, r(_,_,Re), Re ).
%% r_session_data(+DId,+Data,-Datum)
%
% True if Data is a structure representing
% R session associated data and Datum is its
% data item corresponding to data identifier
% DId. DId should be in
% [copy_to,copy_this,at_r_halt,opts].
%
r_session_data( copy_to, rsdata(Copy,_,_,_), Copy ).
r_session_data( copy_this, rsdata(_,This,_,_), This ).
r_session_data( at_r_halt, rsdata(_,_,RHalt,_), RHalt ).
r_session_data( opts, rsdata(_,_,_,Opts), Opts ).
%% r_history
%
% Print on user_output the history of the default session.
%
r_history :-
default_r_session( R ),
r_session_history( R, History ),
reverse( History, Hicory ),
write( history(R) ), nl, write('---' ), nl,
( (member(H,Hicory),write(H),nl,fail) -> true; true ),
write( '---' ), nl.
%% r_history(-H)
%
% H unifies to the history list of the Rcmds fed into the default
% session. Most recent command appears at the head of the list.
%
r_history( History ) :-
default_r_session( R ),
r_session_history( R, History ).
%% r_history(?R,-H)
% As r_history/1 but for named session R.
% It can be used to enumerate all histories.
% It fails when no session is open.
%
r_history( R, History ) :-
r_session_history( R, History ).
%% r_session_version(-Version)
% Installed version. Version is of the form Major:Minor:Fix,
% where all three are integers.
%
r_session_version( 0:0:4 ).
%% r_verbose( What, CutOff )
%
r_verbose( What, CutOff ) :-
r_verbosity_level( Level ),
( CutOff > Level ->
true
;
write( What ), nl
).
%% r_verbosity( ?Level )
%
% Set, +Level, or interrogate, -Level, the verbosity level. +Level could be
% =false= (=0), =true= (=3) or an integer in {0,1,2,3}. 3 being the most verbose.
% The default is 0. -Level will instantiate to the current verbosity level,
% an integer in {0,1,2,3}.
%
r_verbosity( Level ) :-
var( Level ),
!,
r_verbosity_level( Level ).
r_verbosity( Level ) :-
( Level == true ->
Numeric is 3
;
( Level == false ->
Numeric is 0
;
( integer(Level) ->
( Level < 0 ->
write( 'Adjusting verbosity level to = 0. ' ), nl,
Numeric is 0
;
( Level > 3 ->
write( 'Adjusting verbosity level to = 3. ' ), nl,
Numeric is 3
;
Numeric is Level
)
)
;
fail_term( 'Unknown verbosity level. Use : true, false, 0-3' )
)
)
),
retractall( r_verbosity_level(_) ),
assert( r_verbosity_level(Numeric) ).
%% r_bin_version( -Version)
%
% Get the version of R binary identified by r_bin/1. Version will have the
% same structure as in r_session_version/1 ie M:N:F.
%
r_bin_version( Version ) :-
r_bin( R ),
r_bin_version( R, Version ).
%% r_bin_version( +Rbin, -Version )
%
% Get the version of R binary identified by +Rbin. Version will have the
% same structure as in r_session_version/1 ie M:N:F.
%
r_bin_version( R, Version ) :-
r_bin_version_pl( R, Version ).
%%% Section: Auxiliary predicates
% Rcv == true iff r_open_1/3 is called from recovery.
%
r_open_1( Opts, Alias, Rcv ) :-
( options_have_ssh(Opts,Host,Dir) ->
( current_prolog_flag(windows,true) ->
fail_term( ssh_option_not_supported_on_ms_windows )
;
which( ssh, Ssh )
)
;
true
),
( (memberchk(rbin(Rbin),Opts);locate_rbin(Ssh,Rbin)) ->
true
;
fail_term( 'Use rbin/1 in r_open/n, or r_bin(\'Rbin\') or set R_BIN.' )
),
r_bin_arguments( Opts, Rbin, OptRArgs ),
% ( var(Harg) -> RArgs = OptRArgs; RArgs = [Host,Harg|OptRArgs] ),
( var(Ssh) ->
Exec = Rbin,
Args = OptRArgs
;
Exec = Ssh,
% atoms_concat( [' "cd ',Dir,'; ',Rbin,'"'], Harg ),
atoms_concat( ['cd ',Dir,'; '], Cd ),
PreArgs = [Cd,Rbin|OptRArgs],
double_quote_on_yap( PreArgs, TailArgs ),
Args = [Host|TailArgs]
% atoms_concat( ['ssh ', Host,' "cd ',Dir,'; ',RBin,'"'], R )
),
% atom_concat( PrvExec, RPsf, Exec ),
r_verbose( r_process( Exec, Args, Ri, Ro, Re ), 3 ),
r_process( Exec, Args, Ri, Ro, Re ),
RStreams = r(Ri,Ro,Re),
r_streams_set( Ri, Ro, Re ),
r_process_was_successful( Ri, Ro, Re ),
r_open_opt_copy( Opts, CpOn, CpWh, Rcv ),
r_open_opt_at_r_halt( Opts, RHalt ),
( memberchk(alias(Alias),Opts) ->
( var(Alias) ->
r_session_skolem( Alias, 1 )
;
( r_session(Alias,_,_) ->
fail_term( 'Session already exists for alias':Alias )
;
true
)
)
;
r_session_skolem( Alias, 1 )
),
RData = rsdata(CpOn,CpWh,RHalt,Opts),
( memberchk(assert(Assert),Opts) ->
( Assert == a ->
asserta( r_session(Alias,RStreams,RData) )
;
( Assert == z ->
assertz( r_session(Alias,RStreams,RData) )
;
fail_term( 'Cannot decipher argument to assert/1 option':Assert )
)
)
;
asserta( r_session(Alias,RStreams,RData) )
),
AtRH = at_r_halt(reinstate),
( (memberchk(history(false),Opts),\+memberchk(AtRH,Opts)) ->
true
;
retractall( r_session_history(Alias,_) ),
assert( r_session_history(Alias,[]) )
).
r_close_session( Alias, Streams, Data ) :-
r_streams_data( input, Streams, Ri ),
r_streams_data( output,Streams, Ro ),
r_streams_data( error, Streams, Re ),
r_session_data( copy_to, Data, CopyTo ),
r_session_data( copy_this, Data, CopyThis ),
write( Ri, 'q()' ), nl( Ri ),
r_record_term( CopyThis, CopyTo, 'q()' ),
( (CopyTo=stream(CopyS),stream_property(CopyS,file_name(CopyF)),CopyF\==user)->
close(CopyS)
;
true
),
close( Ri ),
close( Ro ),
close( Re ),
retractall( r_session_history(Alias,_) ).
r_in( R, RCmd, Halt ) :-
r_push( R, RCmd, Rplc, RoLns, ReLns, Halt, HCall ),
r_out_halted_record( Halt, R, RoLns ),
r_lines_print( RoLns, output, user_output ),
r_lines_print( ReLns, error, user_error ),
r_record_history( Halt, R, RCmd ),
replace_variables( Rplc ),
call( HCall ).
r_push( R, RCmd, Rplc, RoLns, ReLns, Halt, HCall ) :-
current_r_session( R, Streams, Data ),
r_session_data( copy_to, Data, CopyTo ),
r_session_data( copy_this, Data, CopyThis ),
r_streams( input, Streams, Ri ),
r_input_normative( RCmd, R, 0, RNrm, Rplc, _ ),
write( Ri, RNrm ), nl( Ri ),
r_record_term( CopyThis, CopyTo, RNrm ),
r_lines( Streams, error, ReLns ),
r_halted( ReLns, R, Halt, HCall ),
( Halt == true ->
r_streams( output, Streams, Ro ),
r_read_lines( Ro, [], RoLns )
;
r_lines( Streams, output, RoLns )
),
r_record_lines( RoLns, output, CopyTo ),
r_record_lines( ReLns, error, CopyTo ),
( (Halt==true,CopyTo=stream(Cl)) -> close(Cl); true ).
r_out_halted_record( true, _Alias, [] ).
r_out_halted_record( false, _Alias, Lines ) :-
r_session_data( copy_this, Data, CopyThis ),
r_session_data( copy_to, Data, CopyTo ),
( (CopyThis==out;CopyThis==both) ->
r_record_lines( Lines, output, CopyTo )
;
true
).
r_flush_onto_1( [], _R, [] ).
r_flush_onto_1( [H|T], R, [HOn|TOns] ) :-
current_r_session( R, Streams, _ ),
r_lines( Streams, H, HOn ),
r_flush_onto_1( T, R, TOns ).
replace_variables( [] ).
replace_variables( [arp(R,Pv,Rv)|T] ) :-
r_out( R, Rv, Lines ),
r_read_list( Lines, Pv ),
% r_lines_to_pl_var( Lines, Pv ),
replace_variables( T ).
% r_lines_to_pl_var( [], [] ).
% r_lines_to_pl_var( [H|T], [] ) :-
% r_line_to_pl_var( [H|T], [] ) :-
% r_lines_to_pl_var( T, TPv ).
r_input_streams_list( Rins ) :-
( select(output,Rins,NoInpIns) -> true; NoInpIns=Rins ),
( select(error,NoInpIns,NoErrIns) -> true; NoErrIns=NoInpIns ),
( NoErrIns = [] ->
true
;
( (memberchk(input,NoErrIns);memberchk(error,NoErrIns)) ->
fail_term( 'duplicate entries in input streams list':Rins )
;
fail_term( 'superfluous entries in input streams list':Rins )
)
).
% succeds if Rcmd produces empty output, otherwise it fails
ro_empty( R, Rcmd ) :-
r_out( R, Rcmd, [] ).
r_input_normative( (A;B), R, I, This, Rplc, OutI ) :-
!,
r_input_normative( A, R, I, ThisA, RplcA, NxI ),
r_input_normative( B, R, NxI, ThisB, RplcB, OutI ),
atoms_concat( [ThisA,'; ',ThisB], This ),
append( RplcA, RplcB, Rplc ).
% r_input_normative( Obj<-List, _R, I, This, Rplc, NxI ) :-
% % atomic( Obj ),
% is_list( List ),
% !,
% Rplc = [],
% NxI is I,
% pl_list_to_r_combine( List,
r_input_normative( Obj<-Call, R, I, This, Rplc, NxI ) :-
!,
( var(Obj) ->
Rplc = [arp(R,Obj,ThisObj)],
number_codes( I, ICs ),
append( "pl_Rv_", ICs, RvCs ),
atom_codes( ThisObj, RvCs ),
NxI is I + 1
;
Rplc = [],
r_input_normative( Obj, ThisObj ),
NxI is I
),
r_input_normative( Call, ThisCall ),
atoms_concat( [ThisObj,' <- ',ThisCall], This ).
r_input_normative( PrvThis, _R, I, This, [], I ) :-
r_input_normative( PrvThis, This ).
r_input_normative( Var, This ) :-
var(Var),
!,
This = Var.
r_input_normative( Opt=Val, This ) :-
!,
r_input_normative( Opt, ThisOpt ),
r_input_normative( Val, ThisVal ),
atoms_concat( [ThisOpt,'=',ThisVal], This ).
% 2008ac06, careful! we are changing behaviour here
r_input_normative( List, This ) :-
is_list( List ),
pl_list_to_r_combine( List, This ),
!.
r_input_normative( PrvThis, This ) :-
( (\+ var(PrvThis),(PrvThis = [_|_];PrvThis=[])) ->
append( PrvThis, [0'"], ThisRight ),
atom_codes( This, [0'"|ThisRight] )
;
( compound(PrvThis) ->
PrvThis =.. [Name|Args],
( (current_op(_Pres,Asc,Name),
atom_codes(Asc,[_,0'f,_]),
Args = [Arg1,Arg2]
) ->
r_input_normative( Arg1, Arg1Nrm ),
r_input_normative( Arg2, Arg2Nrm ),
atoms_concat( [Arg1Nrm,Name,Arg2Nrm], This )
;
r_input_normative_tuple( Args, Tuple ),
atoms_concat( [Name,'(',Tuple,')'], This )
)
;
( number(PrvThis) ->
number_codes( PrvThis, ThisCs ),
atom_codes( This, ThisCs )
;
This = PrvThis
)
)
).
r_input_normative_tuple( [], '' ).
r_input_normative_tuple( [H|T], Tuple ) :-
r_input_normative_tuple( T, Psf ),
r_input_normative( H, HNorm ),
( Psf == '' -> Tuple = HNorm
; atoms_concat([HNorm,',',Psf], Tuple) ).
pl_list_to_r_combine( [H|T], This ) :-
number_atom_to_atom( H, Hatm ),
atom_concat( 'c(', Hatm, Pfx ),
pl_list_to_r_combine( T, Pfx, This ).
pl_list_to_r_combine( [], Pfx, This ) :-
atom_concat( Pfx, ')', This ).
pl_list_to_r_combine( [H|T], Pfx, This ) :-
number_atom_to_atom( H, Hatm ),
atom_concat( Pfx, ',', PfxComma ),
atom_concat( PfxComma, Hatm, Nxt ),
pl_list_to_r_combine( T, Nxt, This ).
number_atom_to_atom( NorA, Atom ) :-
number_atom_to_codes( NorA, Codes ),
atom_codes( Atom, Codes ).
number_atom_to_codes( NorA, Codes ) :-
number( NorA ),
!,
number_codes( NorA, Codes ).
number_atom_to_codes( NorA, Codes ) :-
atom( NorA ),
!,
atom_codes( NorA, Codes ).
r_read_lines( Ro, TermLine, Lines ) :-
read_line_to_codes( Ro, Line ),
r_read_lines_1( Line, TermLine, Ro, Lines ).
r_halted( Lines, R, Halted, HCall ) :-
last( Lines, "Execution halted" ),
!,
Halted = true,
findall( rs(Alias,Streams,Data), retract(r_session(Alias,Streams,Data)), Sessions),
\+ var(R),
r_halted_recovery( Sessions, R, HCall ).
r_halted( _, _R, false, true ).
r_halted_recovery( [], R, Which ) :-
( var(Which) ->
fail_term( internal_error_in_recovering_from_halt(R) )
;
true
).
r_halted_recovery( [rs(AliasH,StreamsH,DataH)|T], R, Which ) :-
( R == AliasH ->
r_session_data( at_r_halt, DataH, AtHalt ),
r_halted_recovery_action( AtHalt, AliasH, StreamsH, DataH, Which )
;
assertz(r_session(AliasH,StreamsH,DataH))
),
r_halted_recovery( T, R, Which ).
r_halted_recovery_action( restart, Alias, _Streams, Data, RecCall ) :-
Mess = 'at_r_halt(restart): restarting r_session ':Alias,
RecCall = (write( user_error, Mess ),nl( user_error )),
r_session_data( opts, Data, Opts ),
( memberchk(copy(CopyTo,_),Opts) ->
r_halted_restart_copy(CopyTo)
;
true
),
r_open_1( Opts, Alias, true ),
current_r_session( Alias, Streams, _ ),
r_lines( Streams, output, _ReLines ).
r_halted_recovery_action( reinstate, Alias, _Streams, Data, RecCall ) :-
( r_session_history(Alias,History) ->
r_session_data( opts, Data, Opts ),
r_open_1( Opts, Alias, true ),
reverse( History, Hicory ),
r_halted_recovery_rollback( Hicory, Alias )
;
fail_term( 'at_r_halt(reinstate): cannnot locate history for':Alias )
),
Mess = 'at_r_halt(reinstate): reinstating r_session ':Alias,
RecCall = (write( user_error, Mess ), nl( user_error ) ).
r_halted_recovery_action( abort, _Alias, _Streams, _Data, RecCall ) :-
Mess = 'at_r_halt(abort): R session halted by slave',
RecCall = (write( user_error, Mess ),nl( user_error ),abort).
r_halted_recovery_action( fail, Alias, _Streams, _Data, Call ) :-
retractall( r_session_history(Alias,_) ),
% % r_session_data( copy_to, Data, CopyTo ),
% write( copy_to(CopyTo) ), nl,
% ( CopyTo = stream(Stream) ->
% close(Stream)
% ;
% true
% ),
L='at_r_halt(fail): failure due to execution halted by slave on r_session',
Call = fail_term( L:Alias ).
r_halted_recovery_action( call(Call), _Alias, Streams, _Data, Call ) :-
Call = call( Call, Streams ).
r_halted_recovery_action( call_ground(Call), _Alias, _Streams, _Data, Call) :-
Call = call( Call ).
r_halted_restart_copy( CopyTo ) :-
((atomic(CopyTo),File=CopyTo);CopyTo=once(File)),
File \== user, % you never known
!,
open( File, read, Dummy ),
stream_property( Dummy, file_name(Full) ),
close( Dummy ),
( stream_property(OpenStream,file_name(Full)) ->
write( close(OpenStream) ), nl,
close( OpenStream )
;
true
).
r_halted_restart_copy( _CopyTo ).
r_halted_recovery_rollback( [], _Alias ).
r_halted_recovery_rollback( [H|T], Alias ) :-
r_in( Alias, H, _Halted ),
r_halted_recovery_rollback( T, Alias ).
r_record_history( true, _Alias, _This ).
r_record_history( false, Alias, This ) :-
r_session_history( Alias, Old ),
!,
retractall( r_session_history(Alias,_) ),
assert( r_session_history(Alias,[This|Old]) ).
r_record_history( false, _, _ ). % fold with true if assumption is correct
r_read_lines_1( eof, _TermLine, _Ro, Lines ) :- !, Lines = [].
r_read_lines_1( end_of_file, _TermLine, _Ro, Lines ) :- !, Lines = [].
r_read_lines_1( [255], _TermLine, _Ro, Lines ) :- !, Lines = [].
% yap idiosyncrasy
r_read_lines_1( TermLine, TermLine, _Ro, Lines ) :- !, Lines = [].
r_read_lines_1( Line, TermLine, Ro, [Line|Lines] ) :-
read_line_to_codes( Ro, NewLine ),
r_read_lines_1( NewLine, TermLine, Ro, Lines ).
r_boolean( Boo, Rboo ) :-
( memberchk(Boo,[t,true,'TRUE']) ->
Rboo = 'TRUE'
;
memberchk(Boo,[f,false,'FALSE']),
Rboo = 'FALSE'
).
r_read_list( [], [] ).
r_read_list( [PreH|T], List ) :-
delete_leading( PreH, 0' , H ),
( H = [0'[|Hrm] ->
break_list_on( Hrm, 0'], _, Hprv ),
delete_leading( Hprv, 0' , Hproper )
;
Hproper = H
),
r_read_list_line( Hproper, List, ConTail ),
r_read_list( T, ConTail ).
r_read_list_line( [], List, List ).
r_read_list_line( [0' |RRead], List, ConTail ) :-
!,
r_read_list_line( RRead, List, ConTail ).
r_read_list_line( [Fst|RRead], [H|List], ConTail ) :-
break_list_on( RRead, 0' , RemCs, RemNumCs ),
!,
number_codes( H, [Fst|RemCs] ),
r_read_list_line( RemNumCs, List, ConTail ).
r_read_list_line( [Fst|RemCs], [H|List], List ) :-
number_codes( H, [Fst|RemCs] ).
r_streams( [], _R, [] ).
r_streams( [H|T], R, [SH|ST] ) :-
!,
r_stream( H, R, SH ),
r_streams( T, R, ST ).
r_streams( Id, R, Stream ) :-
r_stream( Id, R, Stream ).
r_stream( H, R, SH ) :-
% current_r_session( R ),
( var(H) ->
fail_term( variable_stream_identifier )
;
true
),
( r_streams_data( H, R, SH ) ->
true
;
fail_term( invalid_r_stream:H )
).
/*
r_terminator( r(Ri,Ro,_Re), Lines ) :-
write( Ri, 'print(\"prolog_eoc\")' ),
nl( Ri ),
r_read_lines_till( Ro, "[1] \"prolog_eoc\"", Lines ).
r_read_lines_till( Ro, Terminator, Lines ) :-
fget_line( Ro, Line ),
r_read_lines_till_1( Line, Terminator, Ro, Lines ).
r_read_lines_till_1( Line, Line, _Ro, Lines ) :-
!,
Lines = [].
r_read_lines_till_1( Line, Terminator, Ro, [Line|Lines] ) :-
fget_line( Ro, NxLine ),
NxLine \== eof,
r_read_lines_till_1( NxLine, Terminator, Ro, Lines ).
*/
r_open_opt_copy( Opts, CpTerm, What, Rcv ) :-
( (memberchk(copy(Cp,CpWh),Opts),Cp \== null) ->
% heere
( ((catch(is_stream(Cp),_,fail),CpS=Cp);Cp=stream(CpS)) -> % catch = yap bug
CpTerm = stream(CpS)
;
( atomic(Cp) ->
( Rcv==true -> Mode = append; Mode = write ),
open( Cp, Mode, CpStream ),
CpTerm = stream(CpStream)
;
( Cp = once(CpFile) ->
( Rcv==true -> Mode = append; Mode = write ),
open( CpFile, Mode, CpStream ),
CpTerm = stream(CpStream)
;
( Cp = many(CpFile) ->
CpTerm = file(CpFile)
;
fail_term( 'I cannot decipher 1st argument of copy/2 option':Cp )
)
)
)
),
( memberchk(CpWh,[both,none,in,out])->
What = CpWh
;
fail_term( 'I cannot decipher 2nd arg. to copy/2 option':CpWh )
)
;
CpTerm = null, What = none
).
r_open_opt_at_r_halt( Opts, RHalt ) :-
( memberchk(at_r_halt(RHalt),Opts) ->
Poss = [restart,reinstate,fail,abort,call(_),call_ground(_)],
( memberchk(RHalt,Poss) ->
true
;
fail_term( 'Cannot decipher argument to at_r_halt option':RHalt )
)
;
RHalt = fail
).
r_bin_arguments( Opts, _Rbin, _RArgs ) :-
member( with(With), Opts ),
\+ memberchk(With, [environ,restore,save] ),
!,
fail_term( 'Cannot decipher argument to option with/1': With ).
r_bin_arguments( Opts, Rbin, Args ) :-
( current_prolog_flag(windows,true) ->
Args = ['--ess','--slave'|RArgs]
; % assuming unix here, --interactive is only supported on these
( r_bin_takes_interactive(Rbin) ->
Args = ['--interactive','--slave'|RArgs]
;
Args = ['--slave'|RArgs]
)
),
findall( W, member(with(W),Opts), Ws ),
sort( Ws, Sr ),
length( Ws, WsL ),
length( Sr, SrL ),
( WsL =:= SrL ->
r_bin_arguments_complement( [environ,restore,save], Ws, RArgs )
;
fail_term( 'Multiple identical args in with/1 option': Ws )
).
% r_opt_exec_no( [environ,restore,save], Ws, Pfx, Exec ) :-
r_opt_exec_no( [], _Ws, [] ).
r_opt_exec_no( [H|T], Ws, Exec ) :-
( memberchk(H,Ws) ->
TExec=Exec
;
atom_concat( '--no-', H, NoH ),
Exec=[NoH|TExec]
),
r_opt_exec_no( T, Ws, TExec ).
r_bin_arguments_complement( [], _Ws, [] ).
r_bin_arguments_complement( [H|T], Ws, Args ) :-
( memberchk(H,Ws) ->
% we could add explicit --with- here ?
Args = TArgs
;
atom_concat( '--no-', H, NoH ),
Args = [NoH|TArgs]
),
r_bin_arguments_complement( T, Ws, TArgs ).
r_record_lines( [], _Type, _CopyTo ) :- !.
r_record_lines( Lines, Type, CopyTo ) :-
( CopyTo == null ->
true
;
copy_stream_open( CopyTo, CopyStream ),
r_lines_print( Lines, Type, CopyStream )
).
r_record_term( CopyThis, CopyTo, This ) :-
( CopyThis == in; CopyThis == both),
CopyTo \== null,
!,
copy_stream_open( CopyTo, CopyOn ),
write( CopyOn, This ),
nl( CopyOn ),
copy_stream_close( CopyTo ).
r_record_term( _CopyThis, _CopyTo, _This ).
copy_stream_open( stream(CopyStream), CopyStream ).
copy_stream_open( file(File), CopyStream ) :-
open( File, append, CopyStream ).
copy_stream_close( Atom ) :-
atomic( Atom ),
!,
( Atom == user ->
true
;
close( Atom )
).
copy_stream_close( CopyTo ) :-
copy_stream_close_non_atomic( CopyTo ).
copy_stream_close_non_atomic( file(CopyTo) ) :- close( CopyTo ).
copy_stream_close_non_atomic( once(CopyTo) ) :- close( CopyTo ).
copy_stream_close_non_atomic( many(CopyTo) ) :- close( CopyTo ).
copy_stream_close_non_atomic( stream(_) ).
/*
write_list_to_comma_separated( [], _Sep, _Out ).
write_list_to_comma_separated( [H|T], Sep, Out ) :-
write( Out, Sep ),
write( Out, H ),
write_list_to_comma_separated( T, ',', Out ).
*/
fail_term( Term ) :-
( Term = What:Which ->
write( user_error, What ),
write( user_error, ': ' ),
write( user_error, Which )
;
write( user_error, Term )
),
nl( user_error ), fail.
r_lines( Streams, ROstream, Lines ) :-
r_streams_data( input, Streams, Ri ),
r_streams_data( ROstream, Streams, Ro ),
( ROstream == error ->
Mess = 'message("prolog_eoc")',
Trmn = "prolog_eoc"
;
Mess = 'print("prolog_eoc")',
Trmn = "[1] \"prolog_eoc\""
),
Excp = error(io_error(write, _), context(_,_)),
catch( (write(Ri,Mess),nl(Ri)), Excp, true ),
r_read_lines( Ro, Trmn, Lines ).
r_lines_print_type_stream( output, user_output ).
r_lines_print_type_stream( error, user_error ).
r_lines_print_prefix( error, Stream ) :- write( Stream, '! ' ).
r_lines_print_prefix( output, _Stream ).
r_session_skolem( Alias, I ) :-
Alias = '$rsalias'(I),
\+ r_session( Alias, _, _ ),
!.
r_session_skolem( Alias, I ) :-
NxI is I + 1,
r_session_skolem( Alias, NxI ).
r_process_was_successful( Ri, Ro, Re ) :-
Mess = 'message("prolog_eoc")',
Trmn = "prolog_eoc",
catch( (write(Ri,Mess),nl(Ri)), Excp, true ),
r_read_lines( Re, Trmn, Lines ),
r_lines_print( Lines, error, user_error ),
( (var(Excp),Lines==[]) ->
true
;
( Excp = error(io_error(write, _), context(_,_)) ->
true
;
print_message( error, Excp )
),
close( Ri ), close( Ro ), close( Re ),
fail_term( failed_to_open_session )
).
%%%%%%%%
% break_list_on( +List, +Element, ?LeftPartition, ?RightPartition ).
% Element does not appear in either the end of LeftPartition,
% or as first element of RightPartition.
% Only finds first partition so Element should be ground
% | ?- break_list_on( L, El, [a], [c,b,d,b,e] ).
% = [a,El,c,b,d,b,e] ? ; no
%
break_list_on( [X|Xs], X, [], Xs ) :-
!.
break_list_on( [X|Xs], Xa, [X|XLa], XRa ) :-
break_list_on( Xs, Xa, XLa, XRa ).
delete_leading( [], _Chop, [] ).
delete_leading( [H|T], Chop, Clean ) :-
( H == Chop ->
R = T,
Clean = TClean
;
R = [],
Clean = [H|T]
),
delete_leading( R, Chop, TClean ).
options_have_ssh( Opts, Host, Dir ) :-
( memberchk(ssh(Host),Opts) ->
Dir = '/tmp'
;
memberchk( ssh(Host,Dir), Opts )
).
locate_rbin( Ssh, RBin ) :-
locate_rbin_file( File ),
( var(Ssh) ->
(
current_prolog_flag(windows,true)
->
Exe='exe',
file_name_extension( File, Exe, RBin )
;
File = RBin
)
->
exists_file( RBin )
;
% currently when we using ssh, there is no check for existance
% of the binary on the remote host
File = RBin
),
r_verbose( using_R_bin(RBin), 1 ).
% order of clauses matters. only first existing one to succeed is considered.
locate_rbin_file( RBin ) :-
% current_predicate( r_bin/1 ),
r_bin_location( RBin ).
locate_rbin_file( RBin ) :-
environ( 'R_BIN', RBin ).
locate_rbin_file( RBin ) :-
current_prolog_flag( unix, true ),
which( 'R', RBin ).
locate_rbin_file( RBin ) :-
current_prolog_flag( windows, true ),
r_bin_wins( RBin ).
r_bin_wins( Rbin ) :-
r_expand_wins_rterm( Stem, Candidates ),
r_verbose( wins_candidates(Candidates), 3 ),
Candidates \== [],
( Candidates = [Rbin] ->
true
;
maplist( atom_concat(Stem), Tails, Candidates ),
maplist( atom_codes, Tails, TailsCs ),
cur_tail_candidates_with_pair( TailsCs, Candidates, Pairs ),
keysort( Pairs, Sorted ),
reverse( Sorted, [_-Rbin|_] )
),
!.
cur_tail_candidates_with_pair( [], [], [] ).
cur_tail_candidates_with_pair( [H|T], [F|R], [Hnum-F|TPairs] ) :-
( break_list_on( H, 0'/, Hlft, _ ) -> true; break_list_on( H, 0'\\, Hlft, _) ),
break_list_on( Hlft, 0'., MjCs, NonMjCs ),
break_list_on( NonMjCs, 0'., MnCs, FxCs ),
maplist( number_codes, Nums, [MjCs,MnCs,FxCs] ),
integers_list_to_integer( Nums, 2, 1000, 0, Hnum ),
cur_tail_candidates_with_pair( T, R, TPairs ).
integers_list_to_integer( [], _Pow, _Spc, Int, Int ).
integers_list_to_integer( [H|T], Pow, Spc, Acc, Int ) :-
Nxt is Acc + ( H * (Spc ** Pow) ),
Red is Pow - 1,
integers_list_to_integer( T, Red, Spc, Nxt, Int ).
r_bin_warning :-
write('Flag --interactive which is used when starting R sessions,'),
nl,
write( 'is not behaving as expected on your installed R binary.' ), nl,
write( 'R sessions with this binary will be started without this flag.' ),
nl,
write( 'As a result, graphic windows will suffer and the connection is' ),
write( ' more flaky.' ), nl,
write( 'If you want to overcome these limitations we strongly suggest' ),
nl,
write( 'the installation of R from sources.' ), nl, nl.
r_bin_takes_interactive( Rbin ) :-
r_bin_takes_interactive( Rbin, Bool ),
!,
Bool == true.
r_bin_takes_interactive( Rbin ) :-
Args = ['--interactive','--slave','--no-environ','--no-restore','--no-save'],
r_process( Rbin, Args, Ri, Ro, Re ),
r_streams_set( Ri, Ro, Re ),
% Streams = r(Ri,Ro,Re),
write( Ri, 'print("whatever")' ), nl( Ri ),
% r_read_lines( Re, eof, RoLns ),
% read_line_to_codes( Re, _ReLns ),
% r_lines( Streams, error, ReLns ),
% r_lines( Streams, output, RoLns ),
read_line_to_codes( Ro, RoLn ),
( append("print", _, RoLn ) ->
r_bin_warning,
Bool = false
;
Bool = true
),
assert( r_bin_takes_interactive(Rbin,Bool) ),
write( Ri, 'q()' ), nl( Ri ),
read_line_to_codes( Re, _ReLn ),
% write( Ri, 'message("whatever")' ), nl( Ri ),
close( Ri ), close( Ro ), close( Re ),
Bool == true.
% Section: Yap Specifics.
atoms_concat( Atoms, Concat ) :-
atom_concat( Atoms, Concat ).
which( Which, This ) :-
SshStrs = [null,pipe(SshWhc),null],
atom_concat( 'which ', Which, ProcWhich ),
exec( ProcWhich, SshStrs, _ ),
read_line_to_codes( SshWhc, Codes),
atom_codes( This, Codes ),
r_verbose( which(Which,This), 2 ).
r_streams_set( _Ri, _Ro, _Re ).
r_process( Exec, Args, Ri, Ro, Re ) :-
space_out_by_atom( Args, SpcArgs ),
atoms_concat( [Exec,' '|SpcArgs], Full ),
Streams = [pipe(Ri),pipe(Ro),pipe(Re)],
r_verbose( creating(Full,Streams), 3 ),
exec( Full, Streams, _ ).
r_bin_version_pl( R, Mj:Mn:Fx ) :-
Streams = [std,pipe(Ro),std],
atom_concat( R, ' --version', Rv ),
exec( Rv, Streams, _ ),
% read_line_to_codes( Ro, _ ),
read_line_to_codes( Ro, Codes ),
break_list_on( Codes, 0' , _R, Psf1 ),
break_list_on( Psf1, 0' , _V, Psf2 ),
break_list_on( Psf2, 0' , VersionCs, _ ),
break_list_on( VersionCs, 0'., MjCs, VPsf1Cs ),
break_list_on( VPsf1Cs, 0'., MnCs, FxCs ),
number_codes( Mj, MjCs ),
number_codes( Mn, MnCs ),
number_codes( Fx, FxCs ).
space_out_by_atom( [], [] ).
space_out_by_atom( [A|T], R ) :-
( T == [] ->
R = [A|M]
;
R = [A,' '|M]
),
space_out_by_atom( T, M ).
% r_bin_wins( _ ) :-
% r_expand_wins_rterm( _ ).
% fail. % I don't know how to do this in Yap...
r_expand_wins_rterm( WinsStem, Candidates ) :-
PfsR = 'C:/Program Files/R/',
% WinsStem = 'C:\\Program Files\\R\\R-', % yap 5-1-3
WinsStem = 'C:/Program Files/R/R-', % yap 6-0-3
file_exists( PfsR ), % works for dirs as well in Yap
directory_files( PfsR, Files ),
Psf = '/bin/Rterm.exe',
sieve_r_term_containers( Files, PfsR, Psf, Candidates ).
sieve_r_term_containers( [], _Pfx, _Psf, [] ).
sieve_r_term_containers( [H|T], Pfx, Psf, Cs ) :-
atom_concat( H, Psf, HPsf ),
atom_concat( Pfx, HPsf, C ),
( file_exists(C) ->
Cs = [C|TCs]
;
Cs = TCs
),
sieve_r_term_containers( T, Pfx, Psf, TCs ).
exists_file( File ) :-
file_exists( File ).
double_quote_on_yap( InArgs, Args ) :-
append( ['"'|InArgs], ['"'], Args ).
|