lbraglia / RStata

[R package]: A R-Stata interface
112 stars 37 forks source link

Error in foreign::read.dta(dtaOutFile) with Windows Stata 14.2 #4

Closed dalewsteele closed 8 years ago

dalewsteele commented 8 years ago

I'm using RStata on a Windows 8 machine running Stata/IC 14.2. The code below fails, apparently because data is read using package{foreign} that does not recognize a Stata version 14 .dta file.

Is this an argument to use haven (which can read these files)?

> auto <- stata("sysuse auto", data.out=TRUE)
. sysuse auto
(1978 Automobile Data)
Error in foreign::read.dta(dtaOutFile, ...) : 
  not a Stata version 5-12 .dta file
dalewsteele commented 8 years ago

Note, works without error on Linux Stata 14.2

lbraglia commented 8 years ago

in Windows with

stata("sysuse auto", data.out = TRUE, stata.version = 14.2)

stata (function) put this save command at the end of the do file

saveold RStataDataOut, version(12)

which should be perfectly fine to save an R-readable (with foreign) file. Could you try (in your Windows Stata)

sysuse auto
saveold autotest, version(12)

and let me know if it's R-readable with foreign::read.dta?

dalewsteele commented 8 years ago

Yes, I can read the autotest.dta file saved in version 12 (using Windows Stata) format using foreign::read.dta.

lbraglia commented 8 years ago

What did you set up for

options("RStata.StataVersion")

?

dalewsteele commented 8 years ago

> options("RStata.StataVersion")

$RStata.StataVersion
[1] 14

`

lbraglia commented 8 years ago

could you

debug(stata)
auto <- stata("sysuse auto", data.out = TRUE)

and step until

https://github.com/lbraglia/RStata/blob/master/R/stata.R#L155

then post the content of the SRC variable?

dalewsteele commented 8 years ago

Thanks for your patience. I tried this on my Windows 7 laptop with Stata 14 and cannot reproduce the error.

Will try debug(stata) on my work desktop running Windows 8 that generates the error.

dalewsteele commented 8 years ago

Hmmm. I re-ran the code below (Windows 8, Stata 14, Microsoft R Open 3.3.1) within RStudio v1.0.19) auto <- stata("sysuse auto", data.out = TRUE) Works as expected without error!

However, when I run from Windows R conole, I still still get the error.

> auto <- stata("sysuse auto", data.out=TRUE)
. sysuse auto
(1978 Automobile Data)
Error in foreign::read.dta(dtaOutFile, ...) : 
  not a Stata version 5-12 .dta file

Will try debug(stata) ...

dalewsteele commented 8 years ago

Output from debug(stata) below (in R consolate)

Browse[2]> n
debug: SRC <- c({
    if (dataIn) sprintf("use %s", tools::file_path_sans_ext(dtaInFile)) else ""
}, "capture noisily {", cut_me_comment, SRC, cut_me_comment, 
    "} /* end capture noisily */")
Browse[2]> SRC
[1] "sysuse auto"
lbraglia commented 8 years ago

you stopped too early with debug... you should press enter until you get something like

debug:  SRC <- c(SRC, "exit, clear STATA")

then display the content of SRC

dalewsteele commented 8 years ago

OK, thanks. See below:

Browse[2]> n
debug: SRC <- c(SRC, "exit, clear STATA")
Browse[2]> SRC
[1] "set more off"                       ""                                   "capture noisily {"                 
[4] "/*RSTATA: cut me here*/"            "sysuse auto"                        "/*RSTATA: cut me here*/"           
[7] "} /* end capture noisily */"        "saveold RStataDataOut, version(12)"
lbraglia commented 8 years ago

Does this come from the R standard console? You can see that RStata does what it's intended to do; the last line is

saveold RStataDataOut, version(12)

which is the right Stata command to produce a version 12 dta.

dalewsteele commented 8 years ago

Yes, I ran debug(stata) and produced the output for the 'SRC' object in the R console. Very strange. Appreciate your help. Not sure it's worth spending time on since the error does hot happen on the same machine when I run R via RStudio.

lbraglia commented 8 years ago

Yes very strange ... ok, thanks anyway for multi-platform/interpreters testing.