UP | HOME |

Procesar un documento .Rnw con knitr y LaTeX

Procesar un documento .Rnw con knitr y LaTeX

Un documento .Rnw contiene código ejecutable en R y en LaTeX. Primero procesamos el fichero pp2.Rnw para obtener el código en R, y también lo procesamos para crear el fichero .tex que contienen todos los resultados. Finalmente lo compilamos con latexmk.

setwd('~/temp')
require(knitr); 
knitr::knit('~/temp/pp2.Rnw',output='~/temp/pp2-woven.R',tangle = TRUE); 
knitr::knit('~/temp/pp2.Rnw', output='~/temp/pp2-woven.tex'); 
system('latexmk -pdf -shell-escape ~/temp/pp2-woven.tex')

En emacs se puede configurar una función que automatice el proceso.

;; Modified from http://delhey.info/inc/ess-rmarkdown.pdf
(defun etm-ess-rmd-rnw ()
  "Compile R markdown (.Rmd) or Sweave Rnw. Should work for any output type with Markdown."
  (interactive)
  ;; Check if attached R-session
  (condition-case nil
      (ess-get-process)
    (error
     (ess-switch-process)))
  (let* ((rmd-buf (current-buffer))
         (rmd-buf-extension ( file-name-extension (buffer-file-name)))
         (rmd-buf-nombre (file-name-sans-extension (buffer-file-name)))
         (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-rmd
                   (format "library(rmarkdown); rmarkdown::render(\"%s\")"
                           buffer-file-name))
                  ;; (R-cmd-rnw
                  ;;  (format "require(knitr); knitr::knit(\"%s\",tangle = TRUE); knitr::knit(\"%s\"); require(tools); texi2pdf(\"%s.tex\",index=TRUE); "
                  ;;          buffer-file-name buffer-file-name rmd-buf-nombre))
                  (R-cmd-rnw
                   ;; (format "require(knitr); knitr::knit(\"%s\",tangle = TRUE); knitr::knit(\"%s\"); system(\"latexmk -synctex=1 -pdf %s.tex\")"
                   (format "require(knitr); knitr::knit('%s',output='%s-woven.R',tangle = TRUE); knitr::knit('%s', output='%s-woven.tex'); system('latexmk -pdf -shell-escape %s-woven.tex')"
                           buffer-file-name rmd-buf-nombre buffer-file-name rmd-buf-nombre rmd-buf-nombre))
                  (Latex-cmd-rnw
                   (format "latexmk -synctex=1 -pdf -shell-escape %s.tex" rmd-buf-nombre))
                  (R-cmd "" ))
             (save-buffer)
             (if (member rmd-buf-extension '("Rnw" "Snw" "rnw"))
                 (progn ;
                   (setq R-cmd (format R-cmd-rnw))
                   (message "Running knitr on %s..." rmd-buf)
                   (ess-execute R-cmd 'buffer nil nil)
                   )
               (progn
                 (setq R-cmd (format R-cmd-rmd))
                 (message "Running rmarkdown on %s" rmd-buf)
                 ;; (when ( file-exists-p (concat rmd-buf-nombre ".html"))
                 ;;   (message "existe el %s" (concat rmd-buf-nombre ".html"))
                 ;;   (delete-file (concat rmd-buf-nombre ".html")); BUFFF! In order to prevent errors!
                 (ess-execute R-cmd 'buffer nil nil)
                 ))
             (switch-to-buffer rmd-buf)
             ;;          (message "Finalizada la compilación %s" rmd-buf)
             ;; (ess-show-buffer (buffer-name sbuffer) nil)
             ;; (when (not (member rmd-buf-extension '("Rnw" "Snw" "rnw")))
             ;;   (pm--display-file (concat rmd-buf-nombre ".html")) ;; polymode
             ;;   (message "Opening the output of rmarkdown  %s" (concat rmd-buf-nombre ".html")))
             )))))