org-R.el 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. ;;; org-R.el --- Computing and data visualisation in Org-mode using R
  2. ;; Copyright (C) 2009
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Dan Davison <davison@stats.ox.ac.uk>
  5. ;; Keywords: org, R, ESS, tables, graphics
  6. ;; Homepage: http://www.stats.ox.ac.uk/~davison/software/org-R
  7. ;; Version: 0.06 2009-04-15
  8. ;;
  9. ;; This file is not part of GNU Emacs.
  10. ;;
  11. ;; This file is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 3, or (at your option)
  14. ;; any later version.
  15. ;; This file is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;; This file allows R (http://www.r-project.org) code to be applied to
  25. ;; emacs org-mode (http://orgmode.org) tables. When the result of the
  26. ;; analysis is a vector or matrix, it is output back into the org-mode
  27. ;; buffer as a new org table. Alternatively the R code may be used to
  28. ;; plot the data in the org table. It requires R to be running in an
  29. ;; inferior-ess-mode buffer (install Emacs Speaks Statistics
  30. ;; http://ess.r-project.org and issue M-x R).
  31. ;;
  32. ;;
  33. ;; The user interface is via two different options lines in the org
  34. ;; buffer. As is conventional in org-mode, these are lines starting
  35. ;; with `#+'. Lines starting with #+R: specify options in the
  36. ;; standard org style (option:value) and are used to specify certain
  37. ;; off-the-shelf transformations and plots of the table data. The
  38. ;; #+R: line is also used to specify the data to be analysed
  39. ;; (either an org table or a csv file), and to restrict the analysis
  40. ;; to certain columns etc. In lines starting #+RR: you can supply
  41. ;; literal R code, giving you full control over what you do with the
  42. ;; table. With point in the first #+R line, M-x org-R-apply
  43. ;; makes happen whatever has been specified in those lines.
  44. ;; The documentation is currently the Worg tutorial:
  45. ;;
  46. ;; http://orgmode.org/worg/org-tutorials/org-R/org-R.php
  47. ;;
  48. ;; changelog:
  49. ;; 2009-04-05 two bug fixes in org-R-eval contributed by David Moffat
  50. ;;
  51. (defconst org-R-skeleton-funcall-1-arg
  52. "%s(x[%s]%s)"
  53. "Skeleton of a call to an R function.
  54. E.g. barplot(x[,3:5], names.arg=rownames(x))")
  55. (defconst org-R-skeleton-funcall-2-args
  56. "%s(x[,%s], x[,%s]%s)"
  57. "Skeleton of a call to an R function which can take x and y
  58. args.")
  59. (defconst org-R-write-org-table-def
  60. "write.org.table <- function (x, write.rownames = TRUE)
  61. {
  62. if(!is.null(dim(x)) && length(dim(x)) > 2)
  63. stop(\"Object must be 1- or 2-dimensional\") ;
  64. if(is.vector(x) || is.table(x) || is.factor(x) || is.array(x))
  65. x <- as.matrix(x) ;
  66. if(!(is.matrix(x) || inherits(x, c('matrix', 'data.frame')))) {
  67. invisible() ;
  68. print(x) ;
  69. stop(\"Object not recognised as 1- or 2-dimensional\") ;
  70. } ;
  71. if(is.null(colnames(x)))
  72. colnames(x) <- rep('', ncol(x)) ;
  73. if(write.rownames)
  74. x <- cbind(rownames(x), x) ;
  75. cat('|', paste(colnames(x), collapse = ' | '), '|\\n') ;
  76. cat('|', paste(rep('----', ncol(x)), collapse = '+'), '|\\n', sep = '') ;
  77. invisible(apply(x, 1, function(row) cat('|', paste(row, collapse = ' | '), '|\\n'))) ;
  78. }"
  79. "Definition of R function to write org table representation of R objects.
  80. To see a more human-readable version of this, look at the code,
  81. or type dput(write.org.table) RET at the R (inferior-ess-mode
  82. buffer) prompt.")
  83. (defun org-R-apply-maybe ()
  84. (if (save-excursion
  85. (beginning-of-line 1)
  86. (looking-at "#\\+RR?:"))
  87. (progn (call-interactively 'org-R-apply)
  88. t) ;; to signal that we took action
  89. nil)) ;; to signal that we did not
  90. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-R-apply-maybe)
  91. (defun org-R-apply ()
  92. "Construct and evaluate an R function call.
  93. Construct an R function corresponding to the #+R: and #+RR:
  94. lines. R must be currently running in an inferior-ess-mode
  95. buffer. The function evaluates any user-supplied R code in the
  96. #+RR: line before the off-the-shelf actions specified in the #+R:
  97. line. The user-supplied R code can operate on a variable called x
  98. that is the org table represented as a data frame in R. Text
  99. output from the R process may be inserted into the org buffer, as
  100. an org table where appropriate."
  101. (interactive)
  102. (require 'ess)
  103. (save-excursion
  104. (beginning-of-line)
  105. (unless (looking-at "#\\+RR?:") (error "Point must be in a #+R or #+RR line"))
  106. (while (looking-at "#\\+RR?:") (forward-line -1))
  107. (forward-line)
  108. ;; For the rest of the code in this file we are based at the
  109. ;; beginning of the first #+R line
  110. ;; FIXME: if point is at the beginning of the #+RR? lines when
  111. ;; this function is called, then tabular output gets inserted,
  112. ;; leaving point up at the top of the tabular output.
  113. (let* ((options (org-R-get-options))
  114. (code (org-R-construct-code options))
  115. (infile (plist-get options :infile))
  116. (ext (if infile (file-name-extension infile)))
  117. csv-file)
  118. (if (string-equal ext "csv")
  119. (setq csv-file infile)
  120. (setq csv-file
  121. (org-R-export-to-csv
  122. (make-temp-file "org-R-tmp" nil ".csv") options)))
  123. (org-R-eval code csv-file options)
  124. (delete-other-windows) ;; FIXME
  125. (if (plist-get options :showcode) (org-R-showcode code)))))
  126. (defun org-R-apply-throughout-subtree ()
  127. "Call org-R-apply in every org-R block in current subtree."
  128. ;; This currently relies on re-search-forward leaving point after
  129. ;; the #+RR?: If point were at the beginning of the line, then
  130. ;; tabular input would get inserted leaving point above the #+RR?:,
  131. ;; and this would loop infinitely. Same for org-R-apply-to-buffer.
  132. (interactive)
  133. (save-excursion
  134. (org-back-to-heading)
  135. (while (re-search-forward
  136. "^#\\+RR?:"
  137. (save-excursion (org-end-of-subtree)) t)
  138. (org-R-apply)
  139. (forward-line)
  140. (while (looking-at "#\\+RR?")
  141. (forward-line)))))
  142. (defun org-R-apply-throughout-buffer ()
  143. "Call org-R-apply in every org-R block in the buffer."
  144. (interactive)
  145. (save-excursion
  146. (goto-char (point-min))
  147. (while (re-search-forward "^#\\+RR?:" nil t)
  148. (org-R-apply)
  149. (forward-line)
  150. (while (looking-at "#\\+RR?")
  151. (forward-line)))))
  152. (defun org-R-construct-code (options)
  153. "Construct the R function that implements the requested
  154. behaviour.
  155. The body of this function derives from two sources:
  156. 1. Explicit R code which is read from lines starting with
  157. #+RR: by org-R-get-user-code, and
  158. 2. Off-the-shelf code corresponding to options specified in the
  159. #+R: line. This code is constructed by
  160. org-R-off-the-shelf-code."
  161. (let ((user-code (org-R-get-user-code))
  162. (action (plist-get options :action)))
  163. (if (or (eq action 'tabulate) (eq action 'transpose))
  164. (setq options (plist-put options :output-to-buffer t)))
  165. (format "function(x){%sx}"
  166. (concat
  167. (when user-code (concat user-code ";"))
  168. (when action (concat (org-R-off-the-shelf-code options) ";"))))))
  169. (defun org-R-get-user-code (&optional R)
  170. "Read user-supplied R code from #+RR: lines."
  171. (let ((case-fold-search t))
  172. (save-excursion
  173. (while (looking-at "^#\\+\\(RR?:\\) *\\(.*\\)")
  174. (if (string= "RR:" (match-string 1))
  175. (setq R (concat R (when R ";") (match-string 2))))
  176. (forward-line))))
  177. R)
  178. (defun org-R-off-the-shelf-code (options)
  179. "Return R code implementing the actions requested in the
  180. #+R: lines."
  181. ;; This is a somewhat long function as it deals with several
  182. ;; different cases, corresponding to all the off-the-shelf actions
  183. ;; that have been implemented.
  184. (let* ((action (plist-get options :action))
  185. (cols (plist-get options :columns))
  186. (ncols (org-R-number-of-columns cols))
  187. (nxcols (nth 0 ncols))
  188. (nycols (nth 1 ncols))
  189. (cols-R (org-R-make-index-vectors cols))
  190. (xcols-R (nth 0 cols-R))
  191. (ycols-R (nth 1 cols-R))
  192. seq args largs extra-code title colour matrix-index)
  193. ;; I want this to affect options outside this function. Will it
  194. ;; necessarily do so? (not if plist-put adds to head of the
  195. ;; plist?)
  196. (setq options (plist-put options :nxcols nxcols))
  197. (cond ((eq action 'points)
  198. (setq action 'plot)
  199. (setq options (plist-put options :lines nil)))
  200. ((eq action 'lines)
  201. (setq action 'plot)
  202. (setq options (plist-put options :lines t))))
  203. (if (and (setq title (plist-get options :title)) (symbolp title))
  204. (setq title symbol-name title))
  205. (setq args (plist-put args :main (concat "\"" title "\"")))
  206. (if (setq colour (or (plist-get options :colour)
  207. (plist-get options :color)
  208. (plist-get options :col)))
  209. (setq args
  210. (plist-put args :col
  211. (concat "\"" (if (symbolp colour) (symbol-name colour) colour) "\""))))
  212. (setq largs
  213. (if (setq legend (plist-get options :legend))
  214. (plist-put largs :x
  215. (concat "\"" (if (symbolp legend) (symbol-name legend) legend) "\""))
  216. (plist-put largs :x "\"topright\"")))
  217. (cond
  218. ((null ycols-R)
  219. ;; single set of columns; implicit x values
  220. (if (null xcols-R)
  221. (setq xcols-R "" matrix-index "")
  222. (setq matrix-index (concat "," xcols-R)))
  223. (cond
  224. ;;----------------------------------------------------------------------
  225. ((eq action 'barplot)
  226. (if (eq nxcols 1)
  227. (progn
  228. (setq args (plist-put args :names.arg "rownames(x)"))
  229. (setq args (org-R-set-user-supplied-args args (plist-get options :args)))
  230. (format org-R-skeleton-funcall-1-arg
  231. "barplot" xcols-R
  232. (concat ", " (org-R-plist-to-R-args args))))
  233. (setq args (plist-put args :names.arg "colnames(x)"))
  234. (setq args (plist-put args :col "seq(nrow(x))"))
  235. (setq args (plist-put args :beside "TRUE"))
  236. (setq largs (plist-put largs :bty "\"n\""))
  237. ;; (setq largs (plist-put largs :lwd 10))
  238. (setq largs (plist-put largs :col "seq(nrow(x))"))
  239. (setq largs (plist-put largs :legend "rownames(x)"))
  240. (setq args (org-R-set-user-supplied-args args (plist-get options :args)))
  241. (concat (format org-R-skeleton-funcall-1-arg
  242. "barplot(as.matrix" matrix-index
  243. (concat "), " (org-R-plist-to-R-args args)))
  244. "; legend(" (org-R-plist-to-R-args largs) ")")))
  245. ;;----------------------------------------------------------------------
  246. ((eq action 'density)
  247. (if (and nxcols (> nxcols 1))
  248. (error "Multiple columns not implemented for action:%s" action))
  249. (setq args (plist-put args :xlab (concat "colnames(x)["xcols-R"]")))
  250. (setq args (org-R-set-user-supplied-args args (plist-get options :args)))
  251. (format org-R-skeleton-funcall-1-arg
  252. "plot(density" matrix-index
  253. (concat "), " (org-R-plist-to-R-args args))))
  254. ;;----------------------------------------------------------------------
  255. ((eq action 'hist)
  256. (if (and nxcols (> nxcols 1))
  257. (error "Multiple columns not implemented for action:%s" action))
  258. (setq args (plist-put args :xlab (concat "colnames(x)["xcols-R"]")))
  259. (setq args (org-R-set-user-supplied-args args (plist-get options :args)))
  260. (setq args (concat ", " (org-R-plist-to-R-args args)))
  261. (format org-R-skeleton-funcall-1-arg "hist" matrix-index args))
  262. ;;----------------------------------------------------------------------
  263. ((eq action 'image)
  264. (format org-R-skeleton-funcall-1-arg "image(as.matrix" matrix-index ")"))
  265. ;;----------------------------------------------------------------------
  266. ((eq action 'plot)
  267. (setq seq (concat "seq_along("xcols-R")"))
  268. (setq args (plist-put args :type (if (plist-get options :lines) "\"l\"" "\"p\"")))
  269. (setq args (plist-put args :ylab (concat "colnames(x)["xcols-R"]")))
  270. (setq args (concat ", " (org-R-plist-to-R-args args)))
  271. (concat (format org-R-skeleton-funcall-1-arg
  272. (if (eq nxcols 1) "plot" "matplot") matrix-index args)
  273. extra-code))
  274. ;;----------------------------------------------------------------------
  275. ((eq action 'tabulate)
  276. (concat
  277. (if (plist-get options :sort)
  278. (format org-R-skeleton-funcall-1-arg
  279. "x <- sort(table" xcols-R "), decreasing=TRUE")
  280. (format org-R-skeleton-funcall-1-arg "x <- table" matrix-index ""))
  281. (if (eq nxcols 1) "; x <- data.frame(value=names(x), count=x[])")))
  282. ;;----------------------------------------------------------------------
  283. ((eq action 'transpose)
  284. (format org-R-skeleton-funcall-1-arg "x <- t" matrix-index ""))
  285. ;;----------------------------------------------------------------------
  286. ;; Don't recognise action: option, try applying it as the name of an R function.
  287. (t (format org-R-skeleton-funcall-1-arg
  288. (concat "x <- " (symbol-name action)) matrix-index ""))))
  289. ;;----------------------------------------------------------------------
  290. (ycols-R
  291. ;; x and y columns specified
  292. (cond
  293. ;;----------------------------------------------------------------------
  294. ((eq action 'plot)
  295. (unless (eq nxcols 1) (error "Multiple x-columns not implemented for action:plot"))
  296. (setq args
  297. (plist-put
  298. args :ylab
  299. (concat "if(length("ycols-R") == 1) colnames(x)["ycols-R"] else ''")))
  300. (setq args (plist-put args :xlab (concat "colnames(x)["xcols-R"]")))
  301. (setq args (plist-put args :type (if (plist-get options :lines) "\"l\"" "\"p\"")))
  302. (setq args (concat ", " (org-R-plist-to-R-args args)))
  303. (setq seq (concat "seq_along("ycols-R")"))
  304. (setq largs (plist-put largs :col seq))
  305. (setq largs (plist-put largs :lty seq))
  306. (setq largs (plist-put largs :bty "\"n\""))
  307. (setq largs (plist-put largs :legend (concat "colnames(x)["ycols-R"]")))
  308. (setq extra-code
  309. (concat "; "
  310. "if(length("ycols-R") > 1) "
  311. "legend(" (org-R-plist-to-R-args largs) ")"))
  312. (concat (format org-R-skeleton-funcall-2-args
  313. (if (and (eq nxcols 1) (eq nycols 1)) "plot" "matplot")
  314. xcols-R ycols-R args)
  315. extra-code))
  316. ;;----------------------------------------------------------------------
  317. (t (error "action:%s requires a single set of columns" (symbol-name action))))))))
  318. (defun org-R-set-user-supplied-args (args user-args)
  319. "Set user-supplied values in arguments plist."
  320. (while (setq prop (pop user-args))
  321. (setq args (plist-put args prop (pop user-args))))
  322. args)
  323. (defun org-R-plist-to-R-args (plist)
  324. "Convert a plist into a string of R arguments."
  325. (let (arg-string arg)
  326. (while (setq arg (pop plist))
  327. (string-match ":\\(\.*\\)" (symbol-name arg))
  328. (setq arg (match-string 1 (symbol-name arg)))
  329. (setq arg-string
  330. (concat
  331. (if arg-string (concat arg-string ", "))
  332. (format "%s=%s" arg (pop plist)))))
  333. arg-string))
  334. (defun org-R-alist-to-R-args (alist)
  335. "Convert an alist of (argument . val) pairs into a string of R arguments.
  336. The alist is something like
  337. '((arg1 . 1)
  338. (arg2 . a))
  339. This isn't used, but it seems much nicer than
  340. my plist equivalent. Is there a better way to write the plist
  341. version?
  342. "
  343. (mapconcat
  344. 'identity
  345. (mapcar (lambda(pair) (format "%s = %s" (car pair) (cdr pair))) alist)
  346. ", "))
  347. (defun org-R-make-index-vectors (cols)
  348. "Construct R indexing vectors as strings from lisp form.
  349. COLS is the lisp form given by the `columns:' option. It may
  350. take the following forms:
  351. 1. integer atom - the number of the column
  352. 2. symbol/string atom - the name of the column
  353. 3. list of length 1 - same as 1 or 2 above
  354. 4. list of length > 1 - specification of multiple columns as 1 or 2 above, unless it is
  355. 5. list of 2 lists - each list specifies (possibly multiple) columns
  356. In cases 1-4 this function returns a list of length 1, containing
  357. the R index vector as a string. In case 5 this function returns a
  358. list of two such index vectors.
  359. In cases 1 - 4, when a bivariate plot is requested such as by
  360. `action:lines', the x values are implicit, i.e
  361. 1,2,...,number-of-rows.
  362. In case 4, an attempt is made to do something sensible with the
  363. multiple columns, e.g. for `action:lines' they will be plotted
  364. together on the same graph against the implicit x values, and for
  365. `action:barplot' the bars corresponding to a single row will be
  366. stacked on top of each other, or placed side by side, depending
  367. on the value of the `beside' option.
  368. For `action:tabulate', if 2 columns are selected, a
  369. two-dimensional table is created. If more than 2, then the
  370. appropriately dimensioned table is computed and inserted using
  371. the standard text representation of multi-dimensional arrays used
  372. by R (as org does not currently have tables of dimension > 2).
  373. The straightforward case of case 5 is that both lists are of
  374. length 1. For `action:plot' and `action:lines' these specify the
  375. y and x coordinates of the points to be plotted or joined by
  376. lines.
  377. The intention is that `org-R-apply' does something
  378. corresponding to what would happen if you did the following in R:
  379. fun(x=tab[,xcols], y=tab[,ycols])
  380. where fun is the R function implementing the desired
  381. action (plotting/computation), tab is the org table, xcols are
  382. the columns specified in cases 1-4 above, and ycols are the
  383. second set of columns which might have been specified under case
  384. 5 above. For relevant R documentation see the help page
  385. associated with the function xy.coords, e.g. by typing ?xy.coords
  386. at the R prompt.
  387. The following won't work with case 5: `tabulate'
  388. "
  389. (defun org-R-make-index-vector (cols)
  390. "Return the R indexing vector (as a string) corresponding to
  391. the lisp form COLS. In this function, COLS is a either a list of
  392. atoms, or an atom, i.e. in the form of cases 1-4"
  393. (when cols
  394. (let (to-stringf)
  395. (unless (listp cols) (setq cols (list cols)))
  396. (setq to-stringf
  397. (cond ((car (mapcar 'symbolp cols))
  398. (lambda (symbol) (concat "\"" (symbol-name symbol) "\"")))
  399. ((car (mapcar 'integerp cols))
  400. 'int-to-string)
  401. ((car (mapcar 'stringp cols))
  402. (lambda (string) (concat "\"" string "\"")))
  403. (t (error "Column selection should be symbol, integer or string: %S" cols))))
  404. (concat (when (> (length cols) 1) "c(")
  405. (mapconcat to-stringf cols ",")
  406. (when (> (length cols) 1) ")")))))
  407. (if (and (listp cols) (listp (car cols)))
  408. (mapcar 'org-R-make-index-vector cols) ;; case 5
  409. (list (org-R-make-index-vector cols)))) ;; other cases
  410. (defun org-R-number-of-columns (cols)
  411. (defun f (c) (if (listp c) (length c) 1))
  412. (if (and (listp cols) (listp (car cols)))
  413. (mapcar 'f cols)
  414. (list (f cols))))
  415. (defun org-R-eval (R-function csv-file options)
  416. "Apply an R function to tabular data and receive output as an org table.
  417. R-FUNCTION is a string; it may be simply the name of an
  418. appropriate R function (e.g. \"summary\", \"plot\"), or a
  419. user-defined anonymous function of the form
  420. \"(function(data.frame) {...})\". It will receive as its first
  421. argument the org table as an R 'data frame' -- a table-like
  422. structure which can have columns containing different types of
  423. data -- numeric, character etc.
  424. The R function may produce graphical and/or text output. If it
  425. produces text output, and the replace:t is specified, and if
  426. there is a table immediately above the #+R lines, then it is
  427. replaced by the text output. Otherwise the text output is
  428. inserted above the #+R lines.
  429. "
  430. (let ((transit-buffer "org-R-transit")
  431. (infile (plist-get options :infile))
  432. (output-file (plist-get options :outfile))
  433. (title (plist-get options :title))
  434. output-format graphics-output-file width height)
  435. (unless (not output-file)
  436. ;; We are writing output to file. Determine file format and
  437. ;; location, and open graphics device if necessary.
  438. (if (string-match
  439. "\\(.*\.\\)?\\(org\\|png\\|jpg\\|jpeg\\|pdf\\|ps\\|bmp\\|tiff\\)$"
  440. output-file)
  441. (setq output-format (match-string 2 output-file))
  442. (error "Did not recognise file name suffix %s as available output format"
  443. (match-string 2 output-file)))
  444. (unless (match-string 1 output-file)
  445. ;; only suffix provided: store in org-attach dir
  446. (require 'org-attach)
  447. (let ((temporary-file-directory (org-attach-dir t)))
  448. (setq output-file
  449. (make-temp-file
  450. "org-R-output-" nil (concat "." output-format)))))
  451. ;;; MdQ bug fix.
  452. ;;; If a filename is given, make sure it's absolute,
  453. ;;; as ess-execute needs that later.
  454. (if (match-string 1 output-file)
  455. (setq output-file (expand-file-name output-file)) )
  456. (if (eq output-format "jpg") (setq output-format "jpeg"))
  457. (setq graphics-output-file (not (string-equal output-format "org")))
  458. (if graphics-output-file ;; open the graphics device
  459. (ess-execute
  460. (concat output-format "(file=\"" output-file "\""
  461. (if (setq width (plist-get options :width))
  462. (format ", width=%d" width))
  463. (if (setq height (plist-get options :height))
  464. (format ", height=%d" height)) ")"))))
  465. ;; Apply R code to table (which is now stored as a csv file)
  466. ;; does it matter whether this uses ess-command or ess-execute?
  467. ;; First evaluate function definition for R -> org table conversion
  468. ;;; MdQ bug fix.
  469. ;;; The following save-excursion has been brought up to here
  470. ;;; so that the two ess-execute commands are now within it.
  471. ;;; This is because they have the side effect of changing current
  472. ;;; buffer to the transit-buffer, which causes error of deleting
  473. ;;; the wrong table there, instead of in the org buffer.
  474. (save-excursion
  475. (ess-execute (replace-regexp-in-string "\n" " " org-R-write-org-table-def)
  476. nil transit-buffer)
  477. ;; FIXME: why not eval the function def together with the function call
  478. ;; as in the commented out line below (it didn't work for some reason)
  479. (ess-execute
  480. (concat
  481. ;; (replace-regexp-in-string "\n" " " org-R-write-org-table-def) ";"
  482. (org-R-make-expr R-function csv-file options)) nil transit-buffer)
  483. ;; (set-buffer (concat "*" transit-buffer "*"))
  484. (unless (or (looking-at "$")
  485. (string-equal (buffer-substring-no-properties 1 2) "|"))
  486. (error "Error in R evaluation:\n%s" (buffer-string))))
  487. (if csv-file
  488. (unless (and infile
  489. (string-equal (file-name-extension infile) "csv"))
  490. (delete-file csv-file)))
  491. (if graphics-output-file (ess-execute "dev.off()")) ;; Close graphics device
  492. (unless (or graphics-output-file
  493. (not (plist-get options :output-to-buffer)))
  494. ;; Send tabular output to a org buffer as new org
  495. ;; table. Recall that we are currently at the beginning of the
  496. ;; first #+R line
  497. (if (and output-file graphics-output-file)
  498. (error "output-to-buffer and graphics-output-file both t"))
  499. (save-excursion
  500. (if output-file
  501. (progn (set-buffer (find-file-noselect output-file))
  502. (delete-region (point-min) (point-max)))
  503. (if (plist-get options :replace)
  504. (progn ;; kill a table iff in one or one ends on the previous line
  505. (delete-region (org-table-begin) (org-table-end))
  506. (save-excursion
  507. (forward-line -1)
  508. (if (looking-at "#\\+TBLNAME")
  509. (delete-region (point) (1+ (point-at-eol))))))))
  510. (if title (insert "#+TBLNAME:" title "\n"))
  511. (insert-buffer-substring (concat "*" transit-buffer "*"))
  512. (org-table-align)
  513. (if output-file (save-buffer))))
  514. ;; We might be linking to graphical output, or to org output in
  515. ;; another file. Either way, point is still at the beginning of
  516. ;; the first #+R line.
  517. (unless (not output-file)
  518. (save-excursion
  519. (forward-line -1)
  520. (if (looking-at "\\[\\[file:")
  521. (delete-region (point) (1+ (point-at-eol)))))
  522. (insert (org-make-link-string
  523. (concat "file:" output-file)
  524. (unless (plist-get options :inline)
  525. (or title (concat output-format " output")))) "\n"))
  526. (kill-buffer (concat "*" transit-buffer "*"))))
  527. (defun org-R-export-to-csv (csv-file options)
  528. "Find and export org table to csv.
  529. If the intable: option has not been supplied, then the table must
  530. end on the line immediately above the #+R lines. Otherwise,
  531. the remote table referenced by the intable: option is found using
  532. org-R-find-table. If options:infile has been set then this is the
  533. org file containing the table. See the docstring of
  534. org-R-find-table for details."
  535. (let ((tbl-name-or-id (plist-get options :intable))
  536. (org-file (plist-get options :infile)) tbl-marker)
  537. (if (and org-file
  538. (not (string-equal (file-name-extension org-file) "org")))
  539. (error "File %s extension is not .csv so should be .org"))
  540. (save-excursion
  541. (if tbl-name-or-id
  542. ;; a remote table has been specified -- move into it
  543. (progn
  544. (if org-file (set-buffer (find-file-noselect org-file)))
  545. (setq tbl-marker (org-R-find-table tbl-name-or-id 'marker))
  546. (set-buffer (marker-buffer tbl-marker))
  547. (goto-char (marker-position tbl-marker)))
  548. (forward-line -1)) ;; move into table above
  549. (if (looking-at "[ \t]*|")
  550. (progn (org-table-export csv-file "orgtbl-to-csv") csv-file)
  551. nil))))
  552. (defun org-R-find-table (name-or-id &optional markerp)
  553. "Return location of a table.
  554. NAME-OR-ID may be the name of a
  555. table in the current file as set by a \"#+TBLNAME:\" directive.
  556. The first table following this line will then be used.
  557. Alternatively, it may be an ID referring to any entry, perhaps in
  558. a different file. In this case, the first table in that entry
  559. will be referenced. The location is returned as a marker pointing
  560. to the beginning of the first line of the table.
  561. This is taken from the first part of org-table-get-remote-range
  562. in org-table.el.
  563. "
  564. (cond
  565. ((symbolp name-or-id) (setq name-or-id (symbol-name name-or-id)))
  566. ((numberp name-or-id) (setq name-or-id (number-to-string name-or-id))))
  567. (save-match-data
  568. (let ((id-loc nil) (case-fold-search t) buffer loc)
  569. (save-excursion
  570. (save-restriction
  571. (widen)
  572. (save-excursion
  573. (goto-char (point-min))
  574. (if (re-search-forward
  575. (concat "^#\\+TBLNAME:[ \t]*" (regexp-quote name-or-id) "[ \t]*$")
  576. nil t)
  577. ;; OK, we've found a matching table name in this buffer.
  578. (setq buffer (current-buffer) loc (match-beginning 0))
  579. ;; It's not a table name in this buffer. It must be an entry id.
  580. ;; obtain a marker pointing to it.
  581. (setq id-loc (org-id-find name-or-id 'marker)
  582. buffer (marker-buffer id-loc)
  583. loc (marker-position id-loc))
  584. (move-marker id-loc nil))) ;; disable the marker
  585. ;; (switch-to-buffer buffer)
  586. (set-buffer buffer)
  587. ;; OK, so now we're in the right buffer, and loc is either
  588. ;; the beginning of the #+TBLNAME line, or the location of the entry
  589. ;; either way we need to search forward to get to the beginning of the table
  590. (save-excursion
  591. (save-restriction
  592. (widen)
  593. (goto-char loc)
  594. (forward-char 1)
  595. ;; The following regexp search finds the beginning of
  596. ;; the next table in this entry. If it gets to the next
  597. ;; entry before the next table, then it signals failure.
  598. (unless (and (re-search-forward "^\\(\\*+ \\)\\|[ \t]*|" nil t)
  599. (not (match-beginning 1)))
  600. (error "Cannot find a table at NAME or ID %s" name-or-id))
  601. (if markerp
  602. (move-marker (make-marker) (point-at-bol) (current-buffer))
  603. (error "Option to return cons cell not implemented.
  604. It should return (file-name . position) to be
  605. consistent with functions in org-id.el")))))))))
  606. (defun org-R-make-expr (R-function csv-file options)
  607. "Construct R code to read data, analyse it and write output."
  608. (let ((rownames (plist-get options :rownames))
  609. (colnames (plist-get options :colnames))
  610. (action (plist-get options :action))
  611. (replace (plist-get options :replace)))
  612. (if (and csv-file (symbolp csv-file))
  613. (setq csv-file (symbol-name csv-file)))
  614. (format "write.org.table((%s)(%s), write.rownames=%s)"
  615. R-function
  616. (if csv-file
  617. (format
  618. "read.csv(\"%s\", header=%s, row.names=%s)"
  619. csv-file
  620. ;; Do we treat first row as colnames? Yes by default
  621. ;; FIXME: should really check for hline
  622. (if colnames "TRUE" "FALSE")
  623. ;; Do we use a column as rownames? Not unless rownames: is specified
  624. (if rownames "1" "NULL"))
  625. "NULL")
  626. ;; Do we write rownames into org table?
  627. (cond ((eq action 'tabulate)
  628. (if (eq (plist-get options :nxcols) 1) "FALSE" "TRUE"))
  629. ((eq action 'transpose) (if colnames "TRUE" "FALSE"))
  630. (rownames "TRUE")
  631. (t "TRUE")))))
  632. (defun org-R-get-options ()
  633. "Parse the #+R: lines and return the options and values as a p-list."
  634. (let ((opts '(
  635. (:infile . "infile")
  636. (:intable . "intable")
  637. (:rownames . "rownames")
  638. (:colnames . "colnames")
  639. (:columns . "columns")
  640. (:action . "action")
  641. (:args . "args")
  642. (:outfile . "outfile")
  643. (:replace . "replace")
  644. (:title . "title")
  645. (:legend . "legend")
  646. (:colour . "colour")
  647. (:color . "color")
  648. (:col . "col")
  649. (:height . "height")
  650. (:width . "width")
  651. (:lines . "lines")
  652. (:sort . "sort")
  653. (:inline . "inline")
  654. (:output-to-buffer . "output-to-buffer")
  655. (:showcode . "showcode")))
  656. (regexp ":\\(\"[^\"]*\"\\|(([^)]*) *([^)]*))\\|([^)]*)\\|[^ \t\n\r;,.]*\\)")
  657. (case-fold-search t) p)
  658. ;; FIXME: set default options properly
  659. (setq p (plist-put p :output-to-buffer t)) ;; FIXME: hack: null options plist is bad news
  660. (setq p (plist-put p :replace t))
  661. (setq p (plist-put p :rownames nil))
  662. (setq p (plist-put p :colnames t))
  663. (setq p (plist-put p :inline nil))
  664. (save-excursion
  665. (while (looking-at "^#\\+\\(RR?:+\\) *\\(.*\\)")
  666. (if (string= "R:" (match-string 1))
  667. (setq p (org-R-add-options-to-plist p (match-string 2) opts regexp)))
  668. (forward-line)))
  669. p))
  670. (defun org-R-add-options-to-plist (p opt-string op regexp)
  671. "Parse a #+R: line and set values in the property list p.
  672. This function is adapted from similar functions in org-exp.el
  673. and org-plot.el. It might be a good idea to have a single
  674. function serving these three files' needs."
  675. ;; Adapted from org-exp.el and org-plot.el
  676. (let (o)
  677. (when opt-string
  678. (while (setq o (pop op))
  679. (if (string-match
  680. (concat (regexp-quote (cdr o)) regexp)
  681. opt-string)
  682. (setq p (plist-put p (car o)
  683. (car (read-from-string
  684. (match-string 1 opt-string)))))))))
  685. p)
  686. (defun org-R-sanitise-options (options)
  687. (error "not used yet")
  688. (let (should-be-strings '(title legend colour color col csv)))
  689. )
  690. (defun org-R-showcode (R)
  691. "Display R function constructed by org-R in a new R-mode
  692. buffer."
  693. (split-window-vertically)
  694. (switch-to-buffer "*org-table.R*")
  695. (kill-region (point-min) (point-max))
  696. (R-mode)
  697. (insert (replace-regexp-in-string
  698. ";" "\n" (replace-regexp-in-string "\\([{}]\\)" "\n\\1\n" R)))
  699. ;; (mark-whole-buffer)
  700. ;; (indent-region)
  701. ;; why doesn't that do what I hoped?
  702. )
  703. (defun org-R-get-remote-range (name-or-id form)
  704. "Get a field value or a list of values in a range from table at ID.
  705. This is a refactoring of Carsten's original version. I have
  706. extracted the first bit of his function and named it
  707. org-R-find-table (which would presumably be called something like
  708. org-table-find-table or org-id-find-table if this were accepted).
  709. ---
  710. Get a field value or a list of values in a range from table at ID.
  711. NAME-OR-ID may be the name of a table in the current file as set by
  712. a \"#+TBLNAME:\" directive. The first table following this line
  713. will then be used. Alternatively, it may be an ID referring to
  714. any entry, possibly in a different file. In this case, the first table
  715. in that entry will be referenced.
  716. FORM is a field or range descriptor like \"@2$3\" or or \"B3\" or
  717. \"@I$2..@II$2\". All the references must be absolute, not relative.
  718. The return value is either a single string for a single field, or a
  719. list of the fields in the rectangle."
  720. (let ((tbl-marker (org-R-find-table name-or-id 'marker))
  721. org-table-column-names org-table-column-name-regexp
  722. org-table-local-parameters org-table-named-field-locations
  723. org-table-current-line-types org-table-current-begin-line
  724. org-table-current-begin-pos org-table-dlines
  725. org-table-hlines org-table-last-alignment
  726. org-table-last-column-widths org-table-last-alignment
  727. org-table-last-column-widths tbeg)
  728. (save-excursion
  729. (set-buffer (marker-buffer tbl-marker))
  730. (goto-char (marker-position tbl-marker))
  731. (org-table-get-specials)
  732. (setq form (org-table-formula-substitute-names form))
  733. (if (and (string-match org-table-range-regexp form)
  734. (> (length (match-string 0 form)) 1))
  735. (save-match-data
  736. (org-table-get-range (match-string 0 form) (point) 1))
  737. form))))
  738. (provide 'org-R)