function [status ,msg, handle] = openR
%OPENR Connect to an R server process.
% STATUS = OPENR connects to an R server process. If there is an existing
% R server process a warning will be given. STATUS is set to true if
% the connection was successful, false otherwise.
% [STATUS, MSG] = OPENR returns any error or warning messages in the
% output MSG and does not throw warnings. Note that error messages from
% the server can by quite cryptic.
% [STATUS, MSG, HANDLE] = OPENR returns the handle of the R COM server
% The connection to R is made via the R (D)COM Server. This can be
% downloaded from http://cran.au.r-project.org/contrib/extra/dcom or
% other CRAN mirror sites. These functions were tested with version 1.2
% of the (D)COM Server. Not all R data types are supported by the (D)COM
% Server. Version 1.2 supports scalars (booleans, integers, doubles and
% strings) and arrays of these.
% status = openR;
% % Run one of the R demos to test the connection.
% % Now copy the volcano data into MATLAB
% volcano = getRdata('volcano');
% % Use SURF to plot the volcano
% axis off; view(-135,40);
% % You can also copy the colormap from R
% cols = char(evalR('terrain.colors(20)'));
% red = hex2dec(cols(:,[2 3]));
% green = hex2dec(cols(:,[4 5]));
% blue = hex2dec(cols(:,[6 7]));
% % Close the connection.
% See also: CLOSER, EVALR, GETRDATA, PUTRDATA.
% Robert Henson, May 2004
% Copyright 2004 The MathWorks, Inc.
status = false;
msg = '';
% Use a global variable to keep track of the connection handle.
% Check if a connection exists
msg = 'Already connected to an R server.';
if nargout < 2
% if not, call the StatConnector and initialize an R session
R_lInK_hANdle = actxserver('StatConnectorSrv.StatConnector');
status = true;
status = false;
R_lInK_hANdle = ;
if nargout == 0
error('Cannot connect to R.\n%s',lasterr);
msg = lasterr;
% deal with outputs
if nargout > 2
handle = R_lInK_hANdle;
if nargout ==0