Skip to content
This repository has been archived by the owner on Apr 8, 2023. It is now read-only.

Latest commit

 

History

History
168 lines (138 loc) · 5.5 KB

starter-kit-stats.org

File metadata and controls

168 lines (138 loc) · 5.5 KB

Starter Kit Statistics

This is part of the Emacs Starter Kit.

This file provides settings for ESS and R.

R and ESS

Load ESS: Emacs Speaks Statistics, and several further tweaks for R.

Load ESS

#+src-name: ess-mode

  (require 'ess-site)

;; smart-underscore and ess-smart-S-assign-key have been removed in ESS 19.04 so we need to set them up manually
  (define-key ess-r-mode-map "_" #'ess-insert-assign)
  (define-key inferior-ess-r-mode-map "_" #'ess-insert-assign)

Coding Hooks

(add-hook 'ess-mode-hook 'run-starter-kit-coding-hook)
  (add-hook 'ess-R-post-run-hook 'smartparens-mode)

Define Rnw-mode and make LaTeX aware of it.

If you use Rnw documents, tangle this section. (Rnw files have been mostly superseded by Rmd files.)

(add-to-list 'auto-mode-alist '("\\.Rnw\\'" . Rnw-mode))
(add-to-list 'auto-mode-alist '("\\.Snw\\'" . Rnw-mode))

;; Make TeX and RefTex aware of Snw and Rnw files
(setq reftex-file-extensions
      '(("Snw" "Rnw" "nw" "tex" ".tex" ".ltx") ("bib" ".bib")))
(setq TeX-file-extensions
      '("Snw" "Rnw" "nw" "tex" "sty" "cls" "ltx" "texi" "texinfo"))

;; Lets you do 'C-c C-c Sweave' from your Rnw file
(add-hook 'Rnw-mode-hook
	  (lambda ()
	    (add-to-list 'TeX-command-list
			 '("Sweave" "R CMD Sweave %s"
			   TeX-run-command nil (latex-mode) :help "Run Sweave") t)
	    (add-to-list 'TeX-command-list
			 '("LatexSweave" "%l %(mode) %s"
			   TeX-run-TeX nil (latex-mode) :help "Run Latex after Sweave") t)
	    (setq TeX-command-default "Sweave")))

Use Knitr to process Sweave documents

(setq ess-swv-processor "'knitr")

Use Polymode

Polymode is a package that supports multiple modes inside a single buffer. It is aimed particularly at literate programming approaches and supports, e.g., R and markdown in a single .Rmd file. So it is very useful with knitr in R.

(require 'poly-R)
(require 'poly-markdown)

Make shift-enter to a lot in ESS.

Use shift-enter to split window & launch R (if not running), execute highlighted region (if R running & area highlighted), or execute current line (and move to next line, skipping comments). Nice. See http://www.emacswiki.org/emacs/EmacsSpeaksStatistics, FelipeCsaszar. Adapted to split vertically instead of horizontally. #+src-name: ess-shift-enter

(setq ess-ask-for-ess-directory nil)
  (setq ess-local-process-name "R")
  (setq ansi-color-for-comint-mode 'filter)
  (setq comint-scroll-to-bottom-on-input t)
  (setq comint-scroll-to-bottom-on-output t)
  (setq comint-move-point-for-output t)
  (defun my-ess-start-R ()
    (interactive)
    (unless (mapcar (lambda (s) (string-match "*R" (buffer-name s))) (buffer-list))
;;    (unless (string-match "*R" (mapcar (function buffer-name) (buffer-list)))
      (progn
	(delete-other-windows)
	(setq w1 (selected-window))
	(setq w1name (buffer-name))
	(setq w2 (split-window w1 nil t))
	(R)
	(set-window-buffer w2 "*R*")
	(set-window-buffer w1 w1name))))
  (defun my-ess-eval ()
    (interactive)
    (my-ess-start-R)
    (if (and transient-mark-mode mark-active)
	(call-interactively 'ess-eval-region)
      (call-interactively 'ess-eval-line-and-step)))
  (add-hook 'ess-mode-hook
	    '(lambda()
	       (local-set-key [(shift return)] 'my-ess-eval)))
  (add-hook 'inferior-ess-mode-hook
	    '(lambda()
	       (local-set-key [C-up] 'comint-previous-input)
	       (local-set-key [C-down] 'comint-next-input)))
 (add-hook 'Rnw-mode-hook 
          '(lambda() 
             (local-set-key [(shift return)] 'my-ess-eval))) 
  (require 'ess-site)

Add a Keyboard Shortcut for the Pipe Operator

Assignment in ESS is shift-minus; by extension we’ll use M-shift-minus for inserting the `%>%` operator.

#+src-name: ess-pipe-shortcut

(defun my_pipe_operator ()
  "R/ESS %>% operator"
  (interactive)
  (just-one-space 1)
  (insert "%>%")
  (reindent-then-newline-and-indent))
(define-key ess-mode-map (kbd "M-_") 'my_pipe_operator)
(define-key inferior-ess-mode-map (kbd "M-_") 'my_pipe_operator)

Add a Keyboard Shortcut for Rmd chunks

#+src-name: rmd-chunk-insert

;;;Insert new chunk for Rmarkdown
(defun kjh-insert-r-chunk (header) 
  "Insert an r-chunk in markdown mode." 
  (interactive "sLabel: ") 
  (insert (concat "```{r " header "}\n\n```")) 
  (forward-line -1))

(global-set-key (kbd "C-c i") 'kjh-insert-r-chunk)

Uniquify Buffer Names

This is useful for when you have buffers with many similar names, as when there are various open files from different folders named analysis.R or similar.

(require 'uniquify)
(setq uniquify-buffer-name-style 'post-forward-angle-brackets)  

lintr and flycheck

lintr checks your R code for style and syntax errors. It’s an R library that integrates with flycheck. You must install lintr from R. Flycheck can also check code in many other languages. You will need to install linters for them separately as well. See the flycheck documentation for details.

(add-hook 'after-init-hook #'global-flycheck-mode)
  (add-hook 'ess-mode-hook
            (lambda () (flycheck-mode t)))