ob-R.el 20 KB

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