-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathr_session.pl
78 lines (62 loc) · 1.86 KB
/
r_session.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
:- module(r_session,
[ r_init_session/0, r//1, r_session/1, r_session/2, r_session//1, r_session_source/1,
r_topic/1, r_topic/2, r_topic//1
]).
:- reexport(library(r)).
:- use_module(session).
:- use_module(library(http/http_session)).
:- use_module(library(http/http_log)).
% Initialize R, load some code into the base environment.
r_init_session :-
r_initialize,
r_session_begin.
% Evaluate R expression and render it as html
r(Expr) -->
{ r(Expr, R) },
term_string(R, S),
html(S).
% 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('<-'(Session, 'new.env'())),
session_assert(r_session).
:- listen(http_session(end(_Session, _Peer)), r_session_end).
r_session_end
=> session_id(Session),
r(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)).