From simon.urbanek at r-project.org  Tue Nov  8 23:39:42 2011
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Tue, 8 Nov 2011 17:39:42 -0500
Subject: [Rd] Question on parsing R code from C
In-Reply-To: <loom.20111108T224700-655@post.gmane.org>
References: <loom.20111108T203022-872@post.gmane.org>
	<9465BE63-EA88-4BE2-8D1C-9E33BE8AA45D@r-project.org>
	<loom.20111108T224700-655@post.gmane.org>
Message-ID: <3D4F776D-4D5D-4C39-90B3-12DF09C5C6BA@r-project.org>


On Nov 8, 2011, at 4:57 PM, KR wrote:

> Simon Urbanek <simon.urbanek <at> r-project.org> writes:
>> I'm not sure I understand this really - it doesn't define SEXP. I would expect 
> something like
>> 
>> typedef void *SEXP;
>> 
>> Which may work for declarations. Note, however, that for the actual code 
> you'll need to include the headers anyway.
> 
> Thanks for the prompt reply!
> 
> It's my understanding that using opaque pointers to hide implementation is fine 


Except that you don't know what are macros, inlined functions and actual functions. If you are careful you can possibly fall back to external functions but, obviously, your code will be less efficient. I would still prefer including Rinternals.h - you must have a *really* good reason to not do so ;).


> (as long as they are not de-referenced nor the size of the actual struct is 
> needed). I actually come out with the tentative typedef by looking at the 
> sources, but I got confused by some conditional pre-processor directives so I 
> was searching for confirmation.
> 
> For the actual code I can just declare the types (even opaques depending on the 
> use) and function signatures that I strictly need by hand as long as I am doing 
> it correctly (after all that's what is inside the headers, among with the stuff 
> I don't need).
> 
>> http://r.research.att.com/man/R-exts.html is the documentation and the headers 
> (Rinternals.h
>> mainly) is the canonical reference.
> 
> Understood, thanks.
> 
>>> 2. If the parsed R code of the working implementation contain commands like 
>>> plot(), will the opportune windows pop-up as if I were using the R shell (on 
>>> both windows and unix systems) ?
>>> 
>> 
>> It depends on your settings - plot() uses whatever graphics device you tell it 
> to use, so plot() doesn't
>> decide about that. If you use the proper interactive graphics device *and* 
> make sure you're serving the
>> REPL then it may work. Beware of interactions with your other parts, though, 
> since you are responsible for
>> ensuring that it works. 
> 
> I admit I am quite lost here, is there any code I can look at for examples? :P
> 

The embedding code examples and existing GUIs.

It's not a trivial topic and you should be really familiar with R in order to venture there. Embedding R to perform computations is reasonably easy, but the moment you want to run it interactively like console R, you have to implement a bunch of callbacks and run the even loop (REPL). It is more tricky than it sounds, because you need to synchronize your app with the event loop, make sure you process system events etc. (Simple command line R is not that hard - there are examples in the R sources with stub implementations of the callbacks, but the complexity increases considerably the moment you start using any UI features in your app)



>> BTW: you subject has absolutely nothing to do with your question - your'e 
> asking about embedding R ;)
> 
> Hmmmm I may be excused because I copy and pasted the title of section 5.12 
> (which seemed most relevant for what I wanted to achieve) :-)
> 

;)

Cheers,
Simon



[Rd] Question on parsing R code from C
Simon Urbanek