Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

seq.int() error if Stata Code contains exit #27

Open
MetzgerSK opened this issue Jun 2, 2022 · 0 comments
Open

seq.int() error if Stata Code contains exit #27

MetzgerSK opened this issue Jun 2, 2022 · 0 comments

Comments

@MetzgerSK
Copy link

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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant