Giter Site home page Giter Site logo

rstata's Introduction

RStata

A simple R -> Stata interface allowing the user to:

  • execute Stata commands (both inline and from a .do file) from R;
  • pass a data.frame to Stata;
  • return a data.frame to R;
  • include Stata 'chunks' in knitr documents (however each chunk is independent from the others; feature tested with LaTeX/Linux).

Install

install.packages('RStata')

Setup

In order to use this package you need to setup two options:

options("RStata.StataPath")
options("RStata.StataVersion")

Once done, the main function in the package is stata (see Examples below).

RStata.StataPath

RStata.StataPath contains the path to Stata executable (Windows user have to delete the extension .exe from the path ).

You can use

chooseStataBin()

to set this option the first time:

  • in Linux it searches for the 4 (cli) Stata binaries (stata-mp, stata-se, stata, stata-sm) in the search path and prompt a menu to choose one;

  • in Windows does a file.choose (and a bit of manipulation, in Windows, to obtain a valid path).

  • other system are not handled by chooseStataBin (mainly because of my ignorance regarding both the system itself and Stata installation on it, but contributions are welcome).

    Mac users are a notable group: at this stage they should set the path for the command line executable (not the GUI app). Eg for a Stata13 SE installation in El Capitain (with default options) the path is '/Applications/Stata/StataSE.app/Contents/MacOS/stata-se'. In other cases (Stata licenses, installation path and so on) the path could be slightly different (BTW, i've found the path 'exploring' the filesystem with a terminal, not the graphics explorer since it 'stops' at 'StataSE.app').

chooseStataBin set up RStata.StataPath accordingly to user advice and return the path; but you may consider to put the path returned in .Rprofile options for the next time you start R.

RStata.StataVersion

RStata.StataVersion is needed for right management of data export/import to/from Stata. Eg, for Stata 13:

options("RStata.StataVersion" = 13)

As above, you may consider to put this statement in your .Rprofile.

Other options

See ?stata.

Troubleshooting

set more off, permanently

In some setup the package seems to freeze even with the simplest example (eg stata('set obs 10')). This is likely dued to Stata more option, which has to be set to off:

set more off, permanently

avoid cd in profile.do

See issue 6.

Examples

One inline command

> stata("help regress")  #<- this won't work in Windows dued to needed
                         #   batch mode

. help regress

Title
-----

    [R] regress -- Linear regression


Syntax
------

        regress depvar [indepvars] [if] [in] [weight] [, options]

[...]

Many inline commands

You can do this (eg.):

> stata_src <- "

version 10
sysuse auto
reg mpg weight

"
> stata(stata_src)

[...]

External .do file sourceing

> stata("foo.do")

Data input to Stata

> x <- data.frame(a = rnorm(3), b = letters[1:3])
> stata("sum a", data.in = x)

. sum a

   Variable |       Obs        Mean    Std. Dev.       Min        Max
------------+--------------------------------------------------------
          a |         3   -.5603985    .3000552  -.7720861  -.2170166

Data output from Stata

Eg in order to obtain auto dataset

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

. sysuse auto
(1978 Automobile Data)
 
> head(auto)
           make price mpg rep78 headroom trunk weight length turn displacement
1   AMC Concord  4099  22     3      2.5    11   2930    186   40          121
2     AMC Pacer  4749  17     3      3.0    11   3350    173   40          258
3    AMC Spirit  3799  22    NA      3.0    12   2640    168   35          121
4 Buick Century  4816  20     3      4.5    16   3250    196   40          196
5 Buick Electra  7827  15     4      4.0    20   4080    222   43          350
6 Buick LeSabre  5788  18     3      4.0    21   3670    218   43          231
  gear_ratio  foreign
1       3.58 Domestic
2       2.53 Domestic
3       3.08 Domestic
4       2.93 Domestic
5       2.41 Domestic
6       2.73 Domestic

Data input/output

> x <- data.frame(a = rnorm(3), b = letters[1:3])
> (y <- stata("replace a = 2", data.in = x, data.out = TRUE))

. replace a = 2
(3 real changes made)

  a b
1 2 a
2 2 b
3 2 c

Contribute

You are welcome to:

rstata's People

Contributors

lbraglia avatar slackline avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rstata's Issues

When I call stata using the RStata package, some problems occur.I don't know how to solve them.

Hello ! my computer system is Windows.When I call stata using the RStata package,the probelm --"Error in seq.int(cutpoints[1] + 1, cutpoints[2] - 1) : 'from' must be a finite number " occours .

code ::

library(RStata)
options("stata_path" = "D:/Stata16/StataMP-64")
options("RStata.StataVersion" = 16)
head(iris)
suppressMessages(library(dplyr))
dat_r <- iris %>%
select(Sepal.Length,Sepal.Width,Petal.Length) %>%
rename(y=Sepal.Length,
x1=Sepal.Width,
x2=Petal.Length
)
head(dat_r)
r_string_stata_command <- 'reg y x1 x2'
stata(r_string_stata_command, data.in = dat_r,stata.path = getOption("stata_path"),
stata.version = getOption("RStata.StataVersion"))

how to solve it ? Thanks

In file(con, "r"): cannot open file 'RStata.log': No such file or directory

I've tried your instructions and run into an error

# library(devtools)
# install_github("lbraglia/RStata")

library(RStata)

# "C:\Program Files (x86)\Stata13\StataMP-64.exe"
# stata_path = "C:\\Program Files (x86)\\Stata13\\StataMP-64"

options("RStata.StataPath" = "C:\\Program Files (x86)\\Stata13\\StataMP-64.exe")
options("RStata.StataPath" = "C:\\Program Files (x86)\\Stata13\\StataMP-64")
options("RStata.StataPath" = "C:\\Program Files (x86)\\Stata13\\")

# I tried all three above. Your documentation is not clear on what the syntax should be for the path of the stata executable

options("RStata.StataVersion" = 13)
stata(c("set obs 200", "gen a = 1"))

Warning message:
In file(con, "r"): cannot open file 'RStata.log': No such file or directory

Error in file(con, "r"): cannot open the connection

How to disable the wordwrap or cut-off of the display at 41 characters?

How to disable the wordwrap or cut-off of the display at 41 characters?
It did not improve even if I made the Rstudio pane wider or increased the options(width).

library(RStata)
stata("sum a", data.in= x)
. sum a

Variable |       Obs        Mean    S

td. Dev. Min Max
-------------+---------------------------

       a |         3    .0734056    1

.387142 -1.174939 1.566709
sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Traditional)_Taiwan.950 LC_CTYPE=Chinese (Traditional)_Taiwan.950
[3] LC_MONETARY=Chinese (Traditional)_Taiwan.950 LC_NUMERIC=C
[5] LC_TIME=Chinese (Traditional)_Taiwan.950

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] RStata_1.1.1 tableone_0.9.3 knitr_1.21 sp_1.3-1 spatialEco_1.1-1 rio_0.5.16
[7] visreg_2.5-1 rms_5.1-2 SparseM_1.77 Hmisc_4.1-1 Formula_1.2-3 survival_2.43-3
[13] caret_6.0-81 lattice_0.20-38 gvlma_1.0.0.3 margins_0.3.23 dplyr_0.7.8 shiny_1.2.0
[19] ggplot2_3.1.0 bigKRLS_3.0.0 bigmemory_4.5.33 KRLS_1.0-0

loaded via a namespace (and not attached):
[1] readxl_1.2.0 backports_1.1.3 plyr_1.8.4 lazyeval_0.2.1 splines_3.5.2
[6] usethis_1.4.0 TH.data_1.0-9 digest_0.6.18 foreach_1.4.4 htmltools_0.3.6
[11] rsconnect_0.8.13 githubinstall_0.2.2 magrittr_1.5 checkmate_1.9.0 memoise_1.1.0
[16] cluster_2.0.7-1 openxlsx_4.1.0 remotes_2.0.2 recipes_0.1.4.9000 readr_1.3.1
[21] gower_0.1.2 sandwich_2.5-0 prettyunits_1.0.2 colorspace_1.3-2 haven_2.0.0
[26] xfun_0.4 callr_3.1.1 crayon_1.3.4 jsonlite_1.6 bigmemory.sri_0.1.3
[31] biglm_0.9-1 bindr_0.1.1 zoo_1.8-4 iterators_1.0.10 glue_1.3.0
[36] gtable_0.2.0 ipred_0.9-8 MatrixModels_0.4-1 pkgbuild_1.0.2 scales_1.0.0
[41] mvtnorm_1.0-8 DBI_1.0.0 biganalytics_1.1.14 Rcpp_1.0.0 xtable_1.8-3
[46] htmlTable_1.13.1 units_0.6-2 foreign_0.8-71 survey_3.35 stats4_3.5.2
[51] prediction_0.3.6.1 lava_1.6.4 prodlim_2018.04.18 htmlwidgets_1.3 httr_1.4.0
[56] RColorBrewer_1.1-2 acepack_1.4.1 pkgconfig_2.0.2 nnet_7.3-12 tidyselect_0.2.5
[61] labeling_0.3 rlang_0.3.1 reshape2_1.4.3 later_0.7.5 munsell_0.5.0
[66] cellranger_1.1.0 tools_3.5.2 cli_1.0.1 generics_0.0.2 devtools_2.0.1
[71] stringr_1.3.1 yaml_2.2.0 ModelMetrics_1.2.2 processx_3.2.1 fs_1.2.6
[76] zip_1.0.0 purrr_0.2.5 bindrcpp_0.2.2 nlme_3.1-137 mime_0.6
[81] quantreg_5.38 compiler_3.5.2 rstudioapi_0.9.0 curl_3.3 e1071_1.7-0
[86] testthat_2.0.1 tibble_2.0.1 stringi_1.2.4 ps_1.3.0 desc_1.2.0
[91] forcats_0.3.0 Matrix_1.2-15 classInt_0.3-1 pillar_1.3.1 data.table_1.11.8
[96] httpuv_1.4.5.1 R6_2.3.0 latticeExtra_0.6-28 promises_1.0.1 gridExtra_2.3
[101] sessioninfo_1.1.1 codetools_0.2-16 polspline_1.1.13 MASS_7.3-51.1 assertthat_0.2.0
[106] pkgload_1.0.2 rprojroot_1.3-2 withr_2.1.2 multcomp_1.4-8 parallel_3.5.2
[111] hms_0.4.2 labelled_2.0.1 grid_3.5.2 bigalgebra_0.8.4.1 rpart_4.1-13
[116] timeDate_3043.102 class_7.3-15 Cairo_1.5-9 sf_0.7-2 lubridate_1.7.4
[121] base64enc_0.1-3

improve parallelization

At the moment, calling stata with mclapply does not work well, for example the following command does not finish:

library(RStata)
library(parallel)
library(dplyr)

options(RStata.StataPath = "/usr/local/stata14/stata")
options(RStata.StataVersion = 14)

'%!in%' <- function(x,y)!('%in%'(x,y))


stata_test <- function(x) {

    pid <- as.character(Sys.getpid()) %>% substring(nchar(.)-1, nchar(.))
    print(paste0(pid, "--", x))
    
    stata("ds", data.in = ToothGrowth, stata.echo = F)

}
    
mclapply(seq(40), stata_test, mc.cores = 8)
Results
[1] "45--1"
[1] "46--2"
[1] "51--3"
[1] "54--4"
[1] "55--5"
[1] "56--6"
[1] "57--7"
[1] "64--8"
[1] "45--9"
[1] "46--10"
[1] "56--14"
[1] "57--15"
[1] "51--11"
[1] "54--12"
[1] "55--13"
[1] "45--17"
[1] "57--23"
[1] "56--22"
[1] "51--19"
[1] "45--25"
[1] "56--30"
[1] "57--31"
[1] "51--27"
[1] "45--33"
[1] "57--39"
[1] "56--38"
[1] "51--35"

I think this is due to stata relying on local files with unlink (maybe since then multiple processes try to read and write the same file, which results in some processes (e.g. 46 and 64) not finishing). A slightly modified version with a with a separate directory for each process works fine:

stata_test2 <- function(x) {
        
    pid <- Sys.getpid()
    cur_wd <- getwd()
    new_dir <- paste0(cur_wd, "/", pid)

    present_dirs <- list.dirs(paste0(cur_wd), recursive = F)

    if (new_dir %!in% present_dirs) {
        mkdir_cmd <- paste0("mkdir ", new_dir)
        system(mkdir_cmd)
    }

    setwd(new_dir)
    
    print(paste0(pid, "--", x))
    stata("ds", data.in = ToothGrowth, stata.echo = F)
    
    setwd(cur_wd)

}
    

mclapply(seq(40), stata_test2, mc.cores = 8)

This approach works for my purposes now, but maybe it can be integrated (more elegantly) into Rstata directly?

Batch job

If I try using the package in windows, I get error an error message: "RStata.do has been interrupted. Would you like the batch job to continue?" Is there any way around that?

Issue when running strreg command in the .do file

I am trying to run a .do file ('test.do') through and it produces and error message
"Warning in readLines(SRC[1L]) :
incomplete final line found on 'test.do'"

This .do file contains stcrreg command in Stata and the error message appears when it encounters that specific command. I was able to run other regression commands (e.g. stcox) without a problem.

Any thoughts on how to solve this?

Error in seq.int(cutpoints[1] + 1, cutpoints[2] - 1) : Stata 15.1, Windows 10

R version: 3.5.0 - (64 bit)
RStudio version: 1.1.463 - (64 bit)
Rstata package version: 1.1.1
Stata version: 15.1
OS: Windows 10

I have tried debug steps mentioned at below url:
#6

In above url, @lbraglia mentioned, problem is with below line:
cutpoints <- grep(cut_me_here, stataLog)

When I checked output of "cutpoints" in debug mode, it is 29.

Even I agree with it, since very next line in stata is:
stataLog <- stataLog[seq.int(cutpoints[1] + 1, cutpoints[2] - 1)]

Since, cutpoints[1] contains value 29, but cutpoints[2] is equal to NA, in that way above method is like:
seq.int(29+1,NA-1) = seq.int(30,NA)
Thats why error is throwing as:
Warning: Error in seq.int: 'to' must be a finite number

Below screenshot is supporting debugging steps and outcomes:
image

I hope will get quick help.

Thanks.

NA in chr vector translated to "NA" in string variable

I have a data.frame with a character vector, which I use to generate an indicator variable in Stata, like

gen y = !missing(x)

However, stata() converts my character vector x to a Stata string variable x with NAs translated to "NA" instead of "", which I expected. Even though it's stated in ?foreign::write.dta that "missing values are handled correctly", this seems not to be the case for character vectors. May I suggest switching to haven::write_dta?

Error in seq.int(cutpoints[1] + 1, cutpoints[2] - 1) : 'from' must be a finite number... when using StataMP 15 on OS X

As mentioned in the title, I get the "Error in seq.int(cutpoints[1] + 1, cutpoints[2] - 1) : 'from' must be a finite number" message . I looked up the previous issue and noticed the "set more off, permanently" solution, but didn't work.

It seems that R succeeded in calling Stata and running RStata.do in stata. I looked at the stata log file, which showed something like this...

. do RStata.do

. set more off

.
. capture noisily {
. /RSTATA: cut me here/
. display 35
15
. /RSTATA: cut me here/
. } /
end capture noisily */

. exit, clear STATA

I suspect that the error has to do with capturing stata output and reading into R.

Any suggestion would be greatly helpful. Thanks.

`seq.int()` error if Stata Code contains `exit`

R: 4.1.3 64-bit, RStata: 1.1.1, Stata: 17.0 MP6, Win10
(This issue originates from the same line as issues #6, #11, and #12 , but the reason the error occurs is different.)

If the Stata code has an exit statement, stata() will exit with an error when R tries to parse the Stata log's output.

The culprit is stata()'s lines 74-75. The second cutpoint that seq.int() uses will never be be defined because exit precludes line 48's second cut_me_comment from getting printed to Stata's Results pane. I verified this by checking stataLog's contents (included at very end, to help with readability).

This came up in a more complex use case of mine, but for a MWE:

# Assumes Stata path/Stata version set already
library(RStata)

# Base code
stataCode <-
    "
      // load something arbitrarily
      sysuse auto, clear

      // do stuff
      sum mpg

      // do other stuff
      describe

      // if the kill condition's met, stop executing and throw control back to R
      if(`killCondit'==1){
          exit
      }

      // Do a final thing
      tab foreign
    "
      
# Kill condition code - condition won't be met
localAppd <-
    "// an arbitrary local macro, to serve as the stop condition
    local killCondit = 0"

# !! - This will run fine
stata(paste0(localAppd, stataCode))

# Kill condition code - condition WILL be met
localAppd <-
    "// an arbitrary local macro, to serve as the stop condition
    local killCondit = 1"

# !! - This will throw an error
stata(paste0(localAppd, stataCode))

In my specific case, I could implement a workaround by tweaking the conditional and the code encapsulated by it:

stataCode <-
    " // load something arbitrarily
      sysuse auto, clear

      // do stuff
      sum mpg

      // do other stuff
      describe

      ** THIS IS WHAT CHANGED: NEGATE THE CONDITIONAL AND WRAP ALL THE SUBSQ
      ** CODE INSIDE THE IF()
      // if the kill condition's not met, continue executing
      if(`killCondit'!=1){

          // Do a final thing
          tab foreign
      }
    "

# !! - Will now run fine
stata(paste0(localAppd, stataCode))

If others run into the same problem, I'm unsure whether they'd be able to do the same. I suspect it'd depend on the specifics for that use case.

Returning to the root cause (exit): I wasn't sure if this was intended behavior from stata() (and that we, as users, simply needed to be more mindful) or an overlooked edge case. I didn't see anything in stata()'s help file or the GitHub readme suggesting it was the first, so wanted to bring it to your attention.

Thanks for such a useful package!


stataLog contents, after executing stata(), line 73:

Browse[2]> stataLog
 [1] ""
 [2] "  ___  ____  ____  ____  ____ ®"
 [3] " /__    /   ____/   /   ____/      17.0"
 [4] "___/   /   /___/   /   /___/       MP—Parallel Edition"
 [5] ""
 [6] " Statistics and Data Science       Copyright 1985-2021 StataCorp LLC"
 [7] "                                   StataCorp"
 [8] "                                   4905 Lakeway Drive"
 [9] "                                   College Station, Texas 77845 USA"
[10] "                                   800-STATA-PC        https://www.stata.com"
[11] "                                   979-696-4600        [email protected]"
[12] ""
[13] "Stata license: Single-user 6-core  perpetual"
[14] "Serial number: XXX"
[15] "  Licensed to: XXX"
[16] "               XXX"
[17] ""
[18] "Notes:"
[19] "      1. Stata is running in batch mode."
[20] "      2. Unicode is supported; see help unicode_advice."
[21] "      3. More than 2 billion observations are allowed; see help obs_advice."
[22] "      4. Maximum number of variables is set to 5,000; see help set_maxvar."
[23] ""
[24] ". do RStata.do "
[25] ""
[26] ". set more off"
[27] ". "
[28] ". "
[29] ". "
[30] ". capture noisily {"
[31] ". /*RSTATA: cut me here*/"
[32] ". // an arbitrary local macro, to serve as the stop condition"
[33] ".     local killCondit = 1 "
[34] ". "
[35] ". "
[36] ". "
[37] ".       // load something arbitrarily"
[38] ".       sysuse auto, clear"
[39] "(1978 automobile data)"
[40] ". "
[41] ".       // do stuff"
[42] ".       sum mpg"
[43] ""
[44] "    Variable |        Obs        Mean    Std. dev.       Min        Max"
[45] "-------------+---------------------------------------------------------"
[46] "         mpg |         74     21.2973    5.785503         12         41"
[47] ". "
[48] ".       // do other stuff"
[49] ".       describe"
[50] ""
[51] "Contains data from C:\\Program Files\\Stata17\\ado\\base/a/auto.dta"
[52] " Observations:            74                  1978 automobile data"
[53] "    Variables:            12                  13 Apr 2020 17:45"
[54] "                                              (_dta has notes)"
[55] "-------------------------------------------------------------------------------"
[56] "Variable      Storage   Display    Value"
[57] "    name         type    format    label      Variable label"
[58] "-------------------------------------------------------------------------------"
[59] "make            str18   %-18s                 Make and model"
[60] "price           int     %8.0gc                Price"
[61] "mpg             int     %8.0g                 Mileage (mpg)"
[62] "rep78           int     %8.0g                 Repair record 1978"
[63] "headroom        float   %6.1f                 Headroom (in.)"
[64] "trunk           int     %8.0g                 Trunk space (cu. ft.)"
[65] "weight          int     %8.0gc                Weight (lbs.)"
[66] "length          int     %8.0g                 Length (in.)"
[67] "turn            int     %8.0g                 Turn circle (ft.)"
[68] "displacement    int     %8.0g                 Displacement (cu. in.)"
[69] "gear_ratio      float   %6.2f                 Gear ratio"
[70] "foreign         byte    %8.0g      origin     Car origin"
[71] "-------------------------------------------------------------------------------"
[72] "Sorted by: foreign"
[73] ". "
[74] ".       // if the kill condition's met, stop executing and throw control back t"
[75] "> o R"
[76] ".       if(`killCondit'==1){"
[77] ".           exit"
[78] ""
[79] "end of do-file"

How to export datasets and results from Stata to an R object?

Is it possible to export all datasets and results(e.g. r-class, e-class) of the Stata commands to an R object such as a list? Someting like the following pseudo code:

library("RStata")
options("RStata.StataPath" = "/Applications/Stata/StataMP.app/Contents/MacOS/stata-mp")
options("RStata.StataVersion" = 14)
x <- data.frame(a = rnorm(3), b = letters[1:3])
y <- stata("sum a
            return list
           ", data.in = x, data.out = TRUE)
# below is results of `y <- stata(...)`
-------------------------------------------------------------------
. sum a

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
           a |          3    .4232076    1.731104  -.9232512   2.375893
.             return list

scalars:
                  r(N) =  3
              r(sum_w) =  3
               r(mean) =  .4232075560915901
                r(Var) =  2.996722041676543
                 r(sd) =  1.731104283882558
                r(min) =  -.9232511541259498
                r(max) =  2.375893337944167
                r(sum) =  1.26962266827477

==== what I want ====
y$data # will be the dataset
y$N # will be 3 as r(N)=3
y$sum_w # will be 3 as r(sum_w)=3
y$mean # will be .4232075560915901 as r(mean) =  .4232075560915901
.....

Another "Error in seq.int(cutpoints[1] + 1, cutpoints[2] - 1) : 'from' must be a finite number" problem

Hi, I know there have been other posts about this issue (namely here #11) but none of these solutions have been working for me. I am getting the following error:

"
Error in seq.int(cutpoints[1] + 1, cutpoints[2] - 1) :
'from' must be a finite number
"

My Stata path and version are set up as follows:

options("RStata.StataPath" = "/Applications/Stata/StataBE.app/Contents/MacOS/StataBE")
options("RStata.StataVersion" = 17)

When I try to run any Stata code, the program starts but immediately crashes, and the .do files says the following:

capture noisily {
/RSTATA: cut me here/
clear
/RSTATA: cut me here/
} /* end capture noisily */
exit, clear STATA

I've tried changing my path to say:

options("RStata.StataPath" = "/Applications/Stata/StataBE.app/Contents/MacOS/stata-be")

Since this seems to have worked for other users, but I get an error that this file or directory doesn't exist. Has anyone figured out any other solutions to this?

Thanks so much for any help you can provide!

Error in seq.int(cutpoints[1] + 1, cutpoints[2] - 1) : Stata 14.1, Windows 7

Running Rstata in R 3.3.2, from RStudio 0.99.902 against Stata 14.1 on Windows 7 I get an error. I've set up Rstata using chooseStataBin(), and set options("RStata.StataVersion" = 14).

I run:

x <- data.frame(a = rnorm(3), b = letters[1:3])
stata( "sum a", data.in = x)

I get:

Error in seq.int(cutpoints[1] + 1, cutpoints[2] - 1) : 
  'from' cannot be NA, NaN or infinite

When switching to stata.echo = FALSE I get no error.

Can't output a Stata dta file to R

Hi, I've been using RStata package and found it useful.

I would like to get the data from the stata command and found

not a Stata version 5-12 .dta file

I then tried to run

saveold auto, version(10)
but the warning still appear?

I wonder if there's something I forgot to notice. Please let me know how to correct my code.
Again, I really appreciated the RStata package and just let me thank you developers.

By the way, my system is macOS Catalina 10.15.6😊

Best wishes🎁.

Graphics of stata

it seems any code in stata to generate graphics does not work in R. any suggestion?

stata("hist ABR, discrete freq scheme(s1mono)",data.in = FES_sta)

capturing printed output from stata into a dataframe

Is it possible to capture the printed output from STATA into a dataframe?

x <- data.frame(a = rnorm(3), b = letters[1:3])

stata("sum a", data.in = x)

. sum a

Variable | Obs Mean Std. Dev. Min Max
------------+--------------------------------------------------------
a | 3 -.5603985 .3000552 -.7720861 -.2170166

For instance, the above printed results being captured into a data frame in R. Thank you.

Cryptic error, possibly due to space in Stata path?

Here's my screen after trying a minimal example:

`

#Trivial data.frame
bob=data.frame(x=c(1,2,3),y=c(3,4,5))

#Just testing if it works:
stata_command="reg y x"

#Need to give the path for the stata command:
chooseStataBin()
[1] ""

options(RStata.StataPath = "/Applications/Stata 16/StataSE.app/Contents/MacOS/stata-se")

options(RStata.StataVersion = 16)

stata(stata_command,data.in=bob)
sh: /Applications/Stata: No such file or directory
Error in seq.int(cutpoints[1] + 1, cutpoints[2] - 1) :
'from' must be a finite number
traceback()
1: stata(stata_command, data.in = bob)
`

This error doesn't give me much to work with, but I suspect given the "No such file or directory" comment that somewhere the code is screwing up because it's not expecting a space in the Stata path. (Why the idiots at Stata decided to make their default install location have a space in it I don't know.)

ERROR: dependency ‘foreign’ is not available for package ‘RStata’

Hello everyone...
Today I tried to install RStata but I had the following output when I used install.packages('RStata'):
Warning in install.packages :
dependency ‘foreign’ is not available
trying URL 'https://cran.rstudio.com/src/contrib/RStata_1.1.1.tar.gz'
Content type 'application/x-gzip' length 6460 bytes
==================================================
downloaded 6460 bytes

ERROR: dependency ‘foreign’ is not available for package ‘RStata’
* removing ‘/home/Axo/anaconda3/envs/rstudio/lib/R/library/RStata’
Warning in install.packages :
installation of package ‘RStata’ had non-zero exit status

The downloaded source packages are in
‘/tmp/Rtmpj8Bmgw/downloaded_packages’
Updating HTML index of packages in '.Library'
Making 'packages.html' ... done

Next to I typed install.packages('https://cran.rstudio.com/src/contrib/RStata_1.1.1.tar.gz')and so:
Warning in install.packages :
package ‘https://cran.rstudio.com/src/contrib/RStata_1.1.1.tar.gz’ is not available (for R version 3.6.1)

By the way, I'm using Anaconda and RStudio.

Cannot connect to Stata (Windows)

I have tried multiple variations of my Stata path. (i) I've pasted it directly from the file folder (ii) tried it with the executable name and without (iii) tried it with and without .exe., (iv) with and without administrator rights for Rstudio. I keep getting error

Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file 'RStata.log': No such file or directory

Is this a Stata 17 issue? I'm currently using Stata-SE

Can change foreign to haven?

This package uses foreign. always cause ##empty string is not valid in Stata's documented format.
haven::write_dta will not

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

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

Error in seq.int(cutpoints[1] + 1, cutpoints[2] - 1)... MacOS 10.12.4, Stata 14.2, R 3.4

I get:

Error in seq.int(cutpoints[1] + 1, cutpoints[2] - 1) : 'from' cannot be NA, NaN or infinite

when running:

stata_src <- '
log using eval
sysuse auto
reg mpg weight
'
stata(src = stata_src)

with:

options("RStata.StataPath"="/Applications/Stata\\ 14/Stata.app/Contents/MacOS/Stata")
options("RStata.StataVersion" = 14)

Stata starts normally and runs the code without error and the log-file shows the correct output. It seems the error occurs after Stata has run all of the code.

BTW: I do not have a profile.do file that I know of.

Any help is greatly appreciated!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.