[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: Stata code to run R code from within Stata and returncertain pieces of the results as Stata macros

From   Daniel Becker <>
Subject   Re: st: RE: Stata code to run R code from within Stata and returncertain pieces of the results as Stata macros
Date   Sat, 31 May 2008 09:16:22 +0200

Hallo -

Thanks for this thread. I hope someone is trying to write that smart peace of software! But isn't rsource (findit rsource) already pretty smart?

Am 31.05.2008 um 02:20 schrieb Salah Mahmud:

I think a smart solution is possible

1. Export a subset/all the data into a csv file and construct the
necessary R code to import that data into R.
My understanding of the help of rsource is that the transfer of a dataset from stata to R is already possible. So no hard work here.

2. The Stata user specifies the R library that need to be loaded-- eg
library(cmprsk) -- and the exact R code needed to perform the specific
calculations. These could be stored in an *.R file or for simple tasks
could be passed directly to Rbridge. So Rbridge could have 2 mutually
exclusive options:
i) Rcommand ("out<-crr(ftime,fstatus,cov,failcode=2) ")
ii) Rcmdfilename( "mycode.R")
IN the rsource package, the R code is stored in a file that needs to be specified:

rsource using R_source_filename [ , noloutput lsource rpath(R_pathname) roptions(R_options) ]

This seems perfectly ok to me.

3. The user also specifies the result object returned by R (eg the out
object in the out<-crr(ftime,fstatus,cov,failcode=2) that she would
like Rbridge to return as global macros or r() macros. R is
object-oriented so instead unlike Stata commands which return
individual pieces of information, R functions typically return an
object that contain all these pieces of information. So the object
"out" in the call "out<-crr(ftime,fstatus,cov,failcode=2) " above
stores all the info returned from calling the function crr. Things
like out$coef , out$loglik etc which some call class members.
Version 1 of Rbridge could return a specific class member (eg
out$loglik). A more advanced implementation will parse (within R) the
target object and extract all the members' names and values (contents)
into a text file which would include info on each member's name, data
type and value. The necessary commands are added to source.R. This may
include calls to custom R functions that facilitate the parsing and
extraction of the members of the result object. But as I said this is
not essential for version 1.
it seems like this is the tricky part. rsource can already list the output of the R-code executed in the stata results window, but that's it. It is not yet possible to have the results produced by R in a r() macro.

4. Stata runs R and pass along source.R for execution which results in
the above results text file.
Hm - isn't this already possible? If the R-code contains the necessary commands, the result of the execution of r could be that text file. Hence the ral work is to get the proper R-code that contains all results such that they can be imported back into stata.

4. Stata reads the text file and generates macros with the same name,
data type and values which Rbridge could also leave behind as r()
Once the textfile contains the information, this must be possible? Is there an example of reading a text file and putting something written there in a stata-macro?

I'm sure the devil is in the details (eg there are issues with
coordinating the running of Stata and R). For instance, Stata may have
to go to sleep until R signals that the code execution is over etc.
But the above does not seem any more daunting than the average ado out
Wouldn't the easy check be: Stata, please white until the file /file/ that/is/the/result/of/executing/R/ exists?
A variant of the pause command? How about an option waitfor(/file/that/ is/the/result/of/executing/R/results.txt) for rsource?

It is my impression that most things you want are already present in rsource and that the real hard work is to write the necessary R-code and then get the information back into stata. And for this, it very much depends on what exactly you want to do in R.


* For searches and help try:

© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index