Browse Source

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.
Dan Davison 16 years ago
parent
commit
911e77a7c9
2 changed files with 24 additions and 16 deletions
  1. 16 6
      lisp/langs/org-babel-R.el
  2. 8 10
      lisp/org-babel.el

+ 16 - 6
lisp/langs/org-babel-R.el

@@ -46,8 +46,8 @@ called by `org-babel-execute-src-block' via multiple-value-bind."
 			 (org-babel-R-assign-elisp (car pair) (cdr pair)))
 		       vars "\n") "\n" body "\n"))
 	  (session (org-babel-R-initiate-session session))
-	  (colnames (cdr (assoc :colnames params))))
-      (org-babel-R-evaluate session full-body result-type colnames))))
+	  (column-names-p (cdr (assoc :colnames params))))
+      (org-babel-R-evaluate session full-body result-type column-names-p))))
 
 (defun org-babel-prep-session:R (session params)
   "Prepare SESSION according to the header arguments specified in PARAMS."
@@ -110,14 +110,15 @@ last statement in BODY, as elisp."
           (value
            (with-temp-file in-tmp-file
              (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))
-	   (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
     (org-babel-comint-in-buffer buffer
       (let* ((tmp-file (make-temp-file "org-babel-R"))
              (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
 				   (list body last-value-eval org-babel-R-eoe-indicator) "\n"))
              (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))))))
         (case result-type
           (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)
 ;;; org-babel-R.el ends here

+ 8 - 10
lisp/org-babel.el

@@ -566,7 +566,7 @@ This is taken almost directly from `org-read-prop'."
   (if (string-match "^[[:digit:]]*\\.?[[:digit:]]*$" 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
 the table is trivial, then return it as a scalar."
   (let (result)
@@ -579,15 +579,13 @@ the table is trivial, then return it as a scalar."
                                    (mapcar #'org-babel-string-read row))
                                  (org-table-to-lisp))))
         (error nil)))
-    (if colnames
-	(setq result (cons (car result) (cons 'hline (cdr result))))
-      (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
-	  (if (consp (car result))
-	      (if (null (cdr (car result)))
-		  (caar result)
-		result)
-	    (car result))
-	result))))
+    (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
+	(if (consp (car result))
+	    (if (null (cdr (car result)))
+		(caar result)
+	      result)
+	  (car result))
+      result)))
 
 (defun org-babel-string-read (cell)
   "Strip nested \"s from around strings in exported R values."