Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to automatically close Windows terminal shell after being called from Stata

    I have written a programme in Stata that calls R to run an R script (SLS.R). This works fine on a Mac operating system but when I try this in a Windows operating system the user has to manually click to close the shell. Is there a way to automatically close the shell window instead of having to manually click?


    Code:
        if "`c(os)'" == "MacOSX" {
        shell "/usr/local/bin/r" CMD BATCH SLS.R
        }
        else{
        // Write batch file to find R.exe path and R version
        set more off
        qui: file close _all
        qui: file open bat using setup.bat, write replace
        qui: file write bat ///
        `"@echo off"' _newline ///
        `"SET PATHROOT=C:\Program Files\R\"' _newline ///
        `"echo Locating path of R..."' _newline ///
        `"echo."' _newline ///
        `"if not exist "%PATHROOT%" goto:NO_R"' _newline ///
        `"for /f "delims=" %%r in (' dir /b "%PATHROOT%R*" ') do ("' _newline ///
                `"echo Found %%r"' _newline ///
                `"echo shell "%PATHROOT%%%r\bin\x64\R.exe" CMD BATCH SLS.R > runr.do"' _newline ///
                `"echo All set!"' _newline ///
                `"goto:DONE"' _newline ///
        `")"' _newline ///
        `":NO_R"' _newline ///
        `"echo R is not installed in your system."' _newline ///
        `"echo."' _newline ///
        `"echo Download it from https://cran.r-project.org/bin/windows/base/"' _newline ///
        `"echo Install it and re-run this script"' _newline ///
        `":DONE"' _newline ///
        `"echo."' _newline ///
        `"pause"'
        qui: file close bat
        //Run batch
        shell setup.bat
        //Run R
        do runr.do
        }
    Any help is much appreciated!

  • #2
    Try

    Code:
    ! setup.bat
    There is documentation for this at https://www.stata.com/manuals13/dshell.pdf

    Comment


    • #3
      Hi Daniel,

      Unfortunately, that doesn't seem to work. It still creates a shell that requires one to manually close it.

      The shell window seems to open during this part of the code

      Code:
       
       `"for /f "delims=" %%r in (' dir /b "%PATHROOT%R*" ') do ("' _newline ///             `"echo Found %%r"' _newline ///             `"echo shell "%PATHROOT%%%r\bin\x64\R.exe" CMD BATCH SLS.R > runr.do"' _newline ///             `"echo All set!"' _newline ///             `"goto:DONE"' _newline ///
      I have changed the "shell" to "!", like so

      Code:
       
       `"for /f "delims=" %%r in (' dir /b "%PATHROOT%R*" ') do ("' _newline ///             `"echo Found %%r"' _newline ///             `"echo ! "%PATHROOT%%%r\bin\x64\R.exe" CMD BATCH SLS.R > runr.do"' _newline ///             `"echo All set!"' _newline ///             `"goto:DONE"' _newline ///
      However, I still get the same issue. Do you have any other ideas perhaps?

      Comment


      • #4
        The line:

        Code:
          
         `"echo ! "%PATHROOT%%%r\bin\x64\R.exe" CMD BATCH SLS.R > runr.do"' _newline ///
        is using shell to write this Stata code into a do file called "runr.do". But the shell does not automatically close once this is done.

        Comment


        • #5
          Suggestions:

          1) set trace on, then examine the generated code one line at a time.
          2) What are the contents of runr.do? Does it look legit?
          3) What happens if you unroll the for loop?

          Comment


          • #6
            Hi Daniel, thanks for your feedback. I have tried the above. Essentially I am trying to tell Stata where the R folder (the R.exe file) is within a Windows computer. However, this can vary for each user. Do you know if there is a way to tell Stata where the R.exe file is without having to open shell?

            Comment


            • #7
              I found the
              Code:
              rscript
              command on GitHub that looks for whether R is installed and executes the r script.

              https://github.com/reifjulian/rscript

              Comment

              Working...
              X