A dual-bridge (R/Python) Stata command for importing R data files (.Rdata, .Rda, .Rds).
importr v2.0 is a major upgrade that provides a robust way to bring R datasets into Stata. It is designed to work even if R is not installed on the system, by falling back to Stata's native Python integration. It is not practically possible to import R data using "pure" Stata commands (like file read or Mata) because .Rdata and .RDS files are complex, compressed binary objects that use a custom serialization format. Parsing them from
scratch in Stata would require building a full binary decoder in Mata, which is a massive engineering undertaking. To avoid this, the "Hybrid" approach in importr first looks for R installed, if it doesn't find
R, it will attempt to use Stata's built-in Python integration (available in Stata 16 and newer).
This provides two paths to success:
- Path A (R): Uses Rscript and the haven package (best for R users).
- Path B (Python): Uses Stata's internal Python with the pyreadr library (to read the R file) and pyreadstat (to write the .dta) (best for modern Stata environments).
- Dual-Bridge Architecture:
- Primary (R): Uses
Rscriptand thehavenpackage. - Secondary (Python): Uses Stata's
pythonintegration with thepyreadrandpyreadstatlibraries.
- Primary (R): Uses
- Format Support: Works with
.Rdata,.Rda, and.Rds. - Intelligent Fallback: Automatically detects the best available tool on your machine.
Install from GitHub in Stata:
net install importr, from("https://raw.githubusercontent.com/ericabooth/importR-stata/main/") replace force
discard
which importr
help importrTo pull the example R datasets alongside the command, net get the ancillary files:
net get importr, from("https://raw.githubusercontent.com/ericabooth/importR-stata/main/")
importr using importr_example.rdata, clear
importr using importr_example.rds, clearnet get drops the two example files, importr_example.rdata and
importr_example.rds, into the current directory.
importr needs one of the two bridges below on the machine.
- R and Rscript must be in your system's PATH.
- The R package
haven. If it is missing, the bridge installs it from CRAN (install.packages("haven")) before converting, which needs an internet connection and writes to your R library.
- Stata 16 or newer.
- The Python libraries
pyreadr,pyreadstatandpandas(pyreadr will not import without pandas), installed into the Python thatpython queryreports.python pipis not a Stata command, so run pip from a terminal:When the Python bridge cannot run,/path/to/that/python3 -m pip install pyreadr pyreadstat pandas
importrprints this command with the right path.
* The command will automatically choose the best method
importr using "data/my_results.Rdata", clear
* Works with RDS files too (detected from the extension; the rds option forces it)
importr using "data/model_fit.rds", clear
importr using "data/model_fit.dat", clear rds
* What was read
display r(nobs) " obs, " r(nvars) " vars from " r(filename) " (object " r(object) ")"Options: clear replaces the data in memory (without it, changed data in
memory stop importr with r(4)); rds treats the file as an RDS file.
Stored results: r(nobs), r(nvars), r(filename), and, for .Rdata/.Rda
files, r(object), the name of the object read. A file with several objects
gives the first data frame by name (C-locale order), the same with either
bridge.
While importr is designed for Stata users, this repository also includes standalone Python scripts for converting between Stata and R formats outside of Stata.
Ensure you have the required Python libraries:
pip install pyreadstat pyreadr pandasConvert R data files (.rds, .Rdata) to Stata .dta format:
# Convert to DTA (default)
python RtoStata.py mydata.rds
# Specify Stata version (e.g., version 13)
python RtoStata.py mydata.Rdata -v 13Convert Stata .dta files into R formats (.rds or .rdata):
# Convert to RDS (default)
python pythontoR.py mydata.dta
# Convert to RData
python pythontoR.py mydata.dta --format rdata- 2.0.3 (9 Nov 2026): changed data in memory without
clearnow giver(4)instead of a misleading "needs R or pyreadr" error; the install advice prints a pip command to run in a terminal (python pipis not a Stata command); both bridges take the same object from a multi-object.Rdatafile and report it inr(object).RtoStata.pynow usespyreadr.read_r()(it calledread_rds()/read_rdata(), which pyreadr does not provide). - 2.0.2 (Aug 2026): bridge subroutines are rclass; each bridge is attempted in turn; Windows paths.
Eric A. Booth
- 🏛️ Sr Researcher, Texas 2036
- 📧 eric.a.booth@gmail.com
- 🌐 www.eric-booth.com
- 💼 GitHub Profile
This project is licensed under the MIT License.