fixed non-session variable assignment in R

This commit is contained in:
Eric Schulte 2009-06-15 12:17:58 -07:00
parent a44c98b376
commit 112c7d9236
2 changed files with 64 additions and 65 deletions

View File

@ -39,21 +39,24 @@ called by `org-babel-execute-src-block'."
(message "executing R source code block...")
(save-window-excursion
(let* ((vars (org-babel-ref-variables params))
(full-body (concat
(mapconcat ;; define any variables
(lambda (pair)
(org-babel-R-assign-elisp (car pair) (cdr pair)))
vars "\n") "\n" body "\n"))
(result-params (split-string (or (cdr (assoc :results params)) "")))
(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))))
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))
;; (message (format "result-params=%S" result-params))
;; evaluate body and convert the results to ruby
(setq results (org-babel-R-evaluate session body result-type))
(setq results (org-babel-R-evaluate session full-body result-type))
(setq results (if (member "scalar" result-params)
results
(let ((tmp-file (make-temp-file "org-babel-R")))
@ -77,22 +80,18 @@ called by `org-babel-execute-src-block'."
(concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
(format "%S" s)))
(defun org-babel-R-assign-elisp (session name value)
"Read the elisp VALUE into a variable named NAME in the current
R process in `org-babel-R-buffer'."
(unless session (error "No active R buffer"))
(org-babel-comint-input-command
session
(if (listp value)
(let ((transition-file (make-temp-file "org-babel-R-import")))
;; ensure VALUE has an orgtbl structure (depth of at least 2)
(unless (listp (car value)) (setq value (list value)))
(with-temp-file transition-file
(insert (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)))
(insert "\n"))
(format "%s <- read.table(\"%s\", header=FALSE, sep=\"\\t\", as.is=TRUE)"
name transition-file))
(format "%s <- %s" name (org-babel-R-quote-tsv-field value)))))
(defun org-babel-R-assign-elisp (name value)
"Read the elisp VALUE into a variable named NAME."
(if (listp value)
(let ((transition-file (make-temp-file "org-babel-R-import")))
;; ensure VALUE has an orgtbl structure (depth of at least 2)
(unless (listp (car value)) (setq value (list value)))
(with-temp-file transition-file
(insert (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)))
(insert "\n"))
(format "%s <- read.table(\"%s\", header=FALSE, sep=\"\\t\", as.is=TRUE)"
name transition-file))
(format "%s <- %s" name (org-babel-R-quote-tsv-field value))))
(defun org-babel-R-initiate-session (session)
"If there is not a current R process then create one."

View File

@ -2218,52 +2218,52 @@ org-babel functionality.
of these tests may fail.
#+TBLNAME: org-babel-tests
| functionality | block | arg | expected | results | pass |
|-------------------------+----------------------------+-----+-------------+-------------+---------------------------------|
| basic evaluation | | | | | pass |
|-------------------------+----------------------------+-----+-------------+-------------+---------------------------------|
| emacs lisp | basic-elisp | | 5 | 5 | pass |
| shell | basic-shell | | 6 | 6 | pass |
| ruby | basic-ruby | | org-babel | org-babel | pass |
| python | basic-python | | hello world | hello world | pass |
| R | basic-R | | 13 | 13 | pass |
|-------------------------+----------------------------+-----+-------------+-------------+---------------------------------|
| tables | | | | | pass |
|-------------------------+----------------------------+-----+-------------+-------------+---------------------------------|
| emacs lisp | table-elisp | | 3 | 3 | pass |
| ruby | table-ruby | | 1-2-3 | 1-2-3 | pass |
| python | table-python | | 5 | 5 | pass |
| R | table-R | | 3.5 | #ERROR | expected "3.5" but was "#ERROR" |
|-------------------------+----------------------------+-----+-------------+-------------+---------------------------------|
| source block references | | | | | pass |
|-------------------------+----------------------------+-----+-------------+-------------+---------------------------------|
| all languages | chained-ref-last | | Array | Array | pass |
|-------------------------+----------------------------+-----+-------------+-------------+---------------------------------|
| source block functions | | | | | pass |
|-------------------------+----------------------------+-----+-------------+-------------+---------------------------------|
| emacs lisp | defun-fibb | | fibbd | fibbd | pass |
| run over | Fibonacci | 0 | 1 | 1 | pass |
| a | Fibonacci | 1 | 1 | 1 | pass |
| variety | Fibonacci | 2 | 2 | 2 | pass |
| of | Fibonacci | 3 | 3 | 3 | pass |
| different | Fibonacci | 4 | 5 | 5 | pass |
| arguments | Fibonacci | 5 | 8 | 8 | pass |
|-------------------------+----------------------------+-----+-------------+-------------+---------------------------------|
| bugs and tasks | | | | | pass |
|-------------------------+----------------------------+-----+-------------+-------------+---------------------------------|
| simple ruby arrays | ruby-array-test | | 3 | 3 | pass |
| R number evaluation | bug-R-number-evaluation | | 2 | #ERROR | expected "2" but was "#ERROR" |
| multi-line ruby blocks | multi-line-ruby-test | | 2 | 2 | pass |
| forcing vector results | test-forced-vector-results | | Array | Array | pass |
|-------------------------+----------------------------+-----+-------------+-------------+---------------------------------|
| sessions | | | | | pass |
|-------------------------+----------------------------+-----+-------------+-------------+---------------------------------|
| set ruby session | set-ruby-session-var | | :set | :set | pass |
| get from ruby session | get-ruby-session-var | | 3 | | expected "3" but was "" |
| set python session | set-python-session-var | | set | set | pass |
| get from python session | get-python-session-var | | 4 | | expected "4" but was "" |
| set R session | set-R-session-var | | set | set | pass |
| get from R session | get-R-session-var | | 5 | nil | expected "5" but was "nil" |
| functionality | block | arg | expected | results | pass |
|-------------------------+----------------------------+-----+-------------+-------------+----------------------------|
| basic evaluation | | | | | pass |
|-------------------------+----------------------------+-----+-------------+-------------+----------------------------|
| emacs lisp | basic-elisp | | 5 | 5 | pass |
| shell | basic-shell | | 6 | 6 | pass |
| ruby | basic-ruby | | org-babel | org-babel | pass |
| python | basic-python | | hello world | hello world | pass |
| R | basic-R | | 13 | 13 | pass |
|-------------------------+----------------------------+-----+-------------+-------------+----------------------------|
| tables | | | | | pass |
|-------------------------+----------------------------+-----+-------------+-------------+----------------------------|
| emacs lisp | table-elisp | | 3 | 3 | pass |
| ruby | table-ruby | | 1-2-3 | 1-2-3 | pass |
| python | table-python | | 5 | 5 | pass |
| R | table-R | | 3.5 | 3.5 | pass |
|-------------------------+----------------------------+-----+-------------+-------------+----------------------------|
| source block references | | | | | pass |
|-------------------------+----------------------------+-----+-------------+-------------+----------------------------|
| all languages | chained-ref-last | | Array | Array | pass |
|-------------------------+----------------------------+-----+-------------+-------------+----------------------------|
| source block functions | | | | | pass |
|-------------------------+----------------------------+-----+-------------+-------------+----------------------------|
| emacs lisp | defun-fibb | | fibbd | fibbd | pass |
| run over | Fibonacci | 0 | 1 | 1 | pass |
| a | Fibonacci | 1 | 1 | 1 | pass |
| variety | Fibonacci | 2 | 2 | 2 | pass |
| of | Fibonacci | 3 | 3 | 3 | pass |
| different | Fibonacci | 4 | 5 | 5 | pass |
| arguments | Fibonacci | 5 | 8 | 8 | pass |
|-------------------------+----------------------------+-----+-------------+-------------+----------------------------|
| bugs and tasks | | | | | pass |
|-------------------------+----------------------------+-----+-------------+-------------+----------------------------|
| simple ruby arrays | ruby-array-test | | 3 | 3 | pass |
| R number evaluation | bug-R-number-evaluation | | 2 | 2 | pass |
| multi-line ruby blocks | multi-line-ruby-test | | 2 | 2 | pass |
| forcing vector results | test-forced-vector-results | | Array | Array | pass |
|-------------------------+----------------------------+-----+-------------+-------------+----------------------------|
| sessions | | | | | pass |
|-------------------------+----------------------------+-----+-------------+-------------+----------------------------|
| set ruby session | set-ruby-session-var | | :set | :set | pass |
| get from ruby session | get-ruby-session-var | | 3 | | expected "3" but was "" |
| set python session | set-python-session-var | | set | set | pass |
| get from python session | get-python-session-var | | 4 | | expected "4" but was "" |
| set R session | set-R-session-var | | set | set | pass |
| get from R session | get-R-session-var | | 5 | nil | expected "5" but was "nil" |
#+TBLFM: $5='(if (= (length $3) 1) (progn (message (format "running %S" '(sbe $2 (n $3)))) (sbe $2 (n $3))) (sbe $2))::$6='(if (string= $4 $5) "pass" (format "expected %S but was %S" $4 $5))
** basic tests