From lachmann at eva.mpg.de  Wed Sep  8 00:27:24 2010
From: lachmann at eva.mpg.de (ghostwheel)
Date: Tue, 7 Sep 2010 15:27:24 -0700 (PDT)
Subject: [Rd] Is an R sub-session somehow possible?
In-Reply-To: <DB68F427-6A41-48B5-9C88-6B6B7DB72A2E@r-project.org>
References: <1283882802031-2530174.post@n4.nabble.com>
	<8E175E90-C40F-4D62-857E-F8C4231862B5@r-project.org>
	<1283885014288-2530233.post@n4.nabble.com>
	<DB68F427-6A41-48B5-9C88-6B6B7DB72A2E@r-project.org>
Message-ID: <1283898444715-2530527.post@n4.nabble.com>



Simon Urbanek wrote:
> 
> 
> 
> It doesn't really matter where the R is as long as you have some way of
> getting at the results. You are still leaving us in the dark as of what
> exactly you do (technically) so there is not much detail we can provide...
> 
> 

Sorry, I'll try to provide more detail:

I am trying to provide a good connection between the TeXmacs editor and R.
(A really nice overview of such connections can be found in
http://www.r-project.org/conferences/useR-2004/abstracts/supplements/Urbanek.pdf
;)
What I do is fake a terminal, and interact with the command line interface
to R (something like what the program 'expect' does). To detect that R is
waiting for input I have to check various properties of the terminal. Not
really pretty.
What happens is that:
1. TeXmacs sends me a string to execute. 
2. I pass it to R, and wait for a prompt.
3. I pass all of R's return, somewhat processed back to TeXmacs. Thus, when 
I want to put a graphics inside TeXmacs' buffer, I have to print something
like 'start postscipt graphics', then the postscript file, then 'end
postscript'.

When the user asks for tab completion, TeXmacs sends me a special message
saying that this string needs to be completed. I call a function in R, and
return the result.

My program is able to interact with a remote R session, i.e. the session
will start on a different machine than the one TeXmacs runs on. Luckily, it
is also possible to detect when R waits for a prompt in a remote session by
doing these ugly terminal hacks.

In some cases the R prompt won't be a prompt for another command, but
instead, R is just waiting for user input, or for completion of the previous
input.

Now, my remaining question is this:
When I run R remotely, by emulating a terminal, and interacting with R in
command line mode, AND
when at the same time R is not waiting for a command, but instead for some
other kind of user interaction, my interface can't send to R a request to
execute the code to complete a certain string.
(I.e. call the function t.tab.complete that I attached below).

If I understand correctly, one solution is to write a replacement input
loop, so that R will call my ReadConsole() from time to time, and while I'm
inside that function, I could call R functions to provide tab completion.

In that case, for remote sessions, I would have to compile on the remote
computer this alternative R input loop, and I would have to have one for
every version of R that a user might want to run on the remote machine (the
machine I am currently working with remotely has 11 versions of R
installed...). I was wondering if there is still a way to avoid that, and
interact with the regular input loop, or maybe it is possible to load an
alternative input loop from a package/library inside an already running
regular R interface?
I would like the user to have to do as little as possible in terms of
installing additional programs on a remote machine.

Thanks,
I'm not sure if this was the type of additional information you meant...
Michael

Here is my tab completion routine, ("\2" and "\5" are the codes for TeXmacs
for a start and a stop of blocks. (basically like brackets) ).
-----
t.tab.comp<-function(s,curs)
  {
    rc.settings(help=F) #This is because of a bug in matchAvailableTopics in
package utils
    rc.settings(file=T)

    utils:::.assignLinebuffer(substr(s,1,curs))
    utils:::.assignEnd(nchar(s))
    utils:::.guessTokenFromLine()
    utils:::.completeToken()
    l=utils:::.retrieveCompletions()
    l=sapply(l,function(x) {
      substr(x, nchar(utils:::.CompletionEnv[["token"]])+1,nchar(x) )
    } )
    i=grep("=$",l)
    if( (length(l[-i]) > 10) & (length(i)>0 ) )
    l=c(l[i])
    s3=utils:::.CompletionEnv[["token"]]
    s3=gsub("\"","\\\\\"",s3)
    deb.l <<- l
    
    cat("\2scheme:(tuple \"",s3,"\"",sep="") ;
    cat(" \"")
    if( length(l) > 0 )
    {
      cat(l,sep="\" \"")
    }
    else
    {
      #cat(s)
      cat("\"\"")
    }
    cat("\"")
    cat(")\5")
    
  ---- 

-- 
View this message in context: http://r.789695.n4.nabble.com/Is-an-R-sub-session-somehow-possible-tp2530174p2530527.html
Sent from the R devel mailing list archive at Nabble.com.



[Rd] Is an R sub-session somehow possible?
ghostwheel