Code rearrangement: restrict R column names changes to org-babel-R.el.

On reflection this is (currently) R-specific functionality and should
not affect the code in org-babel.el.
This commit is contained in:
Dan Davison 2009-07-19 12:01:00 -04:00
parent 8294122f8f
commit 911e77a7c9
2 changed files with 24 additions and 16 deletions

View file

@ -46,8 +46,8 @@ called by `org-babel-execute-src-block' via multiple-value-bind."
(org-babel-R-assign-elisp (car pair) (cdr pair))) (org-babel-R-assign-elisp (car pair) (cdr pair)))
vars "\n") "\n" body "\n")) vars "\n") "\n" body "\n"))
(session (org-babel-R-initiate-session session)) (session (org-babel-R-initiate-session session))
(colnames (cdr (assoc :colnames params)))) (column-names-p (cdr (assoc :colnames params))))
(org-babel-R-evaluate session full-body result-type colnames)))) (org-babel-R-evaluate session full-body result-type column-names-p))))
(defun org-babel-prep-session:R (session params) (defun org-babel-prep-session:R (session params)
"Prepare SESSION according to the header arguments specified in PARAMS." "Prepare SESSION according to the header arguments specified in PARAMS."
@ -110,14 +110,15 @@ last statement in BODY, as elisp."
(value (value
(with-temp-file in-tmp-file (with-temp-file in-tmp-file
(insert (format org-babel-R-wrapper-method (insert (format org-babel-R-wrapper-method
body out-tmp-file (if colnames "TRUE" "FALSE")))) body out-tmp-file (if column-names-p "TRUE" "FALSE"))))
(shell-command (format "R --no-save < '%s'" in-tmp-file)) (shell-command (format "R --no-save < '%s'" in-tmp-file))
(org-babel-import-elisp-from-file out-tmp-file colnames)))) (org-babel-R-process-value-result
(org-babel-import-elisp-from-file out-tmp-file) column-names-p))))
;; comint session evaluation ;; comint session evaluation
(org-babel-comint-in-buffer buffer (org-babel-comint-in-buffer buffer
(let* ((tmp-file (make-temp-file "org-babel-R")) (let* ((tmp-file (make-temp-file "org-babel-R"))
(last-value-eval (last-value-eval
(format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=%s, quote=FALSE)" tmp-file (if colnames "TRUE" "FALSE"))) (format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=%s, quote=FALSE)" tmp-file (if column-names-p "TRUE" "FALSE")))
(full-body (mapconcat #'org-babel-chomp (full-body (mapconcat #'org-babel-chomp
(list body last-value-eval org-babel-R-eoe-indicator) "\n")) (list body last-value-eval org-babel-R-eoe-indicator) "\n"))
(raw (org-babel-comint-with-output buffer org-babel-R-eoe-output nil (raw (org-babel-comint-with-output buffer org-babel-R-eoe-output nil
@ -139,8 +140,17 @@ last statement in BODY, as elisp."
(mapcar #'org-babel-trim raw)))))) (mapcar #'org-babel-trim raw))))))
(case result-type (case result-type
(output (org-babel-chomp (mapconcat #'identity results "\n"))) (output (org-babel-chomp (mapconcat #'identity results "\n")))
(value (org-babel-import-elisp-from-file tmp-file colnames))))))) (value (org-babel-R-process-value-result
(org-babel-import-elisp-from-file tmp-file) column-names-p)))))))
(defun org-babel-R-process-value-result (result column-names-p)
"R-specific processing of return value prior to return to org-babel.
Currently, insert hline if column names in output have been requested."
(if column-names-p
(cons (car result) (cons 'hline (cdr result)))
result))
(provide 'org-babel-R) (provide 'org-babel-R)
;;; org-babel-R.el ends here ;;; org-babel-R.el ends here

View file

@ -566,7 +566,7 @@ This is taken almost directly from `org-read-prop'."
(if (string-match "^[[:digit:]]*\\.?[[:digit:]]*$" string) (if (string-match "^[[:digit:]]*\\.?[[:digit:]]*$" string)
(string-to-number string))) (string-to-number string)))
(defun org-babel-import-elisp-from-file (file-name &optional colnames) (defun org-babel-import-elisp-from-file (file-name)
"Read the results located at FILE-NAME into an elisp table. If "Read the results located at FILE-NAME into an elisp table. If
the table is trivial, then return it as a scalar." the table is trivial, then return it as a scalar."
(let (result) (let (result)
@ -579,15 +579,13 @@ the table is trivial, then return it as a scalar."
(mapcar #'org-babel-string-read row)) (mapcar #'org-babel-string-read row))
(org-table-to-lisp)))) (org-table-to-lisp))))
(error nil))) (error nil)))
(if colnames (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
(setq result (cons (car result) (cons 'hline (cdr result)))) (if (consp (car result))
(if (null (cdr result)) ;; if result is trivial vector, then scalarize it (if (null (cdr (car result)))
(if (consp (car result)) (caar result)
(if (null (cdr (car result))) result)
(caar result) (car result))
result) result)))
(car result))
result))))
(defun org-babel-string-read (cell) (defun org-babel-string-read (cell)
"Strip nested \"s from around strings in exported R values." "Strip nested \"s from around strings in exported R values."