Show R plots on a separate buffer
One feature that I always felt was missing in emacs and ESS was being able to display the plots on an emacs buffer instead of a separate window. This is specially troublesome when working on a Mac where for some reason we don’t have an “always-on-top” option for windows.
Therefore, I wrote some functions to save the plots to a temp pdf and display it on a buffer on the right of the R command. This approach is also useful when a command generates multiple plots since the pdf will contain all the plots as separate pages. See screencast to get an idea of what it does.
Alternatively, you can set the variable
nil so that the plot opens next to the current buffer instead.
(defvar rutils-show_plot_next_to_r_process t) (defun add-pdf-to-rcode(rcomm fname) "add pdf(tmpfile) and dev.off() to R command" (let* ( (newc (concat "pdf('" fname "')\n" rcomm "\n dev.off()")) ) (eval newc) ) ) (defun rutils-plot-region-or-paragraph() "execute region or paragraph and save tmp plot to pdf. Then open windows to show pdf" (interactive) (let* ( (fname (concat (make-temp-file "plot_") ".pdf")) ) (progn (if (use-region-p) (ess-eval-linewise (add-pdf-to-rcode (buffer-substring (region-beginning) (region-end)) fname)) (progn (ess-eval-linewise (add-pdf-to-rcode (thing-at-point 'paragraph) fname))) ) ;; (with-help-window "*plots*" ;; (find-ssfile-at-point) ;; ) (if rutils-show_plot_next_to_r_process (ess-switch-to-end-of-ESS) ) (if (window-in-direction 'right) (progn (select-window (window-in-direction 'right)) (find-file fname) ) (progn (split-window-right) (select-window (window-in-direction 'right)) (find-file fname) ) ) ;;(split-window-right) ;;(windmove-right) ) ) ) (define-key ess-mode-map (kbd "C-c g") 'rutils-plot-region-or-paragraph)