summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Davison <davison@stats.ox.ac.uk>2010-08-18 20:20:54 -0400
committerDan Davison <davison@stats.ox.ac.uk>2010-08-18 20:20:54 -0400
commitbf64d25744ae4cf9d755e016e063144ebb837ab9 (patch)
tree3a450a88ddf2362ab6239b3b0fc5c91aaa3fc622
parent66ca61126c99e9706422f18f778d764b85b0be37 (diff)
downloadorg-mode-bf64d25744ae4cf9d755e016e063144ebb837ab9.tar.gz
babel: R: Refactor evaluation code
* ob-R.el (org-babel-R-evaluate): Break the two branches into two separate functions (org-babel-R-evaluate-external-process): New function to handle external process evaluation (org-babel-R-evaluate-session): New function to handle session evaluation
-rw-r--r--lisp/ob-R.el127
1 files changed, 70 insertions, 57 deletions
diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index 7d46437..2a11421 100644
--- a/lisp/ob-R.el
+++ b/lisp/ob-R.el
@@ -219,63 +219,76 @@ write.table(main(), file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=%s, col.names
(defun org-babel-R-evaluate
(session body result-type column-names-p row-names-p)
- "Pass BODY to the R process in SESSION.
-If RESULT-TYPE equals 'output then return a list of the outputs
-of the statements in BODY, if RESULT-TYPE equals 'value then
-return the value of the last statement in BODY, as elisp."
- (if (not session)
- ;; external process evaluation
- (case result-type
- (output (org-babel-eval org-babel-R-command body))
- (value
- (let ((tmp-file (make-temp-file "org-babel-R-results-")))
- (org-babel-eval org-babel-R-command
- (format org-babel-R-wrapper-method
- body tmp-file
- (if row-names-p "TRUE" "FALSE")
- (if column-names-p
- (if row-names-p "NA" "TRUE")
- "FALSE")))
- (org-babel-R-process-value-result
- (org-babel-import-elisp-from-file
- (org-babel-maybe-remote-file tmp-file) '(16)) column-names-p))))
- ;; comint session evaluation
- (case result-type
- (value
- (let ((tmp-file (make-temp-file "org-babel-R"))
- broke)
- (org-babel-comint-with-output (session org-babel-R-eoe-output)
- (insert (mapconcat
- #'org-babel-chomp
- (list
- body
- (format org-babel-R-wrapper-lastvar
- tmp-file
- (if row-names-p "TRUE" "FALSE")
- (if column-names-p
- (if row-names-p "NA" "TRUE")
- "FALSE"))
- org-babel-R-eoe-indicator) "\n"))
- (inferior-ess-send-input))
- (org-babel-R-process-value-result
- (org-babel-import-elisp-from-file
- (org-babel-maybe-remote-file tmp-file) '(16)) column-names-p)))
- (output
- (mapconcat
- #'org-babel-chomp
- (butlast
- (delq nil
- (mapcar
- (lambda (line) ;; cleanup extra prompts left in output
- (if (string-match
- "^\\([ ]*[>+][ ]?\\)+\\([[0-9]+\\|[ ]\\)" line)
- (substring line (match-end 1))
- line))
- (org-babel-comint-with-output (session org-babel-R-eoe-output)
- (insert (mapconcat #'org-babel-chomp
- (list body org-babel-R-eoe-indicator)
- "\n"))
- (inferior-ess-send-input)))) 2) "\n")))))
+ "Evaluate R code in BODY."
+ (if session
+ (org-babel-R-evaluate-session
+ session body result-type column-names-p row-names-p)
+ (org-babel-R-evaluate-external-process
+ body result-type column-names-p row-names-p)))
+
+(defun org-babel-R-evaluate-external-process
+ (body result-type column-names-p row-names-p)
+ "Evaluate BODY in external R process.
+If RESULT-TYPE equals 'output then return standard output as a
+string. If RESULT-TYPE equals 'value then return the value of the
+last statement in BODY, as elisp."
+ (case result-type
+ (value
+ (let ((tmp-file (make-temp-file "org-babel-R-results-")))
+ (org-babel-eval org-babel-R-command
+ (format org-babel-R-wrapper-method
+ body tmp-file
+ (if row-names-p "TRUE" "FALSE")
+ (if column-names-p
+ (if row-names-p "NA" "TRUE")
+ "FALSE")))
+ (org-babel-R-process-value-result
+ (org-babel-import-elisp-from-file
+ (org-babel-maybe-remote-file tmp-file) '(16)) column-names-p)))
+ (output (org-babel-eval org-babel-R-command body))))
+
+(defun org-babel-R-evaluate-session
+ (session body result-type column-names-p row-names-p)
+ "Evaluate BODY in SESSION.
+If RESULT-TYPE equals 'output then return standard output as a
+string. If RESULT-TYPE equals 'value then return the value of the
+last statement in BODY, as elisp."
+ (case result-type
+ (value
+ (let ((tmp-file (make-temp-file "org-babel-R"))
+ broke)
+ (org-babel-comint-with-output (session org-babel-R-eoe-output)
+ (insert (mapconcat
+ #'org-babel-chomp
+ (list
+ body
+ (format org-babel-R-wrapper-lastvar
+ tmp-file
+ (if row-names-p "TRUE" "FALSE")
+ (if column-names-p
+ (if row-names-p "NA" "TRUE")
+ "FALSE"))
+ org-babel-R-eoe-indicator) "\n"))
+ (inferior-ess-send-input))
+ (org-babel-R-process-value-result
+ (org-babel-import-elisp-from-file
+ (org-babel-maybe-remote-file tmp-file) '(16)) column-names-p)))
+ (output
+ (mapconcat
+ #'org-babel-chomp
+ (butlast
+ (delq nil
+ (mapcar
+ (lambda (line) ;; cleanup extra prompts left in output
+ (if (string-match
+ "^\\([ ]*[>+][ ]?\\)+\\([[0-9]+\\|[ ]\\)" line)
+ (substring line (match-end 1))
+ line))
+ (org-babel-comint-with-output (session org-babel-R-eoe-output)
+ (insert (mapconcat #'org-babel-chomp
+ (list body org-babel-R-eoe-indicator)
+ "\n"))
+ (inferior-ess-send-input)))) 2) "\n"))))
(defun org-babel-R-process-value-result (result column-names-p)
"R-specific processing of return value.