#### Peter Anthony Victor Diberardino pavdiber at uwaterloo.ca Tue Sep 29 14:30:08 EDT 2020

Hi everyone,

This summer I took the plunge to learn Emacs - a super flexible and customizable text editor that can do basically anything you usually do on your computer. The cost of such utility is the time and energy required to learn how to set things up and use all the key combinations Emacs requires. I think this deal is a good one, and now I do as much analysis/programming/writing in Emacs as possible.

In this email I’ll share what I’ve done to set up this workflow. I’ll keep things at a higher level for simplicity, but also because we all have different computers and operating systems, so the installations and troubleshooting probably won’t be the same. When we get our identical lab computers, I can make a step-by-step tutorial if people are interested.

Things you’ll need installed: R (with knitr)

• Within R console, install.packages('knitr')

LaTex (I use texlive)

• Within my ubuntu terminal, I ran sudo apt-get install <package> where <package> was each of texlive-latex-base, texlive-fonts-recommended, texlive-fonts-extra

Emacs

• Find your init file. For me it was .emacs.d/init.el This is file to customize your emacs experience. To use Sweave (.Rnw) files, you’ll need ‘polymode’, ‘poly-R’ and ‘ess’. I think all of these things will be installed/activated if you use my init file. Below are the exact contents of my init.el file. If you copy and paste these lines into your init file, you should get the same functionality I have.


;; Elisp file for R coding with Emacs

;; Add MELPA repository and initialise the package manager
(require 'package)
'("melpa" . "https://melpa.org/packages/"))
(package-initialize)

;; Install use-package,in case it does not exist yet
;; The use-package software will install all other packages as required
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))

;; ESS configurationEmacs Speaks Statistics
(use-package ess
:ensure t
)

;; Auto completion
(use-package company
:ensure t
:config
(setq company-idle-delay 0)
(setq company-minimum-prefix-length 2)
(global-company-mode t)
)

; Parentheses
(use-package highlight-parentheses
:ensure t
:config
(progn
(highlight-parentheses-mode)
(global-highlight-parentheses-mode))
)

;; Im trying to add the stuff for Rnw here manually
(use-package polymode
:ensure t)

(use-package poly-R
:ensure t)

;;(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
;; '(package-selected-packages
;;   (quote
;;    (poly-R polymode highlight-parentheses company use-package ess))))
;;(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
;; )

(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(inhibit-startup-screen t)
'(package-selected-packages
(quote
(ess use-package poly-R highlight-parentheses company)))
'(safe-local-variable-values (quote ((Tex-engine . xetex) (TeX-master
. "draft")))))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)

(setq polymode-weaver-output-file-format "%s")
(setq polymode-exporter-output-file-format "%s")

(remove-hook 'flymake-diagnostic-functions 'flymake-proc-legacy-flymake)

;; keybindings for ess
;; to make it easy to evaluate single chunck in ESS mode
;; from http://hansekbrand.se/code/EmacsKeyboardBindings.html
(defun my-ess-hook ()
"Add my keybindings to ESS mode."
(local-set-key (kbd "<f5>") 'ess-eval-chunk)
(local-set-key (kbd "<f6>") 'ess-noweb-next-chunk)
)



Now you can open an Rnw file with emacs. You can find minimal examples online eg https://github.com/yihui/knitr-examples/blob/master/002-minimal.Rnw

If all is working right you should be able to compile everything into a pdf by pressing Alt and the n key at the same time (Alt first), then pressing the e key on its own. This key shortcut is denoted M-n e where ‘M’ represents the “Meta” aka Alt key. The first time you export to pdf, you will be prompted to choose your exporter. Type in pdflatex at the bottom of the window then enter. Then you will be prompted to choose your weaver. Type in knitr. A new window will appear with your pdf! Note, I need to press the pgUp/pdDn buttons to get from one page to the next.

Your Rnw file is essentially latex, so refer to latex help with document preamble, creating sections/figure/equations etc. The parts of your document that begin with <<name>>= and end with @ are R code chunks, where you can do standard R stuff. I customized my emacs to be able to evaluate chunks in a separate R buffer by pressing F5. You should have this too if you use my init file.

As I mentioned in the lab meeting yesterday, I found a way to have the pdf output compile into an apa style manuscript automatically. This can be done with the latex package apa6, which I installed through sudo apt-get install texlive-publishers. Now if you set \documentclass[man]{apa6} in your .Rnw preamble before \begin{document}, it should look like an apa style manuscript. Take a look at all the other things you can do with apa6 too.

Citations are a bit finicky, and I can’t say I know exactly how I got it work. But I think this is it: first install biber through sudo apt-get install texlive-bibtex-extra. In your Rnw before \begin{document} add \usepackage[style=apa,sortcites=true,sorting=nyt,backend=biber]{biblatex} and \addbibresource{<filename>.bib} where <filename> is the name of some file with bibtex citations listed within.

Whenever you add any citations to your .bib (which should be saved in the same folder as your Rnw) you’ll need to run: biber <filename> in your terminal before M-n e you Rnw again. Important this is biber <filename> NOT biber <filename>.bib

Now within your Rnw, you can add \cite{<citation>} within your text where <citation> is the bibtex shortform name from the bib file, and \printbibliography just before \end{document}. In text citations and the reference section should be all good.

Hopefully these direction are clear enough to point you in the right direction. Please let me know if I can clarify or help with anything.

Peter