-
Notifications
You must be signed in to change notification settings - Fork 3
/
r.pl
executable file
·119 lines (95 loc) · 2.56 KB
/
r.pl
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
:- module(r,
[ r_initialize/0,
r/1, r/2, r//1, r_source/1,
r_session/1, r_session/2, r_session//1, r_session_source/1,
r_topic/1, r_topic/2, r_topic//1
]).
:- use_module(library(rologp)).
:- use_module(session).
:- use_module(library(http/http_session)).
:- use_module(library(http/http_log)).
:- multifile r_hook/1.
:- dynamic r_initialized/0.
% Initialize R, load some code into the base environment.
r_initialize,
r_initialized
=> true.
r_initialize
=> r_source(r),
r_session_begin,
assert(r_initialized).
% Call R
r(Expr)
=> r_call(Expr).
% Evaluate R expression
r(Expr, Res)
=> r_eval(Expr, Res).
% Evaluate R expression and render it as html
r(Expr) -->
{ r(Expr, R) },
term_string(R, S),
html(S).
r_source(File)
=> format(string(String), "~w.R", [File]),
r(source(String)).
% Use R environment for session specific R commands
:- listen(http_session(begin(_Session, _Peer)), r_session_begin).
% Avoid calling twice in case of redirection (see_other)
r_session_begin,
session_data(r_session)
=> true.
r_session_begin
=> session_id(Session),
r_call('<-'(Session, 'new.env'())),
session_assert(r_session).
:- listen(http_session(end(_Session, _Peer)), r_session_end).
r_session_end
=> session_id(Session),
r_call(rm(Session)).
% Evaluate R expression in the current http_session for the current topic
r_session(Expr)
=> session_id(Session),
r(with(Session, Expr)).
r_session(Expr, Res)
=> session_id(Session),
r(with(Session, Expr), Res).
r_session(Expr) -->
{ r_session(Expr, Res) },
term_string(Res, String),
html(String).
r_topic(Expr),
b_getval(topic, Topic)
=> r_session(with(Topic, Expr)).
r_topic(Expr, Res),
b_getval(topic, Topic)
=> r_session(with(Topic, Expr), Res).
r_topic(Expr)
--> { r_topic(Expr, Res) },
term_string(Res, String),
html(String).
% Load a topic file into the current session
r_session_source(Topic),
session_data(topic(Topic))
=> true.
r_session_source(Topic)
=> format(string(S), "~w.R", [Topic]),
session_id(Session),
r(with(Session, '<-'(Topic, 'new.env'()))),
r(with(Session, with(Topic, source(S, local='TRUE')))),
session_assert(topic(Topic)).
test :-
r_init,
r_session_begin,
r('<-'(a, 1)),
r_session('<-'(a, 2)),
r(a, A),
r_session(a, S),
writeln(a=A),
writeln(session(a)=S),
r_session_source(tpaired),
r_session($(tpaired, mu), Mu1),
writeln(session($(tpaired, mu))=Mu1),
r_session_source(tpaired),
b_setval(topic, tpaired),
r_topic(mu, Mu2),
writeln(topic(tpaired, mu)=Mu2).