SWI-Prolog interface to R
Nicos Angelopoulos
Abstract
This article documents the
package R, a library to talk to R system for Statistical Computing.
- author
- - Nicos Angelopoulos
- Windows-compatibility is based on work by `JAB'
- version
- 0:0:3
- See also
- -
ensure_loaded(library('
../doc/packages/examples/R/r_demo.pl
'))
- http://www.r-project.org/
- copyright
- Nicos Angelopoulos
- license
- YAP: Artistic
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.
- 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_open
-
Open a new R session. Same as 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_close
-
Close the default R session.
- r_close(+R)
-
Close the named R session.
- r_in(+Rcmd)
-
Push Rcmd to the default R session. Output and Errors will be
printed to the terminal.
- r_in(+R, +Rcmd)
-
As r_in/1 but for session R.
- r_push(+Rcmd)
-
As r_in/1 but does not consume error
or output streams.
- r_push(+R,
+Rcmd)
-
As r_push/1 but for named session.
- r_out(+Rcmd,
-Lines)
-
Push Rcmd to default R session and grab output lines Lines
as a list of code lists.
- r_out(+R,
+Rcmd, -Lines)
-
As r_out/2 but for named session R.
- 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(+R,
+Rcmd, -Lines, -ErrLines)
-
As r_err/3 but for named session R.
- r_print(+X)
-
A shortcut for r_in( print(X) ).
- r_print(+R,
+X)
-
As r_print/1 but for named session R.
- 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,
+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, +Stream)
-
As r_lines_print/3 but Lines
are written on Stream.
- r_lib(+L)
-
A shortcut for r_in( library(X) ).
- r_lib(+R, +L)
-
As r_lib/1 but for named session R.
- r_flush
-
Flush default R's output and error on to the terminal.
- r_flush(+R)
-
As r_flush/0 but for session R.
- 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(+R,
+SAliases, -Onto)
-
As r_flush_onto/2 for
specified session R.
- 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,
?S, ?D)
-
True if R is an open session with streams S and
data D (see introduction to the library).
- default_r_session(?R)
-
True if R is the default session.
- 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_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_history
-
Print on user_output the history of the default session.
- 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(?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_session_version(-Version)
-
Installed version. Version is of the form Major:Minor:Fix,
where all three are integers.
- 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_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(+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.
- C
- current_r_session/1
- current_r_session/3
- D
- default_r_session/1
- R
- r_bin/1
- r_bin_version/1
- r_bin_version/2
- r_close/0
- r_close/1
- r_err/3
- r_err/4
- r_flush/0
- r_flush/1
- r_flush_onto/2
- r_flush_onto/3
- r_history/0
- r_history/1
- r_history/2
- r_in/1
- r_in/2
- r_lib/1
- r_lib/2
- r_lines_print/1
- r_lines_print/2
- r_lines_print/3
- r_open/0
- r_open/1
- r_out/2
- r_out/3
- r_print/1
- r_print/2
- r_push/1
- r_push/2
- r_session_data/3
- r_session_version/1
- r_streams_data/3
- r_verbosity/1
-