open files saved with R's `saveRDS` (serialization interface) with stata
r, stata
Solution
Having to read variables stored in R's binary format from Stata is an indication that your data management strategy is wonky. If you want to read data with multiple formats, then you should use a non-platform-specific format. That usually means a text-based format (CSV or tab-delimited or XML or JSON), or a database, or a well-documented standardised binary format (HDF5).
To fix your situation, you need a `resave` function that reads in an RDS file and outputs it in a new format. Something like
resave <- function(infile)
{
x <- readRDS(infile)
outfile <- sub("\\.rds$", "\\.csv", infile)
write.csv(x, outfile)
}
Another possible strategy is to convert the file with `foreign::write.dta` on an as-needed basis. This is described on the StatLore blog. It may be a better strategy if you only occasionally use Stata.
The idea is that you have a batch file of R code that resaves the file, then you use Stata's `shell` command to call R in batch mode.
shell "<path to R>\R.exe" CMD BATCH resaveAsDta.R
Then you can `use` the DTA file as normal.
Problem
generally I prefer to save my R object using `saveRDS` so that I can later reassign them to a particular variable using `df= readRDS(...)`. Sometimes I want to do something in stata though. So is there a way to open the files saved with `saveRDS` in stata?