I define a function ess-rmarkdown
to compile RMarkdown documents with polymode.
(use-package polymode
:ensure t
:config
(use-package poly-R)
(use-package poly-markdown)
;;; MARKDOWN
(add-to-list 'auto-mode-alist '("\\.md\\'" . poly-markdown-mode))
;;; R modes
(add-to-list 'auto-mode-alist '("\\.Snw\\'" . poly-noweb+r-mode))
(add-to-list 'auto-mode-alist '("\\.Rnw\\'" . poly-noweb+r-mode))
(add-to-list 'auto-mode-alist '("\\.Rmd\\'" . poly-markdown+r-mode))
(markdown-toggle-math t)
;; from https://gist.github.com/benmarwick/ee0f400b14af87a57e4a
;; compile rmarkdown to HTML or PDF with M-n s
;; use YAML in Rmd doc to specify the usual options
;; which can be seen at http://rmarkdown.rstudio.com/
;; thanks http://roughtheory.com/posts/ess-rmarkdown.html
(defun ess-rmarkdown ()
"Compile R markdown (.Rmd). Should work for any output type."
(interactive)
; Check if attached R-session
(condition-case nil
(ess-get-process)
(error
(ess-switch-process)))
(let* ((rmd-buf (current-buffer)))
(save-excursion
(let* ((sprocess (ess-get-process ess-current-process-name))
(sbuffer (process-buffer sprocess))
(buf-coding (symbol-name buffer-file-coding-system))
(R-cmd
(format "library(rmarkdown); rmarkdown::render(\"%s\")"
buffer-file-name)))
(message "Running rmarkdown on %s" buffer-file-name)
(ess-execute R-cmd 'buffer nil nil)
(switch-to-buffer rmd-buf)
(ess-show-buffer (buffer-name sbuffer) nil)))))
)
To bind ess-rmarkdown
function to F5
key I use:
(with-eval-after-load 'polymode
(define-key polymode-mode-map (kbd "<f5>") 'ess-rmarkdown))
Unfortunately, after reloading my init.el
file, the following warning appears:
Symbol's value as variable is void: polymode-mode-map
I kindly as for any advice.
EDIT: Entering emacs with --debug-init
returns me:
Debugger entered--Lisp error: (void-variable polymode-mode-map)
(define-key polymode-mode-map (kbd "<f5>") (quote ess-rmarkdown))
(lambda nil (define-key polymode-mode-map (kbd "<f5>") (quote ess-rmarkdown)))()
eval-after-load(polymode (lambda nil (define-key polymode-mode-map (kbd "<f5>") (quote ess-rmarkdown))))
eval-buffer(#<buffer *load*> nil "/home/andrej/.emacs.d/init.el" nil t) ; Reading at buffer position 4255
load-with-code-conversion("/home/andrej/.emacs.d/init.el" "/home/andrej/.emacs.d/init.el" t t)
load("/home/andrej/.emacs.d/init" t t)
#[0 "\205\266