execute all R chunks at once from an Rmd document
If you only have R source code in your Rmarkdown, and want to be able to interactively evaluate it easily, spin
from knitr
might be easier (see here).
I prefer that (spin) solution, since all the markdown/Rmarkdown mumbo-jumbo is contained within normal R comments, so the buffer can be treated like regular source code. But, the following should evaluate all the R code chunks in a polymode buffer (not tested thoroughly).
(eval-when-compile
(require 'polymode-core) ;; SO format :('
(defvar pm/chunkmode))
(declare-function pm-map-over-spans "polymode-core")
(declare-function pm-narrow-to-span "polymode-core")
(defun rmd-send-chunk ()
"Send current R chunk to ess process."
(interactive)
(and (eq (oref pm/chunkmode :mode) 'r-mode) ;;'
(pm-with-narrowed-to-span nil
(goto-char (point-min))
(forward-line)
(ess-eval-region (point) (point-max) nil nil 'R)))) ;;'
(defun rmd-send-buffer (arg)
"Send all R code blocks in buffer to ess process. With prefix
send regions above point."
(interactive "P")
(save-restriction
(widen)
(save-excursion
(pm-map-over-spans
'rmd-send-chunk (point-min) ;;'
;; adjust this point to send prior regions
(if arg (point) (point-max))))))
If using Poly-Markdown+R, the command to evaluate all R snippets in an Rmarkdown document is M-n v b
.
Reference