Announcement

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

  • how to use Stata in Emacs (with ess)

    I'm trying to run Stata do-files in the Emacs editor, and I have already installed the ESS package (actually I installed a modified Emacs with ess built in, as recommended for Windows users in the ess installation manual. the link is here).

    Now when I open up an do-file, the syntax is properly highlighted, the mode is show as (ESS [sta]), but I just have no clue how to run the do-file (the chosen line or the whole do-file), and how to see the results.

    I have been googling for quite a while and haven't found any thing about it yet, and the ESS package manual has turned out to be the most esoteric user manual I have ever read.

    Can anyone offer some lights on how to use this package? I'm having difficulties to understand how the ess package is linked to the Stata software installed on my computer, and how to run Stata inside Emacs. Anything related would be appreciated, since the latest info I can find on this topic dates back to 2010...

  • #2
    Typically you start a Stata session by doing M-x stata RET
    This runs console-mode Stata in an Emacs buffer.
    You can then execute lines or blocks of code by highlighting them and doing C-c C-r

    However, by default ESS does this in interactive mode (i.e., as if copying and pasting to the command window) so if you have batch-style syntax (comments, alternative line #delimit-ers etc) it's not going to work properly. If you're interested I have an alternative emacs-lisp function that behaves much more like the Stata Do-File Editor (writes to a temporary file, does "do $tempfile" in Stata, deletes temporary file). I append it for reference (below), don't worry if it doesn't make sense.

    See also http://teaching.sociology.ul.ie/bhal...rdpress/?p=135 for some comments on using console mode.

    Code:
    (defun delimit-do (start end toggle &optional clear message)
      "Send the current region to the inferior ESS process, Stata do-editor style.
    Creates a temporary file, \"do\"-es it, deletes it.
    With prefix argument toggle the meaning of `ess-eval-visibly-p';
    this does not apply when using the S-plus GUI, see `ess-eval-region-ddeclient'."
      (interactive "r\nP")
      ;;(untabify (point-min) (point-max))
      ;;(untabify start end); do we really need to save-excursion?
      (ess-force-buffer-current "Process to use: ")
      (message "Starting evaluation...")
      (setq message (or message "Eval region"))
    
      (save-excursion
        ;; don't send new lines (avoid screwing the debugger)
        (goto-char start)
        (skip-chars-forward "\n\t ")
        (setq start (point))
    
        (unless mark-active
          (ess-blink-region start end))
    
        ;; don't send new lines at the end (avoid screwing the debugger)
        (goto-char end)
        (skip-chars-backward "\n\t ")
        (setq end (point)))
    
      (let* (delimit
             (commands (buffer-substring-no-properties start end))
             (delimit-do-file (make-temp-file "delimit-do" nil ".do"))
             (proc (get-process ess-local-process-name))
             (visibly (if toggle (not ess-eval-visibly-p) ess-eval-visibly-p))
             (dev-p (process-get proc 'developer))
             (tb-p  (process-get proc 'tracebug)))
        ;; Go to the start of the section and look back for #delimit
        ;; if found set delimit unless the delimiter is not ";"
        (save-excursion
          (goto-char start)
          (setq delimit (re-search-backward "^#delimit +\\(.+\\)$" nil t))
          (if delimit
              (if (not (string-match ";" (match-string 1))) (setq delimit nil))))
    
        (with-temp-buffer
          (if clear (insert "clear\n"))
          (if delimit (insert "#delimit ;\n"
                              commands
                              "\n#delimit cr\n")
            (insert commands "\n"))
          (write-file delimit-do-file nil)
          (kill-buffer (current-buffer)))
    
        (process-send-string
         (get-ess-process ess-current-process-name)
         (format "do %s\nrm %s\n" delimit-do-file delimit-do-file))
        )
      (if (and (fboundp 'deactivate-mark) ess-eval-deactivate-mark)
          (deactivate-mark))
      ;; return value
      (list start end))

    Comment


    • #3
      Hi Brendan thanks for the insights, my problem is that my proceedings broke at the very first step. When I highlight some some lines in the do-file and C-c C-r, Emacs told me that "Searhing for program: no such file or directory, stata". so I guess Emacs is having some difficulty finding where my Stata is installed.

      So, trying to connect Emacs to my Stata program, I set the value of the inferior-STA-program-name variable to StataSE(which used to be stata), and I added my Stata program folder (D:/Programs/Stata) to the system path.

      Now when I highlight some lines and C-x C-r, Emacs, like always, asks me "Starting data directory?" after I give it my directory, instead of running the code in Emacs, it starts my Stata (in its own window) and does nothing, and Emacs freezes and I have to force close the window.

      Where did I go wrong?

      Comment


      • #4
        You'll get better help from the ESS mailing list for questions about general setup. See http://ess.r-project.org/index.php?S...getting%20help

        Edit: I see you're already there!
        Last edited by Brendan Halpin; 09 Sep 2015, 06:27.

        Comment


        • #5
          yes I am I will take a look at your code later, right now I can't understand a thing about it.. Thanks!

          Comment


          • #6
            Hi Olivier

            Just add the foollowing lines to your ~/,emacs file (replace /usr/local/stata with your stata path)

            (setenv "PATH" (concat (getenv "PATH") ":/usr/local/stata"))
            (setq exec-path (append exec-path '("/usr/local/stata")))

            After that you can execute do files line by line using C-c RET





            Comment


            • #7
              Originally posted by Olivier Ma View Post
              I'm trying to run Stata do-files in the Emacs editor, and I have already installed the ESS package (actually I installed a modified Emacs with ess built in, as recommended for Windows users in the ess installation manual. the link is here).
              From what I remember, ESS was meant to be a replacement for running Stata in console mode.

              If you're interested in using the Stata GUI together with Emacs, you might want to try using the Emacs ado-mode. The directions for downloading and installing can be found here:

              http://louabill.org/Stata


              If you need help with the installation, let me know.

              Comment


              • #8
                Hi Bill, I am actively using Ado-mode in Emacs. Btw, thank you for writing it, it's great. I have an issue though. Sometimes I am running different instances of Stata in Windows. When I send a job from Emacs to Stata (with M-x), Ado-mode is not working because it does not know which instance to send the job to. Would it be possible to let Ado-Mode choose which of the open instances of Stata to send the job to? Thanks a lot.

                Comment


                • #9
                  Originally posted by Brendan Halpin View Post
                  Typically you start a Stata session by doing M-x stata RET
                  This runs console-mode Stata in an Emacs buffer.
                  You can then execute lines or blocks of code by highlighting them and doing C-c C-r

                  However, by default ESS does this in interactive mode (i.e., as if copying and pasting to the command window) so if you have batch-style syntax (comments, alternative line #delimit-ers etc) it's not going to work properly. If you're interested I have an alternative emacs-lisp function that behaves much more like the Stata Do-File Editor (writes to a temporary file, does "do $tempfile" in Stata, deletes temporary file). I append it for reference (below), don't worry if it doesn't make sense.

                  See also http://teaching.sociology.ul.ie/bhal...rdpress/?p=135 for some comments on using console mode.

                  Code:
                  (defun delimit-do (start end toggle &optional clear message)
                  "Send the current region to the inferior ESS process, Stata do-editor style.
                  Creates a temporary file, \"do\"-es it, deletes it.
                  With prefix argument toggle the meaning of `ess-eval-visibly-p';
                  this does not apply when using the S-plus GUI, see `ess-eval-region-ddeclient'."
                  (interactive "r\nP")
                  ;;(untabify (point-min) (point-max))
                  ;;(untabify start end); do we really need to save-excursion?
                  (ess-force-buffer-current "Process to use: ")
                  (message "Starting evaluation...")
                  (setq message (or message "Eval region"))
                  
                  (save-excursion
                  ;; don't send new lines (avoid screwing the debugger)
                  (goto-char start)
                  (skip-chars-forward "\n\t ")
                  (setq start (point))
                  
                  (unless mark-active
                  (ess-blink-region start end))
                  
                  ;; don't send new lines at the end (avoid screwing the debugger)
                  (goto-char end)
                  (skip-chars-backward "\n\t ")
                  (setq end (point)))
                  
                  (let* (delimit
                  (commands (buffer-substring-no-properties start end))
                  (delimit-do-file (make-temp-file "delimit-do" nil ".do"))
                  (proc (get-process ess-local-process-name))
                  (visibly (if toggle (not ess-eval-visibly-p) ess-eval-visibly-p))
                  (dev-p (process-get proc 'developer))
                  (tb-p (process-get proc 'tracebug)))
                  ;; Go to the start of the section and look back for #delimit
                  ;; if found set delimit unless the delimiter is not ";"
                  (save-excursion
                  (goto-char start)
                  (setq delimit (re-search-backward "^#delimit +\\(.+\\)$" nil t))
                  (if delimit
                  (if (not (string-match ";" (match-string 1))) (setq delimit nil))))
                  
                  (with-temp-buffer
                  (if clear (insert "clear\n"))
                  (if delimit (insert "#delimit ;\n"
                  commands
                  "\n#delimit cr\n")
                  (insert commands "\n"))
                  (write-file delimit-do-file nil)
                  (kill-buffer (current-buffer)))
                  
                  (process-send-string
                  (get-ess-process ess-current-process-name)
                  (format "do %s\nrm %s\n" delimit-do-file delimit-do-file))
                  )
                  (if (and (fboundp 'deactivate-mark) ess-eval-deactivate-mark)
                  (deactivate-mark))
                  ;; return value
                  (list start end))
                  As mentioned by aarjpon in this forum, you need to change "get-ess-process" to "ess-get-process" in the last block of code for the command to work, due to updates in ESS. I wasted a day over this small detail, so I hope that this will be useful to other people with the same problem.
                  Notice that, on top of pasting the code above in ~/.emacs, you need to add in the same file a line to bind the program to a shorcut.
                  Code:
                  (define-key ess-mode-map (kbd "C-M-x") 'delimit-do)
                  remaps the Control Meta x combination to delimit-do.

                  Comment


                  • #10
                    I'm able to run Stata with Emacs / ESS, but I miss the GUI features such as a list of variables and their definitions. Is there a way to get to these information within ESS / Emacs / Stata console?

                    Comment


                    • #11
                      Hello everyone,
                      I have the exact same problem as Olivier on Emacs + ESS. I set the getenv PATH, etc. and when I want to use stata in ESS mode it starts Stata application and Emacs freezes. Does anyone have a solution for that? If yes, I would love to know it!

                      Originally posted by Olivier Ma View Post

                      Now when I highlight some lines and C-x C-r, Emacs, like always, asks me "Starting data directory?" after I give it my directory, instead of running the code in Emacs, it starts my Stata (in its own window) and does nothing, and Emacs freezes and I have to force close the window.
                      Thank you very much,

                      C

                      Comment

                      Working...
                      X