[Rd] Port of R header files to Delphi

Prof Brian D Ripley ripley@stats.ox.ac.uk
Wed, 21 Jun 2000 07:52:59 +0100 (BST)

On Wed, 21 Jun 2000, Duncan Murdoch wrote:

> I'm in the process of porting R header files to Delphi.  The short
> term aim is to be able to make calls into the R.dll from Delphi; the
> longer term aim is to play around with writing a Windows front-end
> with some of the features of the S-PLUS environment (e.g. scripts),
> and other features from other IDEs I've used (e.g. watch windows,
> popup values of variables selected by mouse, etc.)
> I've got a port of the simple rterm.c sample front-end sort of
> working, but I need lower level access, and I'm having trouble getting
> any further.  Is this the place to ask technical questions on this, or
> should I ask somewhere else (or just not bug everyone, since I'm not
> working on Unix or in C)?

I suspect it is the best place, as most of your questions are about
R internals.  As for the R for Windows internals, probably only Guido 
knows, but you can read the code as I have to frequently.

> For example:
> 1.  Syntax errors are causing my rterm to shut down, apparently
> because I'm not catching the exceptions that are thrown.  How are
> exceptions thrown?

I suspect that is just because you are not set up to run in interactive
mode (the internal variable R_Interactive).  The default error handler
does abort on error in non-interactive mode.  I don't know what your
`rterm' is, but rtest.c is specifically in non-interactive mode.
The real rterm.c sets R_Interactive based on isatty(0) and the --ess flag.
If you set interactive mode, you will need some way to interrupt,
and with a Windows front-end, the easiest way is to set UserBreak to
non-zero (which is equivalent to Ctrl-C on Unix or Esc in Rgui).

On my TODO list is to run more threads, so that there is a control
thread different from the one running the R interpreter.  Principally
to make interruption easier to handle, and to allow looping C code
to be interrupted.

> 2.  If I want to get R to give me a string showing how an existing
> variable would be printed, what's the normal procedure?  I tried
> calling "PrintValue", passing it the Rinternals export
> "R_LastvalueSymbol", but I just get an access violation in R.DLL.

That's a symbol, not a variable.  Section 3.6.7 of `Writing R Extensions'
may help: the key is to use findVar or one of the related functions.

> This could be because I'm not handling the export properly (DLLs
> normally export entry points, not pointers to variables, so I had to
> try to guess what indirection was being used here).  

Um. DLLs often export both.  Unfortunately, the mechanisms for *imports*
are messy, especially for exported pointers.  You need to refer to them as
_imp___blah, and that's purpose of globalvar.h.  There is no extra
indirection: apart from the name mapping they are handled just like
externs to other modules not in Dlls.

> Or I could be
> calling PrintValue at the wrong time.  Or something else.  How much
> needs to be set up in advance before PrintValue will work?

PrintValue can be called on a SEXP pointing to an R object: you may find
R_PV a little safer. (Both are in src/main/print.c.)  You will need
R_WriteConsole set up, of course.

Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch