Announcement

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

  • Running R script (through terminal, via shell) from Stata

    I want to run an R script through Stata. I can successfully *open* the script in Rstudio, via Stata, this way:
    shell open -a RStudio "PATHWAY/filename.R"
    And in terminal, I can successfully *run* the script through the command:
    Rscript "PATHWAY/filename.R"
    However, shell Rscript "PATHWAY/filename.R" returns the error /bin/bash: Rscript: command not found.

    I see that others execute R scripts via something like:
    shell "C:\Program Files\R\R-2.15.1\bin\x64\R.exe" CMD BATCH test.R
    However, I am unsure about the equivalent R command pathway on a Mac. (Or why I need a pathway at all, since I can execute the file in terminal w/ the mere command Rscript, and can open the file w/ shell in Stata, without a pathway.) Following notes here, I find that file.path(R.home("bin"), "R") returns "/Library/Frameworks/R.framework/Resources/bin/R", but adding this path way to the command (shell /Library/Frameworks/R.framework/Resources/bin/R/R.exe "PATHWAY/filename.R") doesn't help.

    Additionally, I am aware of the Rsource package, but I think this is more capability than I need. I don't want to run the R code within Stata, or import R objects into Stata after running. I merely want Stata to tell terminal "Run this particular R script," and then wait for the run to complete before continuing on with the Stata code. So I *think* shell is the right avenue, if I can get it to work.

    Would appreciate mac-specific advice! Thanks so much. Cross-posted on Stack Overflow.
    Last edited by Leah Bevis; 25 Oct 2017, 07:34.

  • #2


    Dear Leah,

    I have experience in calling R from Stata (but I am a Linux user).

    Differences in the syntax between Linux and a mac OS should be minimal, however.

    Here are my thoughts:

    1. Forget Rstudio for those tasks.
    2. Vanilla is the way to go.
    3. I assume that you have data in Stata and will use R to process part of them, sending them back to Stata.

    Assuming that the executable in the mac OS is at /Library/Frameworks/R.framework/Resources/bin/R

    The command you need would be more or less like:

    shell /Library/Frameworks/R.framework/Resources/bin/R --vanilla <PATHWAY/filename.R

    Pay attention to the < signal, otherwise your argument will be ignored by Stata.

    Let me know if it works for you.

    All the best,

    Tiago








    Comment


    • #3
      This Statalist thread may be relevant: Programs for using Stata with R.

      Comment


      • #4
        Tiago, that works! Thank you SO much, I really appreciate it. Can you possibly explain the vanilla command --- how did you know this syntax, and where does it come from? I notice that terminal doesn't recognize the command. So it's a phrase for Stata? I guess I misunderstood the shell command; I thought it basically ran commands through terminal, but this appears not to be the case. And the help for shell is so sparse... I'm not sure where to look to learn this (potentially super-useful) capability properly.

        Comment


        • #5
          Here are discussions of --vanilla

          https://wenjie-stat.me/2017/01/vanilla/

          https://stackoverflow.com/questions/...ession-vanilla
          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          StataNow Version: 19.5 MP (2 processor)

          EMAIL: [email protected]
          WWW: https://www3.nd.edu/~rwilliam

          Comment


          • #6
            Good references, Richard. As usual, thanks for your important imputs.

            --vanilla is an option to use R non-interactively. In a nutshell, it says: Please, R, do your job, but be as light as possible. That is, do not restore previously saved objects, do not save, use as little memory and CPU as possible to do your job.


            R --help

            Usage: R [options] [< infile] [> outfile]
            or: R CMD command [arguments]

            Start R, a system for statistical computation and graphics, with the
            specified options, or invoke an R tool via the 'R CMD' interface.

            Options:
            -h, --help Print short help message and exit
            --version Print version info and exit
            --encoding=ENC Specify encoding to be used for stdin
            --encoding ENC
            RHOME Print path to R home directory and exit
            --save Do save workspace at the end of the session
            --no-save Don't save it
            --no-environ Don't read the site and user environment files
            --no-site-file Don't read the site-wide Rprofile
            --no-init-file Don't read the user R profile
            --restore Do restore previously saved objects at startup
            --no-restore-data Don't restore previously saved objects
            --no-restore-history Don't restore the R history file
            --no-restore Don't restore anything
            --vanilla Combine --no-save, --no-restore, --no-site-file,
            --no-init-file and --no-environ


            Comment


            • #7
              Thank you so much, Richard and Tiago! This is incredibly helpful, and I'm not sure I would have ever found this information w/ my own forum searches. I'm very much obliged.

              Comment

              Working...
              X