ob-R.el (20019B)
1 ;;; ob-R.el --- Babel Functions for R -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2009-2023 Free Software Foundation, Inc. 4 5 ;; Author: Eric Schulte 6 ;; Dan Davison 7 ;; Maintainer: Jeremie Juste <jeremiejuste@gmail.com> 8 ;; Keywords: literate programming, reproducible research, R, statistics 9 ;; URL: https://orgmode.org 10 11 ;; This file is part of GNU Emacs. 12 13 ;; GNU Emacs is free software: you can redistribute it and/or modify 14 ;; it under the terms of the GNU General Public License as published by 15 ;; the Free Software Foundation, either version 3 of the License, or 16 ;; (at your option) any later version. 17 18 ;; GNU Emacs is distributed in the hope that it will be useful, 19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 ;; GNU General Public License for more details. 22 23 ;; You should have received a copy of the GNU General Public License 24 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 25 26 ;;; Commentary: 27 28 ;; Org-Babel support for evaluating R code 29 30 ;;; Code: 31 32 (require 'org-macs) 33 (org-assert-version) 34 35 (require 'cl-lib) 36 (require 'ob) 37 38 (declare-function orgtbl-to-tsv "org-table" (table params)) 39 (declare-function R "ext:essd-r" (&optional start-args)) 40 (declare-function inferior-ess-send-input "ext:ess-inf" ()) 41 (declare-function ess-make-buffer-current "ext:ess-inf" ()) 42 (declare-function ess-eval-buffer "ext:ess-inf" (vis)) 43 (declare-function ess-wait-for-process "ext:ess-inf" 44 (&optional proc sec-prompt wait force-redisplay)) 45 46 (defconst org-babel-header-args:R 47 '((width . :any) 48 (height . :any) 49 (bg . :any) 50 (units . :any) 51 (pointsize . :any) 52 (antialias . :any) 53 (quality . :any) 54 (compression . :any) 55 (res . :any) 56 (type . :any) 57 (family . :any) 58 (title . :any) 59 (fonts . :any) 60 (version . :any) 61 (paper . :any) 62 (encoding . :any) 63 (pagecentre . :any) 64 (colormodel . :any) 65 (useDingbats . :any) 66 (horizontal . :any) 67 (results . ((file list vector table scalar verbatim) 68 (raw html latex org code pp drawer) 69 (replace silent none append prepend) 70 (output value graphics)))) 71 "R-specific header arguments.") 72 73 (defconst ob-R-safe-header-args 74 (append org-babel-safe-header-args 75 '(:width :height :bg :units :pointsize :antialias :quality 76 :compression :res :type :family :title :fonts 77 :version :paper :encoding :pagecentre :colormodel 78 :useDingbats :horizontal)) 79 "Header args which are safe for R babel blocks. 80 81 See `org-babel-safe-header-args' for documentation of the format of 82 this variable.") 83 84 (defvar org-babel-default-header-args:R '()) 85 (put 'org-babel-default-header-args:R 'safe-local-variable 86 (org-babel-header-args-safe-fn ob-R-safe-header-args)) 87 88 (defcustom org-babel-R-command "R --slave --no-save" 89 "Name of command to use for executing R code." 90 :group 'org-babel 91 :version "24.1" 92 :type 'string) 93 94 (defvar ess-current-process-name) ; dynamically scoped 95 (defvar ess-local-process-name) ; dynamically scoped 96 (defun org-babel-edit-prep:R (info) 97 (let ((session (cdr (assq :session (nth 2 info))))) 98 (when (and session 99 (string-prefix-p "*" session) 100 (string-suffix-p "*" session)) 101 (org-babel-R-initiate-session session nil)))) 102 103 ;; The usage of utils::read.table() ensures that the command 104 ;; read.table() can be found even in circumstances when the utils 105 ;; package is not in the search path from R. 106 (defconst ob-R-transfer-variable-table-with-header 107 "%s <- local({ 108 con <- textConnection( 109 %S 110 ) 111 res <- utils::read.table( 112 con, 113 header = %s, 114 row.names = %s, 115 sep = \"\\t\", 116 as.is = TRUE 117 ) 118 close(con) 119 res 120 })" 121 "R code used to transfer a table defined as a variable from org to R. 122 123 This function is used when the table contains a header.") 124 125 (defconst ob-R-transfer-variable-table-without-header 126 "%s <- local({ 127 con <- textConnection( 128 %S 129 ) 130 res <- utils::read.table( 131 con, 132 header = %s, 133 row.names = %s, 134 sep = \"\\t\", 135 as.is = TRUE, 136 fill = TRUE, 137 col.names = paste(\"V\", seq_len(%d), sep =\"\") 138 ) 139 close(con) 140 res 141 })" 142 "R code used to transfer a table defined as a variable from org to R. 143 144 This function is used when the table does not contain a header.") 145 146 (defun org-babel-expand-body:R (body params &optional _graphics-file) 147 "Expand BODY according to PARAMS, return the expanded body." 148 (mapconcat 'identity 149 (append 150 (when (cdr (assq :prologue params)) 151 (list (cdr (assq :prologue params)))) 152 (org-babel-variable-assignments:R params) 153 (list body) 154 (when (cdr (assq :epilogue params)) 155 (list (cdr (assq :epilogue params))))) 156 "\n")) 157 158 (defun org-babel-execute:R (body params) 159 "Execute a block of R code. 160 This function is called by `org-babel-execute-src-block'." 161 (save-excursion 162 (let* ((result-params (cdr (assq :result-params params))) 163 (result-type (cdr (assq :result-type params))) 164 (async (org-babel-comint-use-async params)) 165 (session (org-babel-R-initiate-session 166 (cdr (assq :session params)) params)) 167 (graphics-file (and (member "graphics" (assq :result-params params)) 168 (org-babel-graphical-output-file params))) 169 (colnames-p (unless graphics-file (cdr (assq :colnames params)))) 170 (rownames-p (unless graphics-file (cdr (assq :rownames params)))) 171 (full-body 172 (let ((inside 173 (list (org-babel-expand-body:R body params graphics-file)))) 174 (mapconcat 'identity 175 (if graphics-file 176 (append 177 (list (org-babel-R-construct-graphics-device-call 178 graphics-file params)) 179 inside 180 (list "},error=function(e){plot(x=-1:1, y=-1:1, type='n', xlab='', ylab='', axes=FALSE); text(x=0, y=0, labels=e$message, col='red'); paste('ERROR', e$message, sep=' : ')}); dev.off()")) 181 inside) 182 "\n"))) 183 (result 184 (org-babel-R-evaluate 185 session full-body result-type result-params 186 (or (equal "yes" colnames-p) 187 (org-babel-pick-name 188 (cdr (assq :colname-names params)) colnames-p)) 189 (or (equal "yes" rownames-p) 190 (org-babel-pick-name 191 (cdr (assq :rowname-names params)) rownames-p)) 192 async))) 193 (if graphics-file nil result)))) 194 195 (defun org-babel-prep-session:R (session params) 196 "Prepare SESSION according to the header arguments specified in PARAMS." 197 (let* ((session (org-babel-R-initiate-session session params)) 198 (var-lines (org-babel-variable-assignments:R params))) 199 (org-babel-comint-in-buffer session 200 (mapc (lambda (var) 201 (end-of-line 1) (insert var) (comint-send-input nil t) 202 (org-babel-comint-wait-for-output session)) 203 var-lines)) 204 session)) 205 206 (defun org-babel-load-session:R (session body params) 207 "Load BODY into SESSION." 208 (save-window-excursion 209 (let ((buffer (org-babel-prep-session:R session params))) 210 (with-current-buffer buffer 211 (goto-char (process-mark (get-buffer-process (current-buffer)))) 212 (insert (org-babel-chomp body))) 213 buffer))) 214 215 ;; helper functions 216 217 (defun org-babel-variable-assignments:R (params) 218 "Return list of R statements assigning the block's variables." 219 (let ((vars (org-babel--get-vars params))) 220 (mapcar 221 (lambda (pair) 222 (org-babel-R-assign-elisp 223 (car pair) (cdr pair) 224 (equal "yes" (cdr (assq :colnames params))) 225 (equal "yes" (cdr (assq :rownames params))))) 226 (mapcar 227 (lambda (i) 228 (cons (car (nth i vars)) 229 (org-babel-reassemble-table 230 (cdr (nth i vars)) 231 (cdr (nth i (cdr (assq :colname-names params)))) 232 (cdr (nth i (cdr (assq :rowname-names params))))))) 233 (number-sequence 0 (1- (length vars))))))) 234 235 (defun org-babel-R-quote-tsv-field (s) 236 "Quote field S for export to R." 237 (if (stringp s) 238 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"") 239 (format "%S" s))) 240 241 (defun org-babel-R-assign-elisp (name value colnames-p rownames-p) 242 "Construct R code assigning the elisp VALUE to a variable named NAME." 243 (if (listp value) 244 (let* ((lengths (mapcar 'length (cl-remove-if-not 'listp value))) 245 (max (if lengths (apply 'max lengths) 0)) 246 (min (if lengths (apply 'min lengths) 0))) 247 ;; Ensure VALUE has an orgtbl structure (depth of at least 2). 248 (unless (listp (car value)) (setq value (mapcar 'list value))) 249 (let ((file (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field))) 250 (header (if (or (eq (nth 1 value) 'hline) colnames-p) 251 "TRUE" "FALSE")) 252 (row-names (if rownames-p "1" "NULL"))) 253 (if (= max min) 254 (format ob-R-transfer-variable-table-with-header 255 name file header row-names) 256 (format ob-R-transfer-variable-table-without-header 257 name file header row-names max)))) 258 (cond ((integerp value) (format "%s <- %s" name (concat (number-to-string value) "L"))) 259 ((floatp value) (format "%s <- %s" name value)) 260 ((stringp value) (format "%s <- %S" name (org-no-properties value))) 261 (t (format "%s <- %S" name (prin1-to-string value)))))) 262 263 264 (defvar ess-ask-for-ess-directory) ; dynamically scoped 265 (defun org-babel-R-initiate-session (session params) 266 "If there is not a current R process then create one." 267 (unless (string= session "none") 268 (let ((session (or session "*R*")) 269 (ess-ask-for-ess-directory 270 (and (boundp 'ess-ask-for-ess-directory) 271 ess-ask-for-ess-directory 272 (not (cdr (assq :dir params)))))) 273 (if (org-babel-comint-buffer-livep session) 274 session 275 (save-window-excursion 276 (when (get-buffer session) 277 ;; Session buffer exists, but with dead process 278 (set-buffer session)) 279 (require 'ess) (R) 280 (let ((R-proc (get-process (or ess-local-process-name 281 ess-current-process-name)))) 282 (while (process-get R-proc 'callbacks) 283 (ess-wait-for-process R-proc))) 284 (rename-buffer 285 (if (bufferp session) 286 (buffer-name session) 287 (if (stringp session) 288 session 289 (buffer-name)))) 290 (current-buffer)))))) 291 292 (defun org-babel-R-associate-session (session) 293 "Associate R code buffer with an R session. 294 Make SESSION be the inferior ESS process associated with the 295 current code buffer." 296 (setq ess-local-process-name 297 (process-name (get-buffer-process session))) 298 (ess-make-buffer-current)) 299 300 (defvar org-babel-R-graphics-devices 301 '((:bmp "bmp" "filename") 302 (:jpg "jpeg" "filename") 303 (:jpeg "jpeg" "filename") 304 (:tikz "tikz" "file") 305 (:tiff "tiff" "filename") 306 (:png "png" "filename") 307 (:svg "svg" "file") 308 (:pdf "pdf" "file") 309 (:ps "postscript" "file") 310 (:postscript "postscript" "file")) 311 "An alist mapping graphics file types to R functions. 312 313 Each member of this list is a list with three members: 314 1. the file extension of the graphics file, as an elisp :keyword 315 2. the R graphics device function to call to generate such a file 316 3. the name of the argument to this function which specifies the 317 file to write to (typically \"file\" or \"filename\")") 318 319 (defun org-babel-R-construct-graphics-device-call (out-file params) 320 "Construct the call to the graphics device." 321 (let* ((allowed-args '(:width :height :bg :units :pointsize 322 :antialias :quality :compression :res 323 :type :family :title :fonts :version 324 :paper :encoding :pagecentre :colormodel 325 :useDingbats :horizontal)) 326 (device (file-name-extension out-file)) 327 (device-info (or (assq (intern (concat ":" device)) 328 org-babel-R-graphics-devices) 329 (assq :png org-babel-R-graphics-devices))) 330 (extra-args (cdr (assq :R-dev-args params))) filearg args) 331 (setq device (nth 1 device-info)) 332 (setq filearg (nth 2 device-info)) 333 (setq args (mapconcat 334 (lambda (pair) 335 (if (member (car pair) allowed-args) 336 (format ",%s=%S" 337 (substring (symbol-name (car pair)) 1) 338 (cdr pair)) "")) 339 params "")) 340 (format "%s(%s=\"%s\"%s%s%s); tryCatch({" 341 device filearg out-file args 342 (if extra-args "," "") (or extra-args "")))) 343 344 (defconst org-babel-R-eoe-indicator "'org_babel_R_eoe'") 345 (defconst org-babel-R-eoe-output "[1] \"org_babel_R_eoe\"") 346 347 (defconst org-babel-R-write-object-command "{ 348 function(object,transfer.file) { 349 object 350 invisible( 351 if ( 352 inherits( 353 try( 354 { 355 tfile<-tempfile() 356 write.table(object, file=tfile, sep=\"\\t\", 357 na=\"\",row.names=%s,col.names=%s, 358 quote=FALSE) 359 file.rename(tfile,transfer.file) 360 }, 361 silent=TRUE), 362 \"try-error\")) 363 { 364 if(!file.exists(transfer.file)) 365 file.create(transfer.file) 366 } 367 ) 368 } 369 }(object=%s,transfer.file=\"%s\")" 370 "Template for an R command to evaluate a block of code and write result to file. 371 372 Has four %s escapes to be filled in: 373 1. Row names, \"TRUE\" or \"FALSE\" 374 2. Column names, \"TRUE\" or \"FALSE\" 375 3. The code to be run (must be an expression, not a statement) 376 4. The name of the file to write to") 377 378 (defun org-babel-R-evaluate 379 (session body result-type result-params column-names-p row-names-p async) 380 "Evaluate R code in BODY." 381 (if session 382 (if async 383 (ob-session-async-org-babel-R-evaluate-session 384 session body result-type column-names-p row-names-p) 385 (org-babel-R-evaluate-session 386 session body result-type result-params column-names-p row-names-p)) 387 (org-babel-R-evaluate-external-process 388 body result-type result-params column-names-p row-names-p))) 389 390 (defun org-babel-R-evaluate-external-process 391 (body result-type result-params column-names-p row-names-p) 392 "Evaluate BODY in external R process. 393 If RESULT-TYPE equals `output' then return standard output as a 394 string. If RESULT-TYPE equals `value' then return the value of the 395 last statement in BODY, as elisp." 396 (cl-case result-type 397 (value 398 (let ((tmp-file (org-babel-temp-file "R-"))) 399 (org-babel-eval org-babel-R-command 400 (format org-babel-R-write-object-command 401 (if row-names-p "TRUE" "FALSE") 402 (if column-names-p 403 (if row-names-p "NA" "TRUE") 404 "FALSE") 405 (format "{function ()\n{\n%s\n}}()" body) 406 (org-babel-process-file-name tmp-file 'noquote))) 407 (org-babel-R-process-value-result 408 (org-babel-result-cond result-params 409 (with-temp-buffer 410 (insert-file-contents tmp-file) 411 (org-babel-chomp (buffer-string) "\n")) 412 (org-babel-import-elisp-from-file tmp-file '(16))) 413 column-names-p))) 414 (output (org-babel-eval org-babel-R-command body)))) 415 416 (defvar ess-eval-visibly-p) 417 418 (defun org-babel-R-evaluate-session 419 (session body result-type result-params column-names-p row-names-p) 420 "Evaluate BODY in SESSION. 421 If RESULT-TYPE equals `output' then return standard output as a 422 string. If RESULT-TYPE equals `value' then return the value of the 423 last statement in BODY, as elisp." 424 (cl-case result-type 425 (value 426 (with-temp-buffer 427 (insert (org-babel-chomp body)) 428 (let ((ess-local-process-name 429 (process-name (get-buffer-process session))) 430 (ess-eval-visibly-p nil)) 431 (ess-eval-buffer nil))) 432 (let ((tmp-file (org-babel-temp-file "R-"))) 433 (org-babel-comint-eval-invisibly-and-wait-for-file 434 session tmp-file 435 (format org-babel-R-write-object-command 436 (if row-names-p "TRUE" "FALSE") 437 (if column-names-p 438 (if row-names-p "NA" "TRUE") 439 "FALSE") 440 ".Last.value" (org-babel-process-file-name tmp-file 'noquote))) 441 (org-babel-R-process-value-result 442 (org-babel-result-cond result-params 443 (with-temp-buffer 444 (insert-file-contents tmp-file) 445 (org-babel-chomp (buffer-string) "\n")) 446 (org-babel-import-elisp-from-file tmp-file '(16))) 447 column-names-p))) 448 (output 449 (mapconcat 450 'org-babel-chomp 451 (butlast 452 (delq nil 453 (mapcar 454 (lambda (line) (when (> (length line) 0) line)) 455 (mapcar 456 (lambda (line) ;; cleanup extra prompts left in output 457 (if (string-match 458 "^\\([>+.]\\([ ][>.+]\\)*[ ]\\)" 459 (car (split-string line "\n"))) 460 (substring line (match-end 1)) 461 line)) 462 (with-current-buffer session 463 (let ((comint-prompt-regexp (concat "^" comint-prompt-regexp))) 464 (org-babel-comint-with-output (session org-babel-R-eoe-output) 465 (insert (mapconcat 'org-babel-chomp 466 (list body org-babel-R-eoe-indicator) 467 "\n")) 468 (inferior-ess-send-input)))))))) "\n")))) 469 470 (defun org-babel-R-process-value-result (result column-names-p) 471 "R-specific processing of return value. 472 Insert hline if column names in output have been requested." 473 (if column-names-p 474 (condition-case nil 475 (cons (car result) (cons 'hline (cdr result))) 476 (error "Could not parse R result")) 477 result)) 478 479 480 ;;; async evaluation 481 482 (defconst ob-session-async-R-indicator "'ob_comint_async_R_%s_%s'") 483 484 (defun ob-session-async-org-babel-R-evaluate-session 485 (session body result-type column-names-p row-names-p) 486 "Asynchronously evaluate BODY in SESSION. 487 Returns a placeholder string for insertion, to later be replaced 488 by `org-babel-comint-async-filter'." 489 (org-babel-comint-async-register 490 session (current-buffer) 491 "^\\(?:[>.+] \\)*\\[1\\] \"ob_comint_async_R_\\(.+?\\)_\\(.+\\)\"$" 492 'org-babel-chomp 493 'ob-session-async-R-value-callback) 494 (cl-case result-type 495 (value 496 (let ((tmp-file (org-babel-temp-file "R-"))) 497 (with-temp-buffer 498 (insert 499 (org-babel-chomp body)) 500 (let ((ess-local-process-name 501 (process-name (get-buffer-process session)))) 502 (ess-eval-buffer nil))) 503 (with-temp-buffer 504 (insert 505 (mapconcat 506 'org-babel-chomp 507 (list (format org-babel-R-write-object-command 508 (if row-names-p "TRUE" "FALSE") 509 (if column-names-p 510 (if row-names-p "NA" "TRUE") 511 "FALSE") 512 ".Last.value" 513 (org-babel-process-file-name tmp-file 'noquote)) 514 (format ob-session-async-R-indicator 515 "file" tmp-file)) 516 "\n")) 517 (let ((ess-local-process-name 518 (process-name (get-buffer-process session)))) 519 (ess-eval-buffer nil))) 520 tmp-file)) 521 (output 522 (let ((uuid (md5 (number-to-string (random 100000000)))) 523 (ess-local-process-name 524 (process-name (get-buffer-process session))) 525 (ess-eval-visibly-p nil)) 526 (with-temp-buffer 527 (insert (format ob-session-async-R-indicator 528 "start" uuid)) 529 (insert "\n") 530 (insert body) 531 (insert "\n") 532 (insert (format ob-session-async-R-indicator 533 "end" uuid)) 534 (ess-eval-buffer nil )) 535 uuid)))) 536 537 (defun ob-session-async-R-value-callback (params tmp-file) 538 "Callback for async value results. 539 Assigned locally to `org-babel-comint-async-file-callback' in R 540 comint buffers used for asynchronous Babel evaluation." 541 (let* ((graphics-file (and (member "graphics" (assq :result-params params)) 542 (org-babel-graphical-output-file params))) 543 (colnames-p (unless graphics-file (cdr (assq :colnames params))))) 544 (org-babel-R-process-value-result 545 (org-babel-result-cond (assq :result-params params) 546 (with-temp-buffer 547 (insert-file-contents tmp-file) 548 (org-babel-chomp (buffer-string) "\n")) 549 (org-babel-import-elisp-from-file tmp-file '(16))) 550 (or (equal "yes" colnames-p) 551 (org-babel-pick-name 552 (cdr (assq :colname-names params)) colnames-p))))) 553 554 555 556 ;;; ob-session-async-R.el ends here 557 558 559 (provide 'ob-R) 560 561 ;;; ob-R.el ends here