haghish / rcall

Seamless interactive R in Stata. rcall allows communicating data sets, matrices, variables, and scalars between Stata and R conveniently
http://www.haghish.com/packages/Rcall.php
91 stars 29 forks source link
interfacing language-interface rstats stata stata-package

Cite: Haghish, E. F. (2019). Seamless interactive language interfacing between R and Stata. The Stata Journal, 19(1), 61-82.
NOTE: The R: command is a synonym of rcall: and can be used exchangably.

GitHub release (latest by date)


rcall : Seamless interactive language interfacing between R and Stata

rcall runs R commands in Stata, allowing the two software automatically communicate variables, matrices, and data. This is done based on a new interfacing paradigm that attempts to synchronize data between two distinct computer languages, particularly when coding is carried out interactively. For more information read the help file or visit rcall homepage. rcall simulates the R language inside Stata and returns rclass scalars, macros, and matrices from R to Stata, anytime an R command is executed. Similarly, it allows passing macro, matrix, scalar, and data frame from Stata to R. As a result, Stata users can open an R window in the middle of a data analysis, execute the R commands, and continue the rest of the analysis with Stata, given the results of the R analysis already made available to Stata in the form of scalar, macro, matrix, or data frame.

rcall makes it so easy to run R within Stata interactively, pass data or a matrix to R, and access the results (numeric, matrix, character, lists) automatically within Stata which simply brings the power of R as well as all other programming languages that can be used interactively in R (e.g. C++ using Rcpp or JavaScript using V8) in Stata.

The rcall package is much more than calling R within Stata! It implements a veriety of procedures for quality check and making sure that the R code can proprly gets executed within Stata and it also provides functions to allow Stata programmers evaluate the satisfactory versions of R and R packages in their programs. Moreover, it automatically returns the results of the analysis from R into Stata, in an accessible formats such as matrices, scalars, data sets, etc. Here is a quick and dirty diagram, showing the huge potential of rcall to enhance your Stata:

Summary of the `rcall` modes of data communication

1. Installation

The github package is the only recommended way for installing rcall. Once github is installed, you can install either the development version or the stable version of the package.

if you like to help testing the newst development, install the development vesion:

github install haghish/rcall

Otherwise, install the latest stable release (recommended for general users)

github install haghish/rcall, stable

2. rcall modes of data communication

Language interfacing is done for a veriety of purposes. For example, you may call R within Stata to:

Both require data communication back and forth between Stata and R and this is where rcall stands out. rcall excells in a few modes to facilitates data communication based on your needs. These modes are summarized in the graph below:

Summary of the `rcall` modes of data communication

In general, it offers 2 modes of data communication which are

  1. interactive, often used for data analysis when the user might call R frequently and interactively. This general mode includes a memory that preserves the history of your R session. This mode itself can be used in two ways:

    1. interactive, where the R session can be continued command by command. this mode allows you call R interactively within Stata do-files
    2. console, where Stata console is converted to R console. this mode allows you to work with R interactively within Stata, but cannot be called from do-files. However, it provides a much more fun experience, namely to have R console within Stata console!
  2. non-interactive, which is the recommended mode for integrating R into Stata programs. This mode does not have a memory and every call will begin a fresh R session

3. Examples

rcall is very powerful, yeat very easy to work with. In the following examples, I simply use R to define a scalar or print a text, just to show you how easy it is to use rcall and to get data back into Stata. But to stand by the traditions, I start with a Hello World example with interactive mode

interactive mode: calling R to print a text

We can also open the R console within Stata to have fun with R without exiting Stata! You notice that working with R interactively in the console mode is even easier than working with MATA within Stata because rcall automatically returns the data from R to Stata.

interactive console mode: defining object a = 99 in R and getting it back in Stata

Type return list to see what objects have ben transfered from R to Stata. This is one of the biggest advantages of rcall, namely, the objects you define in R can be automatically accessed within Stata! Of course, you can control what objects to return (especially if you are programming a Stata ado-program that embeds R). Read the manuscript published by Stata Journal for details.

4. Development

Requirements: The -GitHub- package is required for testing and the -markdoc- is required for building the documentation.

Building: To update R.ado from a modified rcall.ado, run make_R_abb.do. To build the installation files and documentation, run make.do.

Resources

Examples

Release notes

Need help? Ask your questions on statalist.org

Author

E. F. Haghish
Department of Psychology,
University of Oslo, Norway
haghish@uio.no
http://www.haghish.com/packages/Rcall.php
@Haghish