#+BEGIN_COMMENT .. title: Using stata_kernel and Emacs Orgmode for reproducible research goodness .. slug: stata_kernel_emacs .. date: 2020-07-07 10:31:50 UTC+01:00 .. tags: stata, orgmode .. status: .. has_math: yes .. category: .. link: .. description: .. type: text #+END_COMMENT This post is hopefully the last in a series of posts outlining how to use =Stata= in a proper dynamic document/reproducible research setting using Emacs. As of the summer of 2020, I am only using [[https://github.com/kylebarron/stata_kernel][=stata_kernel=]] for my own work and no longer recommend using [[./stata-and-literate-programming-in-emacs-org-mode.html][my customized =ob-ipython.el=]] for reasons described [[https://gitlab.com/robhicks/ob-stata.el/-/issues/10#note_374134363][here]]. This post shows the installation steps to get this working and some usability recommendations if using Org-mode. Before proceeding with anything below, make sure you complete the [[./stata_kernel_jupyterlab.html#Python Preliminaries]["Python Preliminaries" steps]] first. #+HTML: The following will give you a quick idea of how things work once things are working properly: [[https://raw.githubusercontent.com/roblem/stata_kernel/master/docs/src/img/emacs-orgmode-jupyter-stata-demo.gif]] * Running Stata Commands in Emacs #+BEGIN_SRC emacs-lisp :exports none :eval never-export (setf (cdr (assoc :results org-babel-default-header-args:ipython)) "output replace") #+END_SRC #+RESULTS: : output replace Once you have setup the python environment following the steps above, do this in emacs: 1. Install and load [[https://github.com/dzop/emacs-jupyter][emacs-jupyter.el]] 2. Ensure that you have activated the python environment where =stata_kernel= is available 3. Add the following lines to your =init.el=: #+BEGIN_SRC emacs-lisp :exports code :eval never-export (when (functionp 'module-load) (use-package jupyter) (with-eval-after-load 'org (org-babel-do-load-languages 'org-babel-load-languages '((jupyter . t)))) (with-eval-after-load 'jupyter (define-key jupyter-repl-mode-map (kbd "C-l") #'jupyter-repl-clear-cells) (define-key jupyter-repl-mode-map (kbd "TAB") #'company-complete-common-or-cycle) (define-key jupyter-org-interaction-mode-map (kbd "TAB") #'company-complete-common-or-cycle) (define-key jupyter-repl-interaction-mode-map (kbd "C-c C-r") #'jupyter-eval-line-or-region) (define-key jupyter-repl-interaction-mode-map (kbd "C-c M-r") #'jupyter-repl-restart-kernel) (define-key jupyter-repl-interaction-mode-map (kbd "C-c M-k") #'jupyter-shutdown-kernel) (add-hook 'jupyter-org-interaction-mode-hook (lambda () (company-mode) (setq company-backends '(company-capf)))) (add-hook 'jupyter-repl-mode-hook (lambda () (company-mode) :config (set-face-attribute 'jupyter-repl-input-prompt nil :foreground "black") :config (set-face-attribute 'jupyter-repl-output-prompt nil :foreground "grey") (setq company-backends '(company-capf)))) (setq jupyter-repl-prompt-margin-width 4))) ;; associated jupyter-stata with stata (fixes fontification if using pygmentize for html export) (add-to-list 'org-src-lang-modes '("jupyter-stata" . stata)) (add-to-list 'org-src-lang-modes '("Jupyter-Stata" . stata)) ;; you **may** need this for latex output syntax highlighting ;; (add-to-list 'org-latex-minted-langs '(stata "stata")) #+END_SRC Additionally, remove =("ipython" . "ipython")= and =("stata" . "stata")= from ='org-babel-load-languages= in your =init.el= (if you have =ob-ipython= installed). * Usage Stata code blocks need to look like this: #+BEGIN_SRC jupyter-stata :session stata :exports code :eval never-export ,#+BEGIN_SRC jupyter-stata :session stata :kernel stata sysuse auto sum ,#+END_SRC #+END_SRC #+RESULTS: #+begin_example (1978 Automobile Data) Variable | Obs Mean Std. Dev. Min Max -------------+--------------------------------------------------------- make | 0 price | 74 6165.257 2949.496 3291 15906 mpg | 74 21.2973 5.785503 12 41 rep78 | 69 3.405797 .9899323 1 5 headroom | 74 2.993243 .8459948 1.5 5 -------------+--------------------------------------------------------- trunk | 74 13.75676 4.277404 5 23 weight | 74 3019.459 777.1936 1760 4840 length | 74 187.9324 22.26634 142 233 turn | 74 39.64865 4.399354 31 51 displacement | 74 197.2973 91.83722 79 425 -------------+--------------------------------------------------------- gear_ratio | 74 3.014865 .4562871 2.19 3.89 foreign | 74 .2972973 .4601885 0 1 #+end_example Note the header arguments "=jupyter-stata :session stata=". The session name (in this case "stata") can be anything you'd like but can't be missing. Running this code yields both code with syntax highlighting and output: #+BEGIN_SRC jupyter-stata :session stata :results output :exports both :eval never-export sum price trunk headroom #+END_SRC #+RESULTS: : : (1978 Automobile Data) : : : Variable | Obs Mean Std. Dev. Min Max : -------------+--------------------------------------------------------- : price | 74 6165.257 2949.496 3291 15906 : trunk | 74 13.75676 4.277404 5 23 : headroom | 74 2.993243 .8459948 1.5 5 Display the first 5 observations using the R-like head magic: #+BEGIN_SRC jupyter-stata :session stata :results output :exports both :eval never-export %head 5 if price > 3000 #+END_SRC #+RESULTS: #+begin_export html
make | price | mpg | rep78 | headroom | trunk | weight | length | turn | displacement | gear_ratio | foreign | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | AMC Concord | 4099 | 22 | 3 | 2.5 | 11 | 2930 | 186 | 40 | 121 | 3.5799999 | Domestic |
2 | AMC Pacer | 4749 | 17 | 3 | 3 | 11 | 3350 | 173 | 40 | 258 | 2.53 | Domestic |
3 | AMC Spirit | 3799 | 22 | . | 3 | 12 | 2640 | 168 | 35 | 121 | 3.0799999 | Domestic |
4 | Buick Century | 4816 | 20 | 3 | 4.5 | 16 | 3250 | 196 | 40 | 196 | 2.9300001 | Domestic |
5 | Buick Electra | 7827 | 15 | 4 | 4 | 20 | 4080 | 222 | 43 | 350 | 2.4100001 | Domestic |