0
0
Fork 1
mirror of https://git.savannah.gnu.org/git/emacs/org-mode.git synced 2024-09-30 03:27:54 +00:00

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
This commit is contained in:
Dan Davison 2010-08-18 20:20:54 -04:00
parent 66ca61126c
commit bf64d25744

View file

@ -219,14 +219,20 @@ write.table(main(), file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=%s, col.names
(defun org-babel-R-evaluate (defun org-babel-R-evaluate
(session body result-type column-names-p row-names-p) (session body result-type column-names-p row-names-p)
"Pass BODY to the R process in SESSION. "Evaluate R code in BODY."
If RESULT-TYPE equals 'output then return a list of the outputs (if session
of the statements in BODY, if RESULT-TYPE equals 'value then (org-babel-R-evaluate-session
return the value of the last statement in BODY, as elisp." session body result-type column-names-p row-names-p)
(if (not session) (org-babel-R-evaluate-external-process
;; external process evaluation 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 (case result-type
(output (org-babel-eval org-babel-R-command body))
(value (value
(let ((tmp-file (make-temp-file "org-babel-R-results-"))) (let ((tmp-file (make-temp-file "org-babel-R-results-")))
(org-babel-eval org-babel-R-command (org-babel-eval org-babel-R-command
@ -238,8 +244,15 @@ return the value of the last statement in BODY, as elisp."
"FALSE"))) "FALSE")))
(org-babel-R-process-value-result (org-babel-R-process-value-result
(org-babel-import-elisp-from-file (org-babel-import-elisp-from-file
(org-babel-maybe-remote-file tmp-file) '(16)) column-names-p)))) (org-babel-maybe-remote-file tmp-file) '(16)) column-names-p)))
;; comint session evaluation (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 (case result-type
(value (value
(let ((tmp-file (make-temp-file "org-babel-R")) (let ((tmp-file (make-temp-file "org-babel-R"))
@ -275,7 +288,7 @@ return the value of the last statement in BODY, as elisp."
(insert (mapconcat #'org-babel-chomp (insert (mapconcat #'org-babel-chomp
(list body org-babel-R-eoe-indicator) (list body org-babel-R-eoe-indicator)
"\n")) "\n"))
(inferior-ess-send-input)))) 2) "\n"))))) (inferior-ess-send-input)))) 2) "\n"))))
(defun org-babel-R-process-value-result (result column-names-p) (defun org-babel-R-process-value-result (result column-names-p)
"R-specific processing of return value. "R-specific processing of return value.