浏览代码

evaluation is working in R capturing both outputs and values

Eric Schulte 16 年之前
父节点
当前提交
e0dcbce75e
共有 2 个文件被更改,包括 33 次插入32 次删除
  1. 21 13
      lisp/org-babel-R.el
  2. 12 19
      org-babel.org

+ 21 - 13
lisp/org-babel-R.el

@@ -43,16 +43,18 @@ called by `org-babel-execute-src-block'."
            (result-type (cond ((member "output" result-params) 'output)
                               ((member "value" result-params) 'value)
                               (t 'value)))
-           ;; (session (org-babel-R-initiate-session (cdr (assoc :session params))))
-           (session (get-buffer "*R*"))
+           (session (org-babel-R-initiate-session ;; (cdr (assoc :session params))
+                                                  (get-buffer "*R*")))
+           ;; (session (get-buffer "*R*"))
            results)
       ;; assign variables
       (mapc (lambda (pair) (org-babel-R-assign-elisp session (car pair) (cdr pair))) vars)
+      ;; ;;; debugging statements
+      ;; (message (format "result-type=%S" result-type))
+      ;; (message (format "body=%S" body))
+      ;; (message (format "session=%S" session))
       ;; evaluate body and convert the results to ruby
-      (message (format "result-type=%S" result-type))
-      (message (format "body=%S" body))
       (setq results (org-babel-R-evaluate session body result-type))
-      (message (format "results=%S" results))
       (let ((tmp-file (make-temp-file "org-babel-R")))
         (with-temp-file tmp-file (insert results))
         (org-babel-import-elisp-from-file tmp-file)))))
@@ -84,7 +86,8 @@ R process in `org-babel-R-buffer'."
 
 (defun org-babel-R-initiate-session (session)
   "If there is not a current R process then create one."
-  (unless (org-babel-comint-buffer-livep session)
+  (if (org-babel-comint-buffer-livep session)
+      session
     (save-window-excursion (R) (current-buffer))))
 
 (defvar org-babel-R-eoe-indicator "'org_babel_R_eoe'")
@@ -118,17 +121,22 @@ last statement in BODY."
           (accept-process-output (get-buffer-process buffer)))
         ;; remove filter
         (remove-hook 'comint-output-filter-functions 'my-filt))
-      ;; remove echo'd FULL-BODY from input
-      (if (string-match (replace-regexp-in-string "\n" "\r\n" (regexp-quote full-body)) string-buffer)
-          (setq string-buffer (substring string-buffer (match-end 0))))
+      ;; ;;; debugging
+      ;; ;; echo raw results
+      ;; (message (format "raw-results=%S" string-buffer))
+      ;; ;; split results
+      (message (format "split-results=%S" (split-string string-buffer comint-prompt-regexp)))
       ;; split results with `comint-prompt-regexp'
       (setq results (let ((broke nil))
                       (delete nil (mapcar (lambda (el)
                                                   (if (or broke
-                                                          (string-match (regexp-quote org-babel-R-eoe-output) el)
-                                                          (= (length el) 0))
-                                                      (progn (setq broke t) nil)
-                                                    el))
+                                                          (and (string-match (regexp-quote org-babel-R-eoe-output) el) (setq broke t)))
+                                                      nil
+                                                    (if (= (length el) 0)
+                                                        nil
+                                                      (if (string-match comint-prompt-regexp el)
+                                                          (substring el (match-end 0))
+                                                        el))))
                                           (mapcar #'org-babel-trim (split-string string-buffer comint-prompt-regexp))))))
       (case result-type
         (output (mapconcat #'identity results "\n"))

+ 12 - 19
org-babel.org

@@ -521,42 +521,35 @@ schulte + 3
 schulte
 #+end_src
 
-**** TODO R [0/4]
+**** TODO R [2/4]
 
-- [ ] functional results working with comint
-- [ ] script results
+- [X] functional results working with comint
+- [X] script results
 - [ ] ensure callable by other source block
 - [ ] rename buffer after session
 
 To redirect output to a file, you can use the =sink()= command.
 
 #+srcname: task_R_B
-#+begin_src R :results replace output scalar
-92
-21
+#+begin_src R :results replace value
+a <- 9
+b <- 10
+b - a
+a + b
 #+end_src
 
 #+resname: task_R_B
-| "[1]" | 92 |
-| "[1]" | 21 |
-
-
-
+: 19
 
 #+srcname: task-R-use-other-output
-#+begin_src R :var twoentyseven=task_R_B() :results replace script
+#+begin_src R :var twoentyseven=task_R_B() :results replace value
 83
 twoentyseven
 #+end_src
 
 #+resname: task-R-use-other-output
-: [1] 83
-: [1] 27
-
-#+srcname: name
-#+begin_src emacs-lisp 
-(setq debug-on-error t)
-#+end_src
+| "[1]" |  1 |
+| "[1]" | 19 |
 
 **** TODO Python