ob-R.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. ;;; ob-R.el --- org-babel functions for R code evaluation
  2. ;; Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Dan Davison
  5. ;; Keywords: literate programming, reproducible research, R, statistics
  6. ;; Homepage: http://orgmode.org
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org-Babel support for evaluating R code
  20. ;;; Code:
  21. (require 'ob)
  22. (eval-when-compile (require 'cl))
  23. (declare-function orgtbl-to-tsv "org-table" (table params))
  24. (declare-function R "ext:essd-r" (&optional start-args))
  25. (declare-function inferior-ess-send-input "ext:ess-inf" ())
  26. (declare-function ess-make-buffer-current "ext:ess-inf" ())
  27. (declare-function ess-eval-buffer "ext:ess-inf" (vis))
  28. (declare-function ess-wait-for-process "ext:ess-inf"
  29. (proc &optional sec-prompt wait force-redisplay))
  30. (declare-function org-number-sequence "org-compat" (from &optional to inc))
  31. (declare-function org-remove-if-not "org" (predicate seq))
  32. (declare-function org-every "org" (pred seq))
  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 (assoc :session (nth 2 info)))))
  80. (when (and session (string-match "^\\*\\(.+?\\)\\*$" session))
  81. (save-match-data (org-babel-R-initiate-session session nil)))))
  82. ;; The usage of utils::read.table() ensures that the command
  83. ;; read.table() can be found even in circumstances when the utils
  84. ;; package is not in the search path from R.
  85. (defconst ob-R-transfer-variable-table-with-header
  86. "%s <- local({
  87. con <- textConnection(
  88. %S
  89. )
  90. res <- utils::read.table(
  91. con,
  92. header = %s,
  93. row.names = %s,
  94. sep = \"\\t\",
  95. as.is = TRUE
  96. )
  97. close(con)
  98. res
  99. })"
  100. "R code used to transfer a table defined as a variable from org to R.
  101. This function is used when the table contains a header.")
  102. (defconst ob-R-transfer-variable-table-without-header
  103. "%s <- local({
  104. con <- textConnection(
  105. %S
  106. )
  107. res <- utils::read.table(
  108. con,
  109. header = %s,
  110. row.names = %s,
  111. sep = \"\\t\",
  112. as.is = TRUE,
  113. fill = TRUE,
  114. col.names = paste(\"V\", seq_len(%d), sep =\"\")
  115. )
  116. close(con)
  117. res
  118. })"
  119. "R code used to transfer a table defined as a variable from org to R.
  120. This function is used when the table does not contain a header.")
  121. (defun org-babel-expand-body:R (body params &optional graphics-file)
  122. "Expand BODY according to PARAMS, return the expanded body."
  123. (mapconcat 'identity
  124. (append
  125. (when (cdr (assoc :prologue params))
  126. (list (cdr (assoc :prologue params))))
  127. (org-babel-variable-assignments:R params)
  128. (list body)
  129. (when (cdr (assoc :epilogue params))
  130. (list (cdr (assoc :epilogue params)))))
  131. "\n"))
  132. (defun org-babel-execute:R (body params)
  133. "Execute a block of R code.
  134. This function is called by `org-babel-execute-src-block'."
  135. (save-excursion
  136. (let* ((result-params (cdr (assoc :result-params params)))
  137. (result-type (cdr (assoc :result-type params)))
  138. (session (org-babel-R-initiate-session
  139. (cdr (assoc :session params)) params))
  140. (colnames-p (cdr (assoc :colnames params)))
  141. (rownames-p (cdr (assoc :rownames params)))
  142. (graphics-file (and (member "graphics" (assq :result-params params))
  143. (org-babel-graphical-output-file params)))
  144. (full-body
  145. (let ((inside
  146. (list (org-babel-expand-body:R body params graphics-file))))
  147. (mapconcat 'identity
  148. (if graphics-file
  149. (append
  150. (list (org-babel-R-construct-graphics-device-call
  151. graphics-file params))
  152. inside
  153. (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()"))
  154. inside)
  155. "\n")))
  156. (result
  157. (org-babel-R-evaluate
  158. session full-body result-type result-params
  159. (or (equal "yes" colnames-p)
  160. (org-babel-pick-name
  161. (cdr (assoc :colname-names params)) colnames-p))
  162. (or (equal "yes" rownames-p)
  163. (org-babel-pick-name
  164. (cdr (assoc :rowname-names params)) rownames-p)))))
  165. (if graphics-file nil result))))
  166. (defun org-babel-prep-session:R (session params)
  167. "Prepare SESSION according to the header arguments specified in PARAMS."
  168. (let* ((session (org-babel-R-initiate-session session params))
  169. (var-lines (org-babel-variable-assignments:R params)))
  170. (org-babel-comint-in-buffer session
  171. (mapc (lambda (var)
  172. (end-of-line 1) (insert var) (comint-send-input nil t)
  173. (org-babel-comint-wait-for-output session)) var-lines))
  174. session))
  175. (defun org-babel-load-session:R (session body params)
  176. "Load BODY into SESSION."
  177. (save-window-excursion
  178. (let ((buffer (org-babel-prep-session:R session params)))
  179. (with-current-buffer buffer
  180. (goto-char (process-mark (get-buffer-process (current-buffer))))
  181. (insert (org-babel-chomp body)))
  182. buffer)))
  183. ;; helper functions
  184. (defun org-babel-variable-assignments:R (params)
  185. "Return list of R statements assigning the block's variables."
  186. (let ((vars (mapcar 'cdr (org-babel-get-header params :var))))
  187. (mapcar
  188. (lambda (pair)
  189. (org-babel-R-assign-elisp
  190. (car pair) (cdr pair)
  191. (equal "yes" (cdr (assoc :colnames params)))
  192. (equal "yes" (cdr (assoc :rownames params)))))
  193. (mapcar
  194. (lambda (i)
  195. (cons (car (nth i vars))
  196. (org-babel-reassemble-table
  197. (cdr (nth i vars))
  198. (cdr (nth i (cdr (assoc :colname-names params))))
  199. (cdr (nth i (cdr (assoc :rowname-names params)))))))
  200. (org-number-sequence 0 (1- (length vars)))))))
  201. (defun org-babel-R-quote-tsv-field (s)
  202. "Quote field S for export to R."
  203. (if (stringp s)
  204. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  205. (format "%S" s)))
  206. (defun org-babel-R-assign-elisp (name value colnames-p rownames-p)
  207. "Construct R code assigning the elisp VALUE to a variable named NAME."
  208. (if (listp value)
  209. (let* ((lengths (mapcar 'length (org-remove-if-not 'sequencep value)))
  210. (max (if lengths (apply 'max lengths) 0))
  211. (min (if lengths (apply 'min lengths) 0)))
  212. ;; Ensure VALUE has an orgtbl structure (depth of at least 2).
  213. (unless (listp (car value)) (setq value (list value)))
  214. (let ((file (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)))
  215. (header (if (or (eq (nth 1 value) 'hline) colnames-p)
  216. "TRUE" "FALSE"))
  217. (row-names (if rownames-p "1" "NULL")))
  218. (if (= max min)
  219. (format ob-R-transfer-variable-table-with-header
  220. name file header row-names)
  221. (format ob-R-transfer-variable-table-without-header
  222. name file header row-names max))))
  223. (cond ((integerp value) (format "%s <- %s" name (concat (number-to-string value) "L")))
  224. ((floatp value) (format "%s <- %s" name value))
  225. ((stringp value) (format "%s <- %S" name (org-no-properties value)))
  226. (t (format "%s <- %S" name (prin1-to-string value))))))
  227. (defvar ess-ask-for-ess-directory) ; dynamically scoped
  228. (defun org-babel-R-initiate-session (session params)
  229. "If there is not a current R process then create one."
  230. (unless (string= session "none")
  231. (let ((session (or session "*R*"))
  232. (ess-ask-for-ess-directory
  233. (and (boundp 'ess-ask-for-ess-directory)
  234. ess-ask-for-ess-directory
  235. (not (cdr (assoc :dir params))))))
  236. (if (org-babel-comint-buffer-livep session)
  237. session
  238. (save-window-excursion
  239. (when (get-buffer session)
  240. ;; Session buffer exists, but with dead process
  241. (set-buffer session))
  242. (require 'ess) (R)
  243. (let ((R-proc (get-process (or ess-local-process-name
  244. ess-current-process-name))))
  245. (while (process-get R-proc 'callbacks)
  246. (ess-wait-for-process R-proc)))
  247. (rename-buffer
  248. (if (bufferp session)
  249. (buffer-name session)
  250. (if (stringp session)
  251. session
  252. (buffer-name))))
  253. (current-buffer))))))
  254. (defun org-babel-R-associate-session (session)
  255. "Associate R code buffer with an R session.
  256. Make SESSION be the inferior ESS process associated with the
  257. current code buffer."
  258. (setq ess-local-process-name
  259. (process-name (get-buffer-process session)))
  260. (ess-make-buffer-current))
  261. (defvar org-babel-R-graphics-devices
  262. '((:bmp "bmp" "filename")
  263. (:jpg "jpeg" "filename")
  264. (:jpeg "jpeg" "filename")
  265. (:tikz "tikz" "file")
  266. (:tiff "tiff" "filename")
  267. (:png "png" "filename")
  268. (:svg "svg" "file")
  269. (:pdf "pdf" "file")
  270. (:ps "postscript" "file")
  271. (:postscript "postscript" "file"))
  272. "An alist mapping graphics file types to R functions.
  273. Each member of this list is a list with three members:
  274. 1. the file extension of the graphics file, as an elisp :keyword
  275. 2. the R graphics device function to call to generate such a file
  276. 3. the name of the argument to this function which specifies the
  277. file to write to (typically \"file\" or \"filename\")")
  278. (defun org-babel-R-construct-graphics-device-call (out-file params)
  279. "Construct the call to the graphics device."
  280. (let* ((allowed-args '(:width :height :bg :units :pointsize
  281. :antialias :quality :compression :res
  282. :type :family :title :fonts :version
  283. :paper :encoding :pagecentre :colormodel
  284. :useDingbats :horizontal))
  285. (device (and (string-match ".+\\.\\([^.]+\\)" out-file)
  286. (match-string 1 out-file)))
  287. (device-info (or (assq (intern (concat ":" device))
  288. org-babel-R-graphics-devices)
  289. (assq :png org-babel-R-graphics-devices)))
  290. (extra-args (cdr (assq :R-dev-args params))) filearg args)
  291. (setq device (nth 1 device-info))
  292. (setq filearg (nth 2 device-info))
  293. (setq args (mapconcat
  294. (lambda (pair)
  295. (if (member (car pair) allowed-args)
  296. (format ",%s=%S"
  297. (substring (symbol-name (car pair)) 1)
  298. (cdr pair)) ""))
  299. params ""))
  300. (format "%s(%s=\"%s\"%s%s%s); tryCatch({"
  301. device filearg out-file args
  302. (if extra-args "," "") (or extra-args ""))))
  303. (defconst org-babel-R-eoe-indicator "'org_babel_R_eoe'")
  304. (defconst org-babel-R-eoe-output "[1] \"org_babel_R_eoe\"")
  305. (defconst org-babel-R-write-object-command "{
  306. function(object,transfer.file) {
  307. object
  308. invisible(
  309. if (
  310. inherits(
  311. try(
  312. {
  313. tfile<-tempfile()
  314. write.table(object, file=tfile, sep=\"\\t\",
  315. na=\"nil\",row.names=%s,col.names=%s,
  316. quote=FALSE)
  317. file.rename(tfile,transfer.file)
  318. },
  319. silent=TRUE),
  320. \"try-error\"))
  321. {
  322. if(!file.exists(transfer.file))
  323. file.create(transfer.file)
  324. }
  325. )
  326. }
  327. }(object=%s,transfer.file=\"%s\")"
  328. "A template for an R command to evaluate a block of code and write the result to a file.
  329. Has four %s escapes to be filled in:
  330. 1. Row names, \"TRUE\" or \"FALSE\"
  331. 2. Column names, \"TRUE\" or \"FALSE\"
  332. 3. The code to be run (must be an expression, not a statement)
  333. 4. The name of the file to write to")
  334. (defun org-babel-R-evaluate
  335. (session body result-type result-params column-names-p row-names-p)
  336. "Evaluate R code in BODY."
  337. (if session
  338. (org-babel-R-evaluate-session
  339. session body result-type result-params column-names-p row-names-p)
  340. (org-babel-R-evaluate-external-process
  341. body result-type result-params column-names-p row-names-p)))
  342. (defun org-babel-R-evaluate-external-process
  343. (body result-type result-params column-names-p row-names-p)
  344. "Evaluate BODY in external R process.
  345. If RESULT-TYPE equals 'output then return standard output as a
  346. string. If RESULT-TYPE equals 'value then return the value of the
  347. last statement in BODY, as elisp."
  348. (case result-type
  349. (value
  350. (let ((tmp-file (org-babel-temp-file "R-")))
  351. (org-babel-eval org-babel-R-command
  352. (format org-babel-R-write-object-command
  353. (if row-names-p "TRUE" "FALSE")
  354. (if column-names-p
  355. (if row-names-p "NA" "TRUE")
  356. "FALSE")
  357. (format "{function ()\n{\n%s\n}}()" body)
  358. (org-babel-process-file-name tmp-file 'noquote)))
  359. (org-babel-R-process-value-result
  360. (org-babel-result-cond result-params
  361. (with-temp-buffer
  362. (insert-file-contents tmp-file)
  363. (buffer-string))
  364. (org-babel-import-elisp-from-file tmp-file '(16)))
  365. column-names-p)))
  366. (output (org-babel-eval org-babel-R-command body))))
  367. (defvar ess-eval-visibly-p)
  368. (defun org-babel-R-evaluate-session
  369. (session body result-type result-params column-names-p row-names-p)
  370. "Evaluate BODY in SESSION.
  371. If RESULT-TYPE equals 'output then return standard output as a
  372. string. If RESULT-TYPE equals 'value then return the value of the
  373. last statement in BODY, as elisp."
  374. (case result-type
  375. (value
  376. (with-temp-buffer
  377. (insert (org-babel-chomp body))
  378. (let ((ess-local-process-name
  379. (process-name (get-buffer-process session)))
  380. (ess-eval-visibly-p nil))
  381. (ess-eval-buffer nil)))
  382. (let ((tmp-file (org-babel-temp-file "R-")))
  383. (org-babel-comint-eval-invisibly-and-wait-for-file
  384. session tmp-file
  385. (format org-babel-R-write-object-command
  386. (if row-names-p "TRUE" "FALSE")
  387. (if column-names-p
  388. (if row-names-p "NA" "TRUE")
  389. "FALSE")
  390. ".Last.value" (org-babel-process-file-name tmp-file 'noquote)))
  391. (org-babel-R-process-value-result
  392. (org-babel-result-cond result-params
  393. (with-temp-buffer
  394. (insert-file-contents tmp-file)
  395. (buffer-string))
  396. (org-babel-import-elisp-from-file tmp-file '(16)))
  397. column-names-p)))
  398. (output
  399. (mapconcat
  400. 'org-babel-chomp
  401. (butlast
  402. (delq nil
  403. (mapcar
  404. (lambda (line) (when (> (length line) 0) line))
  405. (mapcar
  406. (lambda (line) ;; cleanup extra prompts left in output
  407. (if (string-match
  408. "^\\([ ]*[>+\\.][ ]?\\)+\\([[0-9]+\\|[ ]\\)" line)
  409. (substring line (match-end 1))
  410. line))
  411. (org-babel-comint-with-output (session org-babel-R-eoe-output)
  412. (insert (mapconcat 'org-babel-chomp
  413. (list body org-babel-R-eoe-indicator)
  414. "\n"))
  415. (inferior-ess-send-input)))))) "\n"))))
  416. (defun org-babel-R-process-value-result (result column-names-p)
  417. "R-specific processing of return value.
  418. Insert hline if column names in output have been requested."
  419. (if column-names-p
  420. (cons (car result) (cons 'hline (cdr result)))
  421. result))
  422. (provide 'ob-R)
  423. ;;; ob-R.el ends here