Run all R fragments at once from an Rmd document

A minimal Rmarkdown document has a YAML header, markup syntax, and R code fragments. To edit such a multilingual file, I use the Emacs editor, and the buffer in which the Rmd document is open is in polymode .

A typical document contains more than one R fragment. When I write / debug an R fragment in the middle of an Rmd document, I have a second buffer in ESS mode, where R works inside, and I often have to re-execute all previous fragments by sending R commands from the Rmd file (1st buffer) to console R (second buffer).

Is there one command that allows you to execute all commands from all pieces?

From another question , it looks like org-mode can do this. But can this be done in my conditions?

+4
source share
1 answer

If you only have the source code R in Rmarkdown and you want to easily evaluate it, spinfrom knitrit may be easier ( see. Here ).

I prefer this (spin) solution, since all markdown / Rmarkdown mumbo-jumbo are contained in regular R comments, so the buffer can be considered like regular source code. But the following should evaluate all fragments of the R code in the polymode buffer (not fully verified).

(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))))))
+3
source

Source: https://habr.com/ru/post/1662433/


All Articles