0
0
Fork 1
mirror of https://git.savannah.gnu.org/git/emacs/org-mode.git synced 2024-09-30 00:57:48 +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,63 +219,76 @@ 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)))
(case result-type
(output (org-babel-eval org-babel-R-command body)) (defun org-babel-R-evaluate-external-process
(value (body result-type column-names-p row-names-p)
(let ((tmp-file (make-temp-file "org-babel-R-results-"))) "Evaluate BODY in external R process.
(org-babel-eval org-babel-R-command If RESULT-TYPE equals 'output then return standard output as a
(format org-babel-R-wrapper-method string. If RESULT-TYPE equals 'value then return the value of the
body tmp-file last statement in BODY, as elisp."
(if row-names-p "TRUE" "FALSE") (case result-type
(if column-names-p (value
(if row-names-p "NA" "TRUE") (let ((tmp-file (make-temp-file "org-babel-R-results-")))
"FALSE"))) (org-babel-eval org-babel-R-command
(org-babel-R-process-value-result (format org-babel-R-wrapper-method
(org-babel-import-elisp-from-file body tmp-file
(org-babel-maybe-remote-file tmp-file) '(16)) column-names-p)))) (if row-names-p "TRUE" "FALSE")
;; comint session evaluation (if column-names-p
(case result-type (if row-names-p "NA" "TRUE")
(value "FALSE")))
(let ((tmp-file (make-temp-file "org-babel-R")) (org-babel-R-process-value-result
broke) (org-babel-import-elisp-from-file
(org-babel-comint-with-output (session org-babel-R-eoe-output) (org-babel-maybe-remote-file tmp-file) '(16)) column-names-p)))
(insert (mapconcat (output (org-babel-eval org-babel-R-command body))))
#'org-babel-chomp
(list (defun org-babel-R-evaluate-session
body (session body result-type column-names-p row-names-p)
(format org-babel-R-wrapper-lastvar "Evaluate BODY in SESSION.
tmp-file If RESULT-TYPE equals 'output then return standard output as a
(if row-names-p "TRUE" "FALSE") string. If RESULT-TYPE equals 'value then return the value of the
(if column-names-p last statement in BODY, as elisp."
(if row-names-p "NA" "TRUE") (case result-type
"FALSE")) (value
org-babel-R-eoe-indicator) "\n")) (let ((tmp-file (make-temp-file "org-babel-R"))
(inferior-ess-send-input)) broke)
(org-babel-R-process-value-result (org-babel-comint-with-output (session org-babel-R-eoe-output)
(org-babel-import-elisp-from-file (insert (mapconcat
(org-babel-maybe-remote-file tmp-file) '(16)) column-names-p))) #'org-babel-chomp
(output (list
(mapconcat body
#'org-babel-chomp (format org-babel-R-wrapper-lastvar
(butlast tmp-file
(delq nil (if row-names-p "TRUE" "FALSE")
(mapcar (if column-names-p
(lambda (line) ;; cleanup extra prompts left in output (if row-names-p "NA" "TRUE")
(if (string-match "FALSE"))
"^\\([ ]*[>+][ ]?\\)+\\([[0-9]+\\|[ ]\\)" line) org-babel-R-eoe-indicator) "\n"))
(substring line (match-end 1)) (inferior-ess-send-input))
line)) (org-babel-R-process-value-result
(org-babel-comint-with-output (session org-babel-R-eoe-output) (org-babel-import-elisp-from-file
(insert (mapconcat #'org-babel-chomp (org-babel-maybe-remote-file tmp-file) '(16)) column-names-p)))
(list body org-babel-R-eoe-indicator) (output
"\n")) (mapconcat
(inferior-ess-send-input)))) 2) "\n"))))) #'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) (defun org-babel-R-process-value-result (result column-names-p)
"R-specific processing of return value. "R-specific processing of return value.