diff options
| author | Daniel Colascione | 2012-10-07 14:31:58 -0800 |
|---|---|---|
| committer | Daniel Colascione | 2012-10-07 14:31:58 -0800 |
| commit | 36a305a723c63fd345be65c536c52fe9765c14be (patch) | |
| tree | fb89d9e103552863214c60297a65320917109357 /lisp/org/ob-R.el | |
| parent | 2ab329f3b5d52a39f0a45c3d9c129f1c19560142 (diff) | |
| parent | 795b1482a9e314cda32d62ac2988f573d359366e (diff) | |
| download | emacs-36a305a723c63fd345be65c536c52fe9765c14be.tar.gz emacs-36a305a723c63fd345be65c536c52fe9765c14be.zip | |
Merge from trunk
Diffstat (limited to 'lisp/org/ob-R.el')
| -rw-r--r-- | lisp/org/ob-R.el | 91 |
1 files changed, 67 insertions, 24 deletions
diff --git a/lisp/org/ob-R.el b/lisp/org/ob-R.el index 49a8a85cf6d..3dedb393654 100644 --- a/lisp/org/ob-R.el +++ b/lisp/org/ob-R.el | |||
| @@ -39,24 +39,48 @@ | |||
| 39 | (declare-function ess-make-buffer-current "ext:ess-inf" ()) | 39 | (declare-function ess-make-buffer-current "ext:ess-inf" ()) |
| 40 | (declare-function ess-eval-buffer "ext:ess-inf" (vis)) | 40 | (declare-function ess-eval-buffer "ext:ess-inf" (vis)) |
| 41 | (declare-function org-number-sequence "org-compat" (from &optional to inc)) | 41 | (declare-function org-number-sequence "org-compat" (from &optional to inc)) |
| 42 | 42 | (declare-function org-remove-if-not "org" (predicate seq)) | |
| 43 | (defconst org-babel-header-arg-names:R | 43 | |
| 44 | '(width height bg units pointsize antialias quality compression | 44 | (defconst org-babel-header-args:R |
| 45 | res type family title fonts version paper encoding | 45 | '((width . :any) |
| 46 | pagecentre colormodel useDingbats horizontal) | 46 | (height . :any) |
| 47 | (bg . :any) | ||
| 48 | (units . :any) | ||
| 49 | (pointsize . :any) | ||
| 50 | (antialias . :any) | ||
| 51 | (quality . :any) | ||
| 52 | (compression . :any) | ||
| 53 | (res . :any) | ||
| 54 | (type . :any) | ||
| 55 | (family . :any) | ||
| 56 | (title . :any) | ||
| 57 | (fonts . :any) | ||
| 58 | (version . :any) | ||
| 59 | (paper . :any) | ||
| 60 | (encoding . :any) | ||
| 61 | (pagecentre . :any) | ||
| 62 | (colormodel . :any) | ||
| 63 | (useDingbats . :any) | ||
| 64 | (horizontal . :any) | ||
| 65 | (results . ((file list vector table scalar verbatim) | ||
| 66 | (raw org html latex code pp wrap) | ||
| 67 | (replace silent append prepend) | ||
| 68 | (output value graphics)))) | ||
| 47 | "R-specific header arguments.") | 69 | "R-specific header arguments.") |
| 48 | 70 | ||
| 49 | (defvar org-babel-default-header-args:R '()) | 71 | (defvar org-babel-default-header-args:R '()) |
| 50 | 72 | ||
| 51 | (defvar org-babel-R-command "R --slave --no-save" | 73 | (defcustom org-babel-R-command "R --slave --no-save" |
| 52 | "Name of command to use for executing R code.") | 74 | "Name of command to use for executing R code." |
| 75 | :group 'org-babel | ||
| 76 | :version "24.1" | ||
| 77 | :type 'string) | ||
| 53 | 78 | ||
| 54 | (defvar ess-local-process-name) | 79 | (defvar ess-local-process-name) ; dynamically scoped |
| 55 | (defun org-babel-edit-prep:R (info) | 80 | (defun org-babel-edit-prep:R (info) |
| 56 | (let ((session (cdr (assoc :session (nth 2 info))))) | 81 | (let ((session (cdr (assoc :session (nth 2 info))))) |
| 57 | (when (and session (string-match "^\\*\\(.+?\\)\\*$" session)) | 82 | (when (and session (string-match "^\\*\\(.+?\\)\\*$" session)) |
| 58 | (save-match-data (org-babel-R-initiate-session session nil)) | 83 | (save-match-data (org-babel-R-initiate-session session nil))))) |
| 59 | (setq ess-local-process-name (match-string 1 session))))) | ||
| 60 | 84 | ||
| 61 | (defun org-babel-expand-body:R (body params &optional graphics-file) | 85 | (defun org-babel-expand-body:R (body params &optional graphics-file) |
| 62 | "Expand BODY according to PARAMS, return the expanded body." | 86 | "Expand BODY according to PARAMS, return the expanded body." |
| @@ -120,7 +144,7 @@ This function is called by `org-babel-execute-src-block'." | |||
| 120 | ;; helper functions | 144 | ;; helper functions |
| 121 | 145 | ||
| 122 | (defun org-babel-variable-assignments:R (params) | 146 | (defun org-babel-variable-assignments:R (params) |
| 123 | "Return list of R statements assigning the block's variables" | 147 | "Return list of R statements assigning the block's variables." |
| 124 | (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))) | 148 | (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))) |
| 125 | (mapcar | 149 | (mapcar |
| 126 | (lambda (pair) | 150 | (lambda (pair) |
| @@ -146,25 +170,45 @@ This function is called by `org-babel-execute-src-block'." | |||
| 146 | (defun org-babel-R-assign-elisp (name value colnames-p rownames-p) | 170 | (defun org-babel-R-assign-elisp (name value colnames-p rownames-p) |
| 147 | "Construct R code assigning the elisp VALUE to a variable named NAME." | 171 | "Construct R code assigning the elisp VALUE to a variable named NAME." |
| 148 | (if (listp value) | 172 | (if (listp value) |
| 149 | (let ((transition-file (org-babel-temp-file "R-import-"))) | 173 | (let ((max (apply #'max (mapcar #'length (org-remove-if-not |
| 174 | #'sequencep value)))) | ||
| 175 | (min (apply #'min (mapcar #'length (org-remove-if-not | ||
| 176 | #'sequencep value)))) | ||
| 177 | (transition-file (org-babel-temp-file "R-import-"))) | ||
| 150 | ;; ensure VALUE has an orgtbl structure (depth of at least 2) | 178 | ;; ensure VALUE has an orgtbl structure (depth of at least 2) |
| 151 | (unless (listp (car value)) (setq value (list value))) | 179 | (unless (listp (car value)) (setq value (list value))) |
| 152 | (with-temp-file transition-file | 180 | (with-temp-file transition-file |
| 153 | (insert (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field))) | 181 | (insert |
| 154 | (insert "\n")) | 182 | (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)) |
| 155 | (format "%s <- read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE)" | 183 | "\n")) |
| 156 | name (org-babel-process-file-name transition-file 'noquote) | 184 | (let ((file (org-babel-process-file-name transition-file 'noquote)) |
| 157 | (if (or (eq (nth 1 value) 'hline) colnames-p) "TRUE" "FALSE") | 185 | (header (if (or (eq (nth 1 value) 'hline) colnames-p) |
| 158 | (if rownames-p "1" "NULL"))) | 186 | "TRUE" "FALSE")) |
| 187 | (row-names (if rownames-p "1" "NULL"))) | ||
| 188 | (if (= max min) | ||
| 189 | (format "%s <- read.table(\"%s\", | ||
| 190 | header=%s, | ||
| 191 | row.names=%s, | ||
| 192 | sep=\"\\t\", | ||
| 193 | as.is=TRUE)" name file header row-names) | ||
| 194 | (format "%s <- read.table(\"%s\", | ||
| 195 | header=%s, | ||
| 196 | row.names=%s, | ||
| 197 | sep=\"\\t\", | ||
| 198 | as.is=TRUE, | ||
| 199 | fill=TRUE, | ||
| 200 | col.names = paste(\"V\", seq_len(%d), sep =\"\"))" | ||
| 201 | name file header row-names max)))) | ||
| 159 | (format "%s <- %s" name (org-babel-R-quote-tsv-field value)))) | 202 | (format "%s <- %s" name (org-babel-R-quote-tsv-field value)))) |
| 160 | 203 | ||
| 161 | (defvar ess-ask-for-ess-directory nil) | 204 | (defvar ess-ask-for-ess-directory) ; dynamically scoped |
| 162 | (defun org-babel-R-initiate-session (session params) | 205 | (defun org-babel-R-initiate-session (session params) |
| 163 | "If there is not a current R process then create one." | 206 | "If there is not a current R process then create one." |
| 164 | (unless (string= session "none") | 207 | (unless (string= session "none") |
| 165 | (let ((session (or session "*R*")) | 208 | (let ((session (or session "*R*")) |
| 166 | (ess-ask-for-ess-directory | 209 | (ess-ask-for-ess-directory |
| 167 | (and ess-ask-for-ess-directory (not (cdr (assoc :dir params)))))) | 210 | (and (and (boundp 'ess-ask-for-ess-directory) ess-ask-for-ess-directory) |
| 211 | (not (cdr (assoc :dir params)))))) | ||
| 168 | (if (org-babel-comint-buffer-livep session) | 212 | (if (org-babel-comint-buffer-livep session) |
| 169 | session | 213 | session |
| 170 | (save-window-excursion | 214 | (save-window-excursion |
| @@ -177,7 +221,6 @@ This function is called by `org-babel-execute-src-block'." | |||
| 177 | (buffer-name)))) | 221 | (buffer-name)))) |
| 178 | (current-buffer)))))) | 222 | (current-buffer)))))) |
| 179 | 223 | ||
| 180 | (defvar ess-local-process-name nil) | ||
| 181 | (defun org-babel-R-associate-session (session) | 224 | (defun org-babel-R-associate-session (session) |
| 182 | "Associate R code buffer with an R session. | 225 | "Associate R code buffer with an R session. |
| 183 | Make SESSION be the inferior ESS process associated with the | 226 | Make SESSION be the inferior ESS process associated with the |
| @@ -219,7 +262,7 @@ current code buffer." | |||
| 219 | (setq args (mapconcat | 262 | (setq args (mapconcat |
| 220 | (lambda (pair) | 263 | (lambda (pair) |
| 221 | (if (member (car pair) allowed-args) | 264 | (if (member (car pair) allowed-args) |
| 222 | (format ",%s=%s" | 265 | (format ",%s=%S" |
| 223 | (substring (symbol-name (car pair)) 1) | 266 | (substring (symbol-name (car pair)) 1) |
| 224 | (cdr pair)) "")) | 267 | (cdr pair)) "")) |
| 225 | params "")) | 268 | params "")) |
| @@ -245,7 +288,7 @@ current code buffer." | |||
| 245 | (body result-type result-params column-names-p row-names-p) | 288 | (body result-type result-params column-names-p row-names-p) |
| 246 | "Evaluate BODY in external R process. | 289 | "Evaluate BODY in external R process. |
| 247 | If RESULT-TYPE equals 'output then return standard output as a | 290 | If RESULT-TYPE equals 'output then return standard output as a |
| 248 | string. If RESULT-TYPE equals 'value then return the value of the | 291 | string. If RESULT-TYPE equals 'value then return the value of the |
| 249 | last statement in BODY, as elisp." | 292 | last statement in BODY, as elisp." |
| 250 | (case result-type | 293 | (case result-type |
| 251 | (value | 294 | (value |
| @@ -272,7 +315,7 @@ last statement in BODY, as elisp." | |||
| 272 | (session body result-type result-params column-names-p row-names-p) | 315 | (session body result-type result-params column-names-p row-names-p) |
| 273 | "Evaluate BODY in SESSION. | 316 | "Evaluate BODY in SESSION. |
| 274 | If RESULT-TYPE equals 'output then return standard output as a | 317 | If RESULT-TYPE equals 'output then return standard output as a |
| 275 | string. If RESULT-TYPE equals 'value then return the value of the | 318 | string. If RESULT-TYPE equals 'value then return the value of the |
| 276 | last statement in BODY, as elisp." | 319 | last statement in BODY, as elisp." |
| 277 | (case result-type | 320 | (case result-type |
| 278 | (value | 321 | (value |