minor edits, starting to add litorgy-R tasks as they crop up during use

This commit is contained in:
Dan Davison 2009-03-05 11:16:45 +00:00
parent e292e34041
commit aae046e55e
3 changed files with 44 additions and 23 deletions

View file

@ -1,4 +1,4 @@
;;; org-R.el --- Numerical computation and data visualisation for org-mode using R ;;; org-R.el --- Computing and data visualisation in Org-mode using R
;; Copyright (C) 2009 ;; Copyright (C) 2009
;; Free Software Foundation, Inc. ;; Free Software Foundation, Inc.
@ -27,12 +27,12 @@
;;; Commentary: ;;; Commentary:
;; This file allows R (http://www.r-project.org) code to be applied to ;; This file allows R (http://www.r-project.org) code to be applied to
;; emacs org-mode (http://orgmode.org) tables. When the ;; emacs org-mode (http://orgmode.org) tables. When the result of the
;; result of the analysis is a vector or matrix, it is output back ;; analysis is a vector or matrix, it is output back into the org-mode
;; into the org-mode buffer as a new org table. Alternatively the R ;; buffer as a new org table. Alternatively the R code may be used to
;; code may be used to plot the data in the org table. It requires R to be ;; plot the data in the org table. It requires R to be running in an
;; running in an inferior-ess-mode buffer (install Emacs Speaks ;; inferior-ess-mode buffer (install Emacs Speaks Statistics
;; Statistics http://ess.r-project.org and issue M-x R). ;; http://ess.r-project.org and issue M-x R).
;; ;;
;; ;;
;; The user interface is via two different options lines in the org ;; The user interface is via two different options lines in the org
@ -87,6 +87,16 @@ To see a more human-readable version of this, look at the code,
or type dput(write.org.table) RET at the R (inferior-ess-mode or type dput(write.org.table) RET at the R (inferior-ess-mode
buffer) prompt.") buffer) prompt.")
(defun org-R-apply-maybe ()
(if (save-excursion
(beginning-of-line 1)
(looking-at "#\\+RR?:"))
(progn (call-interactively 'org-R-apply)
t) ;; to signal that we took action
nil)) ;; to signal that we did not
(add-hook 'org-ctrl-c-ctrl-c-hook 'org-R-apply-maybe)
(defun org-R-apply () (defun org-R-apply ()
"Construct and evaluate an R function call. "Construct and evaluate an R function call.
@ -129,7 +139,7 @@ an org table where appropriate."
(delete-other-windows) ;; FIXME (delete-other-windows) ;; FIXME
(if (plist-get options :showcode) (org-R-showcode code))))) (if (plist-get options :showcode) (org-R-showcode code)))))
(defun org-R-apply-to-subtree () (defun org-R-apply-throughout-subtree ()
"Call org-R-apply in every org-R block in current subtree." "Call org-R-apply in every org-R block in current subtree."
;; This currently relies on re-search-forward leaving point after ;; This currently relies on re-search-forward leaving point after
;; the #+RR?: If point were at the beginning of the line, then ;; the #+RR?: If point were at the beginning of the line, then
@ -146,7 +156,7 @@ an org table where appropriate."
(while (looking-at "#\\+RR?") (while (looking-at "#\\+RR?")
(forward-line))))) (forward-line)))))
(defun org-R-apply-to-buffer () (defun org-R-apply-throughout-buffer ()
"Call org-R-apply in every org-R block in the buffer." "Call org-R-apply in every org-R block in the buffer."
(interactive) (interactive)
(save-excursion (save-excursion
@ -159,7 +169,8 @@ an org table where appropriate."
(defun org-R-construct-code (options) (defun org-R-construct-code (options)
"Construct the R function that implements the requested "Construct the R function that implements the requested
behaviour. The body of this function derives from two sources: behaviour.
The body of this function derives from two sources:
1. Explicit R code which is read from lines starting with 1. Explicit R code which is read from lines starting with
#+RR: by org-R-get-user-code, and #+RR: by org-R-get-user-code, and
@ -178,7 +189,7 @@ org-R-off-the-shelf-code."
(when action (concat (org-R-off-the-shelf-code options) ";")))))) (when action (concat (org-R-off-the-shelf-code options) ";"))))))
(defun org-R-get-user-code (&optional R) (defun org-R-get-user-code (&optional R)
"Read user-supplied R code from #+RR: lines" "Read user-supplied R code from #+RR: lines."
(let ((case-fold-search t)) (let ((case-fold-search t))
(save-excursion (save-excursion
(while (looking-at "^#\\+\\(RR?:\\) *\\(.*\\)") (while (looking-at "^#\\+\\(RR?:\\) *\\(.*\\)")
@ -407,7 +418,9 @@ version?
", ")) ", "))
(defun org-R-make-index-vectors (cols) (defun org-R-make-index-vectors (cols)
"COLS is the lisp form given by the `columns:' option. It may "Construct R indexing vectors as strings from lisp form.
COLS is the lisp form given by the `columns:' option. It may
take the following forms: take the following forms:
1. integer atom - the number of the column 1. integer atom - the number of the column
@ -768,8 +781,8 @@ in org-table.el.
p)) p))
(defun org-R-add-options-to-plist (p opt-string op regexp) (defun org-R-add-options-to-plist (p opt-string op regexp)
"Parse a #+R: line and set values in the property list "Parse a #+R: line and set values in the property list p.
p. This function is adapted from similar functions in org-exp.el This function is adapted from similar functions in org-exp.el
and org-plot.el. It might be a good idea to have a single and org-plot.el. It might be a good idea to have a single
function serving these three files' needs." function serving these three files' needs."
;; Adapted from org-exp.el and org-plot.el ;; Adapted from org-exp.el and org-plot.el
@ -791,7 +804,7 @@ function serving these three files' needs."
) )
(defun org-R-showcode (R) (defun org-R-showcode (R)
"Display R function constructed by org-R in a new R-mode "Display R function constructed by org-R in a new R-mode
buffer" buffer."
(split-window-vertically) (split-window-vertically)
(switch-to-buffer "*org-table.R*") (switch-to-buffer "*org-table.R*")
(kill-region (point-min) (point-max)) (kill-region (point-min) (point-max))
@ -804,11 +817,14 @@ buffer"
) )
(defun org-R-get-remote-range (name-or-id form) (defun org-R-get-remote-range (name-or-id form)
"This is a refactoring of Carsten's original version. I have "Get a field value or a list of values in a range from table at ID.
This is a refactoring of Carsten's original version. I have
extracted the first bit of his function and named it extracted the first bit of his function and named it
org-R-find-table (which would presumably be called org-R-find-table (which would presumably be called something like
something like org-table-find-table or org-id-find-table if this org-table-find-table or org-id-find-table if this were accepted).
were accepted).
---
Get a field value or a list of values in a range from table at ID. Get a field value or a list of values in a range from table at ID.

View file

@ -32,8 +32,8 @@
(require 'org) (require 'org)
(defun litorgy-execute-src-block-maybe () (defun litorgy-execute-src-block-maybe ()
"Detect if this is context for a litorgical src-block and run if "Detect if this is context for a litorgical src-block and if so
so then run `litorgy-execute-src-block'." then run `litorgy-execute-src-block'."
(let ((case-fold-search t)) (let ((case-fold-search t))
(if (save-excursion (if (save-excursion
(beginning-of-line 1) (beginning-of-line 1)

View file

@ -569,7 +569,9 @@ and probably has some advantages (and probably shortfalls).
* Tasks * Tasks
** litorgy-R
*** TODO ability to select which of multiple R sessions is being used (like ess-switch-process in .R buffers)
*** TODO a header argument specifying silent evaluation (no output)
* Sandbox * Sandbox
This is a place for code examples This is a place for code examples
@ -601,11 +603,14 @@ To run these examples open both [[file:litorgy/litorgy.el][litorgy.el]], [[file:
them with =M-x eval-buffer= them with =M-x eval-buffer=
#+begin_src R :replace t #+begin_src R :replace t
hist(rgamma(20,3,3))
a <- 9 a <- 9
b <- 18 b <- 17
a + b a + b
#+end_src #+end_src
: 26
** free variables ** free variables