diff options
| author | Rasmus | 2017-06-21 13:20:20 +0200 |
|---|---|---|
| committer | Rasmus | 2017-06-22 11:54:18 +0200 |
| commit | 5cecd275820df825c51bf9a27fcc7e35f30ff273 (patch) | |
| tree | b3f72e63953613d565e6d5a35bec97f158eb603c /lisp/org/ob-R.el | |
| parent | 386a3da920482b8cb3e962fb944d135c8a770e26 (diff) | |
| download | emacs-5cecd275820df825c51bf9a27fcc7e35f30ff273.tar.gz emacs-5cecd275820df825c51bf9a27fcc7e35f30ff273.zip | |
Update Org to v9.0.9
Please see etc/ORG-NEWS for details.
Diffstat (limited to 'lisp/org/ob-R.el')
| -rw-r--r-- | lisp/org/ob-R.el | 270 |
1 files changed, 175 insertions, 95 deletions
diff --git a/lisp/org/ob-R.el b/lisp/org/ob-R.el index 51d342702ce..3accade49f5 100644 --- a/lisp/org/ob-R.el +++ b/lisp/org/ob-R.el | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | ;;; ob-R.el --- org-babel functions for R code evaluation | 1 | ;;; ob-R.el --- Babel Functions for R -*- lexical-binding: t; -*- |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 2009-2017 Free Software Foundation, Inc. | 3 | ;; Copyright (C) 2009-2017 Free Software Foundation, Inc. |
| 4 | 4 | ||
| @@ -27,16 +27,17 @@ | |||
| 27 | ;; Org-Babel support for evaluating R code | 27 | ;; Org-Babel support for evaluating R code |
| 28 | 28 | ||
| 29 | ;;; Code: | 29 | ;;; Code: |
| 30 | |||
| 31 | (require 'cl-lib) | ||
| 30 | (require 'ob) | 32 | (require 'ob) |
| 31 | (eval-when-compile (require 'cl)) | ||
| 32 | 33 | ||
| 33 | (declare-function orgtbl-to-tsv "org-table" (table params)) | 34 | (declare-function orgtbl-to-tsv "org-table" (table params)) |
| 34 | (declare-function R "ext:essd-r" (&optional start-args)) | 35 | (declare-function R "ext:essd-r" (&optional start-args)) |
| 35 | (declare-function inferior-ess-send-input "ext:ess-inf" ()) | 36 | (declare-function inferior-ess-send-input "ext:ess-inf" ()) |
| 36 | (declare-function ess-make-buffer-current "ext:ess-inf" ()) | 37 | (declare-function ess-make-buffer-current "ext:ess-inf" ()) |
| 37 | (declare-function ess-eval-buffer "ext:ess-inf" (vis)) | 38 | (declare-function ess-eval-buffer "ext:ess-inf" (vis)) |
| 38 | (declare-function org-number-sequence "org-compat" (from &optional to inc)) | 39 | (declare-function ess-wait-for-process "ext:ess-inf" |
| 39 | (declare-function org-remove-if-not "org" (predicate seq)) | 40 | (&optional proc sec-prompt wait force-redisplay)) |
| 40 | 41 | ||
| 41 | (defconst org-babel-header-args:R | 42 | (defconst org-babel-header-args:R |
| 42 | '((width . :any) | 43 | '((width . :any) |
| @@ -60,12 +61,25 @@ | |||
| 60 | (useDingbats . :any) | 61 | (useDingbats . :any) |
| 61 | (horizontal . :any) | 62 | (horizontal . :any) |
| 62 | (results . ((file list vector table scalar verbatim) | 63 | (results . ((file list vector table scalar verbatim) |
| 63 | (raw org html latex code pp wrap) | 64 | (raw html latex org code pp drawer) |
| 64 | (replace silent append prepend) | 65 | (replace silent none append prepend) |
| 65 | (output value graphics)))) | 66 | (output value graphics)))) |
| 66 | "R-specific header arguments.") | 67 | "R-specific header arguments.") |
| 67 | 68 | ||
| 69 | (defconst ob-R-safe-header-args | ||
| 70 | (append org-babel-safe-header-args | ||
| 71 | '(:width :height :bg :units :pointsize :antialias :quality | ||
| 72 | :compression :res :type :family :title :fonts | ||
| 73 | :version :paper :encoding :pagecentre :colormodel | ||
| 74 | :useDingbats :horizontal)) | ||
| 75 | "Header args which are safe for R babel blocks. | ||
| 76 | |||
| 77 | See `org-babel-safe-header-args' for documentation of the format of | ||
| 78 | this variable.") | ||
| 79 | |||
| 68 | (defvar org-babel-default-header-args:R '()) | 80 | (defvar org-babel-default-header-args:R '()) |
| 81 | (put 'org-babel-default-header-args:R 'safe-local-variable | ||
| 82 | (org-babel-header-args-safe-fn ob-R-safe-header-args)) | ||
| 69 | 83 | ||
| 70 | (defcustom org-babel-R-command "R --slave --no-save" | 84 | (defcustom org-babel-R-command "R --slave --no-save" |
| 71 | "Name of command to use for executing R code." | 85 | "Name of command to use for executing R code." |
| @@ -73,56 +87,103 @@ | |||
| 73 | :version "24.1" | 87 | :version "24.1" |
| 74 | :type 'string) | 88 | :type 'string) |
| 75 | 89 | ||
| 76 | (defvar ess-local-process-name) ; dynamically scoped | 90 | (defvar ess-current-process-name) ; dynamically scoped |
| 91 | (defvar ess-local-process-name) ; dynamically scoped | ||
| 77 | (defun org-babel-edit-prep:R (info) | 92 | (defun org-babel-edit-prep:R (info) |
| 78 | (let ((session (cdr (assoc :session (nth 2 info))))) | 93 | (let ((session (cdr (assq :session (nth 2 info))))) |
| 79 | (when (and session (string-match "^\\*\\(.+?\\)\\*$" session)) | 94 | (when (and session |
| 80 | (save-match-data (org-babel-R-initiate-session session nil))))) | 95 | (string-prefix-p "*" session) |
| 81 | 96 | (string-suffix-p "*" session)) | |
| 82 | (defun org-babel-expand-body:R (body params &optional graphics-file) | 97 | (org-babel-R-initiate-session session nil)))) |
| 98 | |||
| 99 | ;; The usage of utils::read.table() ensures that the command | ||
| 100 | ;; read.table() can be found even in circumstances when the utils | ||
| 101 | ;; package is not in the search path from R. | ||
| 102 | (defconst ob-R-transfer-variable-table-with-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 | ) | ||
| 114 | close(con) | ||
| 115 | res | ||
| 116 | })" | ||
| 117 | "R code used to transfer a table defined as a variable from org to R. | ||
| 118 | |||
| 119 | This function is used when the table contains a header.") | ||
| 120 | |||
| 121 | (defconst ob-R-transfer-variable-table-without-header | ||
| 122 | "%s <- local({ | ||
| 123 | con <- textConnection( | ||
| 124 | %S | ||
| 125 | ) | ||
| 126 | res <- utils::read.table( | ||
| 127 | con, | ||
| 128 | header = %s, | ||
| 129 | row.names = %s, | ||
| 130 | sep = \"\\t\", | ||
| 131 | as.is = TRUE, | ||
| 132 | fill = TRUE, | ||
| 133 | col.names = paste(\"V\", seq_len(%d), sep =\"\") | ||
| 134 | ) | ||
| 135 | close(con) | ||
| 136 | res | ||
| 137 | })" | ||
| 138 | "R code used to transfer a table defined as a variable from org to R. | ||
| 139 | |||
| 140 | This function is used when the table does not contain a header.") | ||
| 141 | |||
| 142 | (defun org-babel-expand-body:R (body params &optional _graphics-file) | ||
| 83 | "Expand BODY according to PARAMS, return the expanded body." | 143 | "Expand BODY according to PARAMS, return the expanded body." |
| 84 | (let ((graphics-file | 144 | (mapconcat 'identity |
| 85 | (or graphics-file (org-babel-R-graphical-output-file params)))) | 145 | (append |
| 86 | (mapconcat | 146 | (when (cdr (assq :prologue params)) |
| 87 | #'identity | 147 | (list (cdr (assq :prologue params)))) |
| 88 | (let ((inside | 148 | (org-babel-variable-assignments:R params) |
| 89 | (append | 149 | (list body) |
| 90 | (when (cdr (assoc :prologue params)) | 150 | (when (cdr (assq :epilogue params)) |
| 91 | (list (cdr (assoc :prologue params)))) | 151 | (list (cdr (assq :epilogue params))))) |
| 92 | (org-babel-variable-assignments:R params) | 152 | "\n")) |
| 93 | (list body) | ||
| 94 | (when (cdr (assoc :epilogue params)) | ||
| 95 | (list (cdr (assoc :epilogue params))))))) | ||
| 96 | (if graphics-file | ||
| 97 | (append | ||
| 98 | (list (org-babel-R-construct-graphics-device-call | ||
| 99 | graphics-file params)) | ||
| 100 | inside | ||
| 101 | (list "dev.off()")) | ||
| 102 | inside)) | ||
| 103 | "\n"))) | ||
| 104 | 153 | ||
| 105 | (defun org-babel-execute:R (body params) | 154 | (defun org-babel-execute:R (body params) |
| 106 | "Execute a block of R code. | 155 | "Execute a block of R code. |
| 107 | This function is called by `org-babel-execute-src-block'." | 156 | This function is called by `org-babel-execute-src-block'." |
| 108 | (save-excursion | 157 | (save-excursion |
| 109 | (let* ((result-params (cdr (assoc :result-params params))) | 158 | (let* ((result-params (cdr (assq :result-params params))) |
| 110 | (result-type (cdr (assoc :result-type params))) | 159 | (result-type (cdr (assq :result-type params))) |
| 111 | (session (org-babel-R-initiate-session | 160 | (session (org-babel-R-initiate-session |
| 112 | (cdr (assoc :session params)) params)) | 161 | (cdr (assq :session params)) params)) |
| 113 | (colnames-p (cdr (assoc :colnames params))) | 162 | (colnames-p (cdr (assq :colnames params))) |
| 114 | (rownames-p (cdr (assoc :rownames params))) | 163 | (rownames-p (cdr (assq :rownames params))) |
| 115 | (graphics-file (org-babel-R-graphical-output-file params)) | 164 | (graphics-file (and (member "graphics" (assq :result-params params)) |
| 116 | (full-body (org-babel-expand-body:R body params graphics-file)) | 165 | (org-babel-graphical-output-file params))) |
| 166 | (full-body | ||
| 167 | (let ((inside | ||
| 168 | (list (org-babel-expand-body:R body params graphics-file)))) | ||
| 169 | (mapconcat 'identity | ||
| 170 | (if graphics-file | ||
| 171 | (append | ||
| 172 | (list (org-babel-R-construct-graphics-device-call | ||
| 173 | graphics-file params)) | ||
| 174 | inside | ||
| 175 | (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()")) | ||
| 176 | inside) | ||
| 177 | "\n"))) | ||
| 117 | (result | 178 | (result |
| 118 | (org-babel-R-evaluate | 179 | (org-babel-R-evaluate |
| 119 | session full-body result-type result-params | 180 | session full-body result-type result-params |
| 120 | (or (equal "yes" colnames-p) | 181 | (or (equal "yes" colnames-p) |
| 121 | (org-babel-pick-name | 182 | (org-babel-pick-name |
| 122 | (cdr (assoc :colname-names params)) colnames-p)) | 183 | (cdr (assq :colname-names params)) colnames-p)) |
| 123 | (or (equal "yes" rownames-p) | 184 | (or (equal "yes" rownames-p) |
| 124 | (org-babel-pick-name | 185 | (org-babel-pick-name |
| 125 | (cdr (assoc :rowname-names params)) rownames-p))))) | 186 | (cdr (assq :rowname-names params)) rownames-p))))) |
| 126 | (if graphics-file nil result)))) | 187 | (if graphics-file nil result)))) |
| 127 | 188 | ||
| 128 | (defun org-babel-prep-session:R (session params) | 189 | (defun org-babel-prep-session:R (session params) |
| @@ -148,21 +209,21 @@ This function is called by `org-babel-execute-src-block'." | |||
| 148 | 209 | ||
| 149 | (defun org-babel-variable-assignments:R (params) | 210 | (defun org-babel-variable-assignments:R (params) |
| 150 | "Return list of R statements assigning the block's variables." | 211 | "Return list of R statements assigning the block's variables." |
| 151 | (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))) | 212 | (let ((vars (org-babel--get-vars params))) |
| 152 | (mapcar | 213 | (mapcar |
| 153 | (lambda (pair) | 214 | (lambda (pair) |
| 154 | (org-babel-R-assign-elisp | 215 | (org-babel-R-assign-elisp |
| 155 | (car pair) (cdr pair) | 216 | (car pair) (cdr pair) |
| 156 | (equal "yes" (cdr (assoc :colnames params))) | 217 | (equal "yes" (cdr (assq :colnames params))) |
| 157 | (equal "yes" (cdr (assoc :rownames params))))) | 218 | (equal "yes" (cdr (assq :rownames params))))) |
| 158 | (mapcar | 219 | (mapcar |
| 159 | (lambda (i) | 220 | (lambda (i) |
| 160 | (cons (car (nth i vars)) | 221 | (cons (car (nth i vars)) |
| 161 | (org-babel-reassemble-table | 222 | (org-babel-reassemble-table |
| 162 | (cdr (nth i vars)) | 223 | (cdr (nth i vars)) |
| 163 | (cdr (nth i (cdr (assoc :colname-names params)))) | 224 | (cdr (nth i (cdr (assq :colname-names params)))) |
| 164 | (cdr (nth i (cdr (assoc :rowname-names params))))))) | 225 | (cdr (nth i (cdr (assq :rowname-names params))))))) |
| 165 | (org-number-sequence 0 (1- (length vars))))))) | 226 | (number-sequence 0 (1- (length vars))))))) |
| 166 | 227 | ||
| 167 | (defun org-babel-R-quote-tsv-field (s) | 228 | (defun org-babel-R-quote-tsv-field (s) |
| 168 | "Quote field S for export to R." | 229 | "Quote field S for export to R." |
| @@ -173,35 +234,25 @@ This function is called by `org-babel-execute-src-block'." | |||
| 173 | (defun org-babel-R-assign-elisp (name value colnames-p rownames-p) | 234 | (defun org-babel-R-assign-elisp (name value colnames-p rownames-p) |
| 174 | "Construct R code assigning the elisp VALUE to a variable named NAME." | 235 | "Construct R code assigning the elisp VALUE to a variable named NAME." |
| 175 | (if (listp value) | 236 | (if (listp value) |
| 176 | (let* ((lengths (mapcar 'length (org-remove-if-not 'sequencep value))) | 237 | (let* ((lengths (mapcar 'length (cl-remove-if-not 'sequencep value))) |
| 177 | (max (if lengths (apply 'max lengths) 0)) | 238 | (max (if lengths (apply 'max lengths) 0)) |
| 178 | (min (if lengths (apply 'min lengths) 0)) | 239 | (min (if lengths (apply 'min lengths) 0))) |
| 179 | (transition-file (org-babel-temp-file "R-import-"))) | ||
| 180 | ;; Ensure VALUE has an orgtbl structure (depth of at least 2). | 240 | ;; Ensure VALUE has an orgtbl structure (depth of at least 2). |
| 181 | (unless (listp (car value)) (setq value (list value))) | 241 | (unless (listp (car value)) (setq value (list value))) |
| 182 | (with-temp-file transition-file | 242 | (let ((file (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field))) |
| 183 | (insert | ||
| 184 | (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)) | ||
| 185 | "\n")) | ||
| 186 | (let ((file (org-babel-process-file-name transition-file 'noquote)) | ||
| 187 | (header (if (or (eq (nth 1 value) 'hline) colnames-p) | 243 | (header (if (or (eq (nth 1 value) 'hline) colnames-p) |
| 188 | "TRUE" "FALSE")) | 244 | "TRUE" "FALSE")) |
| 189 | (row-names (if rownames-p "1" "NULL"))) | 245 | (row-names (if rownames-p "1" "NULL"))) |
| 190 | (if (= max min) | 246 | (if (= max min) |
| 191 | (format "%s <- read.table(\"%s\", | 247 | (format ob-R-transfer-variable-table-with-header |
| 192 | header=%s, | 248 | name file header row-names) |
| 193 | row.names=%s, | 249 | (format ob-R-transfer-variable-table-without-header |
| 194 | sep=\"\\t\", | ||
| 195 | as.is=TRUE)" name file header row-names) | ||
| 196 | (format "%s <- read.table(\"%s\", | ||
| 197 | header=%s, | ||
| 198 | row.names=%s, | ||
| 199 | sep=\"\\t\", | ||
| 200 | as.is=TRUE, | ||
| 201 | fill=TRUE, | ||
| 202 | col.names = paste(\"V\", seq_len(%d), sep =\"\"))" | ||
| 203 | name file header row-names max)))) | 250 | name file header row-names max)))) |
| 204 | (format "%s <- %s" name (org-babel-R-quote-tsv-field value)))) | 251 | (cond ((integerp value) (format "%s <- %s" name (concat (number-to-string value) "L"))) |
| 252 | ((floatp value) (format "%s <- %s" name value)) | ||
| 253 | ((stringp value) (format "%s <- %S" name (org-no-properties value))) | ||
| 254 | (t (format "%s <- %S" name (prin1-to-string value)))))) | ||
| 255 | |||
| 205 | 256 | ||
| 206 | (defvar ess-ask-for-ess-directory) ; dynamically scoped | 257 | (defvar ess-ask-for-ess-directory) ; dynamically scoped |
| 207 | (defun org-babel-R-initiate-session (session params) | 258 | (defun org-babel-R-initiate-session (session params) |
| @@ -209,8 +260,9 @@ This function is called by `org-babel-execute-src-block'." | |||
| 209 | (unless (string= session "none") | 260 | (unless (string= session "none") |
| 210 | (let ((session (or session "*R*")) | 261 | (let ((session (or session "*R*")) |
| 211 | (ess-ask-for-ess-directory | 262 | (ess-ask-for-ess-directory |
| 212 | (and (and (boundp 'ess-ask-for-ess-directory) ess-ask-for-ess-directory) | 263 | (and (boundp 'ess-ask-for-ess-directory) |
| 213 | (not (cdr (assoc :dir params)))))) | 264 | ess-ask-for-ess-directory |
| 265 | (not (cdr (assq :dir params)))))) | ||
| 214 | (if (org-babel-comint-buffer-livep session) | 266 | (if (org-babel-comint-buffer-livep session) |
| 215 | session | 267 | session |
| 216 | (save-window-excursion | 268 | (save-window-excursion |
| @@ -218,6 +270,10 @@ This function is called by `org-babel-execute-src-block'." | |||
| 218 | ;; Session buffer exists, but with dead process | 270 | ;; Session buffer exists, but with dead process |
| 219 | (set-buffer session)) | 271 | (set-buffer session)) |
| 220 | (require 'ess) (R) | 272 | (require 'ess) (R) |
| 273 | (let ((R-proc (get-process (or ess-local-process-name | ||
| 274 | ess-current-process-name)))) | ||
| 275 | (while (process-get R-proc 'callbacks) | ||
| 276 | (ess-wait-for-process R-proc))) | ||
| 221 | (rename-buffer | 277 | (rename-buffer |
| 222 | (if (bufferp session) | 278 | (if (bufferp session) |
| 223 | (buffer-name session) | 279 | (buffer-name session) |
| @@ -234,11 +290,6 @@ current code buffer." | |||
| 234 | (process-name (get-buffer-process session))) | 290 | (process-name (get-buffer-process session))) |
| 235 | (ess-make-buffer-current)) | 291 | (ess-make-buffer-current)) |
| 236 | 292 | ||
| 237 | (defun org-babel-R-graphical-output-file (params) | ||
| 238 | "Name of file to which R should send graphical output." | ||
| 239 | (and (member "graphics" (cdr (assq :result-params params))) | ||
| 240 | (cdr (assq :file params)))) | ||
| 241 | |||
| 242 | (defvar org-babel-R-graphics-devices | 293 | (defvar org-babel-R-graphics-devices |
| 243 | '((:bmp "bmp" "filename") | 294 | '((:bmp "bmp" "filename") |
| 244 | (:jpg "jpeg" "filename") | 295 | (:jpg "jpeg" "filename") |
| @@ -265,8 +316,7 @@ Each member of this list is a list with three members: | |||
| 265 | :type :family :title :fonts :version | 316 | :type :family :title :fonts :version |
| 266 | :paper :encoding :pagecentre :colormodel | 317 | :paper :encoding :pagecentre :colormodel |
| 267 | :useDingbats :horizontal)) | 318 | :useDingbats :horizontal)) |
| 268 | (device (and (string-match ".+\\.\\([^.]+\\)" out-file) | 319 | (device (file-name-extension out-file)) |
| 269 | (match-string 1 out-file))) | ||
| 270 | (device-info (or (assq (intern (concat ":" device)) | 320 | (device-info (or (assq (intern (concat ":" device)) |
| 271 | org-babel-R-graphics-devices) | 321 | org-babel-R-graphics-devices) |
| 272 | (assq :png org-babel-R-graphics-devices))) | 322 | (assq :png org-babel-R-graphics-devices))) |
| @@ -280,14 +330,43 @@ Each member of this list is a list with three members: | |||
| 280 | (substring (symbol-name (car pair)) 1) | 330 | (substring (symbol-name (car pair)) 1) |
| 281 | (cdr pair)) "")) | 331 | (cdr pair)) "")) |
| 282 | params "")) | 332 | params "")) |
| 283 | (format "%s(%s=\"%s\"%s%s%s)" | 333 | (format "%s(%s=\"%s\"%s%s%s); tryCatch({" |
| 284 | device filearg out-file args | 334 | device filearg out-file args |
| 285 | (if extra-args "," "") (or extra-args "")))) | 335 | (if extra-args "," "") (or extra-args "")))) |
| 286 | 336 | ||
| 287 | (defvar org-babel-R-eoe-indicator "'org_babel_R_eoe'") | 337 | (defconst org-babel-R-eoe-indicator "'org_babel_R_eoe'") |
| 288 | (defvar org-babel-R-eoe-output "[1] \"org_babel_R_eoe\"") | 338 | (defconst org-babel-R-eoe-output "[1] \"org_babel_R_eoe\"") |
| 289 | 339 | ||
| 290 | (defvar org-babel-R-write-object-command "{function(object,transfer.file){object;invisible(if(inherits(try({tfile<-tempfile();write.table(object,file=tfile,sep=\"\\t\",na=\"nil\",row.names=%s,col.names=%s,quote=FALSE);file.rename(tfile,transfer.file)},silent=TRUE),\"try-error\")){if(!file.exists(transfer.file))file.create(transfer.file)})}}(object=%s,transfer.file=\"%s\")") | 340 | (defconst org-babel-R-write-object-command "{ |
| 341 | function(object,transfer.file) { | ||
| 342 | object | ||
| 343 | invisible( | ||
| 344 | if ( | ||
| 345 | inherits( | ||
| 346 | try( | ||
| 347 | { | ||
| 348 | tfile<-tempfile() | ||
| 349 | write.table(object, file=tfile, sep=\"\\t\", | ||
| 350 | na=\"nil\",row.names=%s,col.names=%s, | ||
| 351 | quote=FALSE) | ||
| 352 | file.rename(tfile,transfer.file) | ||
| 353 | }, | ||
| 354 | silent=TRUE), | ||
| 355 | \"try-error\")) | ||
| 356 | { | ||
| 357 | if(!file.exists(transfer.file)) | ||
| 358 | file.create(transfer.file) | ||
| 359 | } | ||
| 360 | ) | ||
| 361 | } | ||
| 362 | }(object=%s,transfer.file=\"%s\")" | ||
| 363 | "A template for an R command to evaluate a block of code and write the result to a file. | ||
| 364 | |||
| 365 | Has four %s escapes to be filled in: | ||
| 366 | 1. Row names, \"TRUE\" or \"FALSE\" | ||
| 367 | 2. Column names, \"TRUE\" or \"FALSE\" | ||
| 368 | 3. The code to be run (must be an expression, not a statement) | ||
| 369 | 4. The name of the file to write to") | ||
| 291 | 370 | ||
| 292 | (defun org-babel-R-evaluate | 371 | (defun org-babel-R-evaluate |
| 293 | (session body result-type result-params column-names-p row-names-p) | 372 | (session body result-type result-params column-names-p row-names-p) |
| @@ -299,12 +378,12 @@ Each member of this list is a list with three members: | |||
| 299 | body result-type result-params column-names-p row-names-p))) | 378 | body result-type result-params column-names-p row-names-p))) |
| 300 | 379 | ||
| 301 | (defun org-babel-R-evaluate-external-process | 380 | (defun org-babel-R-evaluate-external-process |
| 302 | (body result-type result-params column-names-p row-names-p) | 381 | (body result-type result-params column-names-p row-names-p) |
| 303 | "Evaluate BODY in external R process. | 382 | "Evaluate BODY in external R process. |
| 304 | If RESULT-TYPE equals 'output then return standard output as a | 383 | If RESULT-TYPE equals `output' then return standard output as a |
| 305 | string. If RESULT-TYPE equals 'value then return the value of the | 384 | string. If RESULT-TYPE equals `value' then return the value of the |
| 306 | last statement in BODY, as elisp." | 385 | last statement in BODY, as elisp." |
| 307 | (case result-type | 386 | (cl-case result-type |
| 308 | (value | 387 | (value |
| 309 | (let ((tmp-file (org-babel-temp-file "R-"))) | 388 | (let ((tmp-file (org-babel-temp-file "R-"))) |
| 310 | (org-babel-eval org-babel-R-command | 389 | (org-babel-eval org-babel-R-command |
| @@ -319,7 +398,7 @@ last statement in BODY, as elisp." | |||
| 319 | (org-babel-result-cond result-params | 398 | (org-babel-result-cond result-params |
| 320 | (with-temp-buffer | 399 | (with-temp-buffer |
| 321 | (insert-file-contents tmp-file) | 400 | (insert-file-contents tmp-file) |
| 322 | (buffer-string)) | 401 | (org-babel-chomp (buffer-string) "\n")) |
| 323 | (org-babel-import-elisp-from-file tmp-file '(16))) | 402 | (org-babel-import-elisp-from-file tmp-file '(16))) |
| 324 | column-names-p))) | 403 | column-names-p))) |
| 325 | (output (org-babel-eval org-babel-R-command body)))) | 404 | (output (org-babel-eval org-babel-R-command body)))) |
| @@ -327,12 +406,12 @@ last statement in BODY, as elisp." | |||
| 327 | (defvar ess-eval-visibly-p) | 406 | (defvar ess-eval-visibly-p) |
| 328 | 407 | ||
| 329 | (defun org-babel-R-evaluate-session | 408 | (defun org-babel-R-evaluate-session |
| 330 | (session body result-type result-params column-names-p row-names-p) | 409 | (session body result-type result-params column-names-p row-names-p) |
| 331 | "Evaluate BODY in SESSION. | 410 | "Evaluate BODY in SESSION. |
| 332 | If RESULT-TYPE equals 'output then return standard output as a | 411 | If RESULT-TYPE equals `output' then return standard output as a |
| 333 | string. If RESULT-TYPE equals 'value then return the value of the | 412 | string. If RESULT-TYPE equals `value' then return the value of the |
| 334 | last statement in BODY, as elisp." | 413 | last statement in BODY, as elisp." |
| 335 | (case result-type | 414 | (cl-case result-type |
| 336 | (value | 415 | (value |
| 337 | (with-temp-buffer | 416 | (with-temp-buffer |
| 338 | (insert (org-babel-chomp body)) | 417 | (insert (org-babel-chomp body)) |
| @@ -353,12 +432,12 @@ last statement in BODY, as elisp." | |||
| 353 | (org-babel-result-cond result-params | 432 | (org-babel-result-cond result-params |
| 354 | (with-temp-buffer | 433 | (with-temp-buffer |
| 355 | (insert-file-contents tmp-file) | 434 | (insert-file-contents tmp-file) |
| 356 | (buffer-string)) | 435 | (org-babel-chomp (buffer-string) "\n")) |
| 357 | (org-babel-import-elisp-from-file tmp-file '(16))) | 436 | (org-babel-import-elisp-from-file tmp-file '(16))) |
| 358 | column-names-p))) | 437 | column-names-p))) |
| 359 | (output | 438 | (output |
| 360 | (mapconcat | 439 | (mapconcat |
| 361 | #'org-babel-chomp | 440 | 'org-babel-chomp |
| 362 | (butlast | 441 | (butlast |
| 363 | (delq nil | 442 | (delq nil |
| 364 | (mapcar | 443 | (mapcar |
| @@ -366,11 +445,12 @@ last statement in BODY, as elisp." | |||
| 366 | (mapcar | 445 | (mapcar |
| 367 | (lambda (line) ;; cleanup extra prompts left in output | 446 | (lambda (line) ;; cleanup extra prompts left in output |
| 368 | (if (string-match | 447 | (if (string-match |
| 369 | "^\\([ ]*[>+\\.][ ]?\\)+\\([[0-9]+\\|[ ]\\)" line) | 448 | "^\\([>+.]\\([ ][>.+]\\)*[ ]\\)" |
| 449 | (car (split-string line "\n"))) | ||
| 370 | (substring line (match-end 1)) | 450 | (substring line (match-end 1)) |
| 371 | line)) | 451 | line)) |
| 372 | (org-babel-comint-with-output (session org-babel-R-eoe-output) | 452 | (org-babel-comint-with-output (session org-babel-R-eoe-output) |
| 373 | (insert (mapconcat #'org-babel-chomp | 453 | (insert (mapconcat 'org-babel-chomp |
| 374 | (list body org-babel-R-eoe-indicator) | 454 | (list body org-babel-R-eoe-indicator) |
| 375 | "\n")) | 455 | "\n")) |
| 376 | (inferior-ess-send-input)))))) "\n")))) | 456 | (inferior-ess-send-input)))))) "\n")))) |