emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* graphing from org-tables
@ 2008-07-25 15:53 Eric Schulte
  2008-07-25 16:23 ` Carsten Dominik
  2008-07-25 16:25 ` James TD Smith
  0 siblings, 2 replies; 19+ messages in thread
From: Eric Schulte @ 2008-07-25 15:53 UTC (permalink / raw)
  To: Org-mode


Any advice for quick graphing of a table in org-mode?

I searched the mailing list archives and found nothing.  It would be
nice to have a command to dump the contents of a table (or single
column) to gnuplot.

I know calc can interact with gnuplot

info:calc:Basic Graphics

but with my VERY limited calc experience, I don't see an obvious way
to push information from org-tables through the calc gnuplot
interface.

Thanks -- Eric

-- 
schulte

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: graphing from org-tables
  2008-07-25 15:53 graphing from org-tables Eric Schulte
@ 2008-07-25 16:23 ` Carsten Dominik
  2008-07-25 16:25 ` James TD Smith
  1 sibling, 0 replies; 19+ messages in thread
From: Carsten Dominik @ 2008-07-25 16:23 UTC (permalink / raw)
  To: Eric Schulte; +Cc: Org-mode

Hi Eric,


Currently this is not implemented.  Could be done, there are functions  
to grab sections of a table.  Nice add-on project?

- Carsten

On Jul 25, 2008, at 8:53 AM, Eric Schulte wrote:

>
> Any advice for quick graphing of a table in org-mode?
>
> I searched the mailing list archives and found nothing.  It would be
> nice to have a command to dump the contents of a table (or single
> column) to gnuplot.
>
> I know calc can interact with gnuplot
>
> info:calc:Basic Graphics
>
> but with my VERY limited calc experience, I don't see an obvious way
> to push information from org-tables through the calc gnuplot
> interface.
>
> Thanks -- Eric
>
> -- 
> schulte
>
>
>
>
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: graphing from org-tables
  2008-07-25 15:53 graphing from org-tables Eric Schulte
  2008-07-25 16:23 ` Carsten Dominik
@ 2008-07-25 16:25 ` James TD Smith
  2008-07-25 16:33   ` Carsten Dominik
                     ` (2 more replies)
  1 sibling, 3 replies; 19+ messages in thread
From: James TD Smith @ 2008-07-25 16:25 UTC (permalink / raw)
  To: Org-mode

On 2008-07-25 08:53:31(-0700), Eric Schulte wrote:
> 
> Any advice for quick graphing of a table in org-mode?
> 

I have a setup for plotting data from tables. I'm not sure if it's exactly what
you want, but yoy may find it useful.

1. Add the following to your .emacs:

(defun ahkt-plot-table (script)
  "util function to export and plot a table using the supplied
gnuplot `script'"
  (org-table-export)
  (let ((cbuf (current-buffer))
	(cwin (selected-window)))
    (save-restriction
      (save-excursion
	(find-file script)
	(gnuplot-send-buffer-to-gnuplot)
	(bury-buffer)
	(bury-buffer (get-buffer "*gnuplot*"))))
    (and (window-live-p cwin) (select-window cwin))
    (switch-to-buffer cbuf)
    (delete-other-windows)))

2. Create a gnuplot script which plots data from a file.

3. Add the following properties to the headline containing the table.
TABLE_EXPORT_FILE <filename in the gnuploy script>
TABLE_EXPORT_FORMAT orgtbl-to-generic :skip 4 :splice t :sep "\t"

4. Add an org link in the table (it must be in the table otherwise the export
doesn't work) as below:
[[elisp:(ahkt-plot-table "<gnuplot script>")][plot table]] 

I suggest you put it at the top of the table.
You will then need to adjust the 'skip' parameter in the export format depending
on the number of lines at the top of the table which should not be exported
(hlines, more than one plotting link etc). 

5. You should then be able to open the link, and get a plot of the table
contents.


--
|-<James TD Smith>-<email/ahktenzero@mohorovi.cc>-|

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: graphing from org-tables
  2008-07-25 16:25 ` James TD Smith
@ 2008-07-25 16:33   ` Carsten Dominik
  2008-07-25 17:14   ` Eric Schulte
  2008-07-25 19:07   ` Eric Schulte
  2 siblings, 0 replies; 19+ messages in thread
From: Carsten Dominik @ 2008-07-25 16:33 UTC (permalink / raw)
  To: James TD Smith; +Cc: Org-mode

Coool....

On Jul 25, 2008, at 9:25 AM, James TD Smith wrote:

> On 2008-07-25 08:53:31(-0700), Eric Schulte wrote:
>>
>> Any advice for quick graphing of a table in org-mode?
>>
>
> I have a setup for plotting data from tables. I'm not sure if it's  
> exactly what
> you want, but yoy may find it useful.
>
> 1. Add the following to your .emacs:
>
> (defun ahkt-plot-table (script)
>  "util function to export and plot a table using the supplied
> gnuplot `script'"
>  (org-table-export)
>  (let ((cbuf (current-buffer))
> 	(cwin (selected-window)))
>    (save-restriction
>      (save-excursion
> 	(find-file script)
> 	(gnuplot-send-buffer-to-gnuplot)
> 	(bury-buffer)
> 	(bury-buffer (get-buffer "*gnuplot*"))))
>    (and (window-live-p cwin) (select-window cwin))
>    (switch-to-buffer cbuf)
>    (delete-other-windows)))
>
> 2. Create a gnuplot script which plots data from a file.
>
> 3. Add the following properties to the headline containing the table.
> TABLE_EXPORT_FILE <filename in the gnuploy script>
> TABLE_EXPORT_FORMAT orgtbl-to-generic :skip 4 :splice t :sep "\t"
>
> 4. Add an org link in the table (it must be in the table otherwise  
> the export
> doesn't work) as below:
> [[elisp:(ahkt-plot-table "<gnuplot script>")][plot table]]
>
> I suggest you put it at the top of the table.
> You will then need to adjust the 'skip' parameter in the export  
> format depending
> on the number of lines at the top of the table which should not be  
> exported
> (hlines, more than one plotting link etc).
>
> 5. You should then be able to open the link, and get a plot of the  
> table
> contents.
>
>
> --
> |-<James TD Smith>-<email/ahktenzero@mohorovi.cc>-|
>
>
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: graphing from org-tables
  2008-07-25 16:25 ` James TD Smith
  2008-07-25 16:33   ` Carsten Dominik
@ 2008-07-25 17:14   ` Eric Schulte
  2008-07-25 19:07   ` Eric Schulte
  2 siblings, 0 replies; 19+ messages in thread
From: Eric Schulte @ 2008-07-25 17:14 UTC (permalink / raw)
  To: James TD Smith; +Cc: Org-mode

Thanks, both for the code, and the instructions

it worked on the first try!

On Friday, July 25, at 17:25, James TD Smith wrote:
 > On 2008-07-25 08:53:31(-0700), Eric Schulte wrote:
 > > 
 > > Any advice for quick graphing of a table in org-mode?
 > > 
 > 
 > I have a setup for plotting data from tables. I'm not sure if it's exactly what
 > you want, but yoy may find it useful.
 > 
 > 1. Add the following to your .emacs:
 > 
 > (defun ahkt-plot-table (script)
 >   "util function to export and plot a table using the supplied
 > gnuplot `script'"
 >   (org-table-export)
 >   (let ((cbuf (current-buffer))
 > 	(cwin (selected-window)))
 >     (save-restriction
 >       (save-excursion
 > 	(find-file script)
 > 	(gnuplot-send-buffer-to-gnuplot)
 > 	(bury-buffer)
 > 	(bury-buffer (get-buffer "*gnuplot*"))))
 >     (and (window-live-p cwin) (select-window cwin))
 >     (switch-to-buffer cbuf)
 >     (delete-other-windows)))
 > 
 > 2. Create a gnuplot script which plots data from a file.
 > 
 > 3. Add the following properties to the headline containing the table.
 > TABLE_EXPORT_FILE <filename in the gnuploy script>
 > TABLE_EXPORT_FORMAT orgtbl-to-generic :skip 4 :splice t :sep "\t"
 > 
 > 4. Add an org link in the table (it must be in the table otherwise the export
 > doesn't work) as below:
 > [[elisp:(ahkt-plot-table "<gnuplot script>")][plot table]] 
 > 
 > I suggest you put it at the top of the table.
 > You will then need to adjust the 'skip' parameter in the export format depending
 > on the number of lines at the top of the table which should not be exported
 > (hlines, more than one plotting link etc). 
 > 
 > 5. You should then be able to open the link, and get a plot of the table
 > contents.
 > 
 > 
 > --
 > |-<James TD Smith>-<email/ahktenzero@mohorovi.cc>-|
 > 
 > 
 > _______________________________________________
 > Emacs-orgmode mailing list
 > Remember: use `Reply All' to send replies to the list.
 > Emacs-orgmode@gnu.org
 > http://lists.gnu.org/mailman/listinfo/emacs-orgmode

-- 
schulte

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: graphing from org-tables
  2008-07-25 16:25 ` James TD Smith
  2008-07-25 16:33   ` Carsten Dominik
  2008-07-25 17:14   ` Eric Schulte
@ 2008-07-25 19:07   ` Eric Schulte
  2008-07-26 18:15     ` Dan Davison
       [not found]     ` <C8C13077-8B16-4E8C-8425-5782CE1EDC98@uva.nl>
  2 siblings, 2 replies; 19+ messages in thread
From: Eric Schulte @ 2008-07-25 19:07 UTC (permalink / raw)
  To: James TD Smith; +Cc: Org-mode


I had some time waiting for things to execute, so I condensed your
process into a single command (borrowing heavily from
org-export-table).

(defun org-table/gnuplot (&optional x-col)
  "Plot the current table using gnuplot.  Use a prefix argument
to specify a column to use for the x-coordinates, to use the row
number for the x-coordinates provide a prefix argument of 0."
  (interactive "p")
  (message (format "%S" x-col))
  (unless (org-at-table-p)
    (error "No table at point"))
  (require 'org-exp)
  (require 'gnuplot)
  (org-table-align) ;; make sure we have everything we need
  (let* ((beg (org-table-begin))
	 (end (org-table-end))
	 (cols (save-excursion
		 (goto-char end)
		 (backward-char 3)
		 (org-table-current-column)))
	 (data-beg (if (and 
			(goto-char beg)
			(re-search-forward org-table-dataline-regexp end t)
			(re-search-forward org-table-hline-regexp end t)
			(re-search-forward org-table-dataline-regexp end t))
		       (match-beginning 0)
		     beg))
	 (skip (- (line-number-at-pos data-beg) (line-number-at-pos beg)))
	 (exp-format (format "orgtbl-to-tsv :skip %d" skip))
	 (file (make-temp-file "org-table-plot")))
    ;; export table
    (org-table-export file exp-format)
    (with-temp-buffer
      ;; write script
      (insert (org-table/gnuplot-script file x-col cols))
      ;; graph table
      (gnuplot-mode)
      (gnuplot-send-buffer-to-gnuplot)
      (bury-buffer (get-buffer "*gnuplot*")))
    (delete-file file)))

(defun org-table/gnuplot-script (file x-col num-cols)
  (let ((plot-str "'%s' using %s:%d with lines title '%d'");; "\\\n    ,"
	script)
    (dotimes (col (+ 1 num-cols))
      (unless (or (and x-col (equal col x-col)) (equal col 0))
	(setf script (cons (format plot-str file (or (and x-col (format "%d" x-col)) "") col col) script))))
    (concat "plot " (mapconcat 'identity (reverse script) "\\\n    ,"))))

On Friday, July 25, at 17:25, James TD Smith wrote:
 > On 2008-07-25 08:53:31(-0700), Eric Schulte wrote:
 > > 
 > > Any advice for quick graphing of a table in org-mode?
 > > 
 > 
 > I have a setup for plotting data from tables. I'm not sure if it's exactly what
 > you want, but yoy may find it useful.
 > 
 > 1. Add the following to your .emacs:
 > 
 > (defun ahkt-plot-table (script)
 >   "util function to export and plot a table using the supplied
 > gnuplot `script'"
 >   (org-table-export)
 >   (let ((cbuf (current-buffer))
 > 	(cwin (selected-window)))
 >     (save-restriction
 >       (save-excursion
 > 	(find-file script)
 > 	(gnuplot-send-buffer-to-gnuplot)
 > 	(bury-buffer)
 > 	(bury-buffer (get-buffer "*gnuplot*"))))
 >     (and (window-live-p cwin) (select-window cwin))
 >     (switch-to-buffer cbuf)
 >     (delete-other-windows)))
 > 
 > 2. Create a gnuplot script which plots data from a file.
 > 
 > 3. Add the following properties to the headline containing the table.
 > TABLE_EXPORT_FILE <filename in the gnuploy script>
 > TABLE_EXPORT_FORMAT orgtbl-to-generic :skip 4 :splice t :sep "\t"
 > 
 > 4. Add an org link in the table (it must be in the table otherwise the export
 > doesn't work) as below:
 > [[elisp:(ahkt-plot-table "<gnuplot script>")][plot table]] 
 > 
 > I suggest you put it at the top of the table.
 > You will then need to adjust the 'skip' parameter in the export format depending
 > on the number of lines at the top of the table which should not be exported
 > (hlines, more than one plotting link etc). 
 > 
 > 5. You should then be able to open the link, and get a plot of the table
 > contents.
 > 
 > 
 > --
 > |-<James TD Smith>-<email/ahktenzero@mohorovi.cc>-|
 > 
 > 
 > _______________________________________________
 > Emacs-orgmode mailing list
 > Remember: use `Reply All' to send replies to the list.
 > Emacs-orgmode@gnu.org
 > http://lists.gnu.org/mailman/listinfo/emacs-orgmode

-- 
schulte

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: graphing from org-tables
  2008-07-25 19:07   ` Eric Schulte
@ 2008-07-26 18:15     ` Dan Davison
  2008-07-28 15:32       ` Eric Schulte
       [not found]     ` <C8C13077-8B16-4E8C-8425-5782CE1EDC98@uva.nl>
  1 sibling, 1 reply; 19+ messages in thread
From: Dan Davison @ 2008-07-26 18:15 UTC (permalink / raw)
  To: Eric Schulte; +Cc: Org-mode

R (www.r-project.org) is pretty good for data plotting and statistical
analyses. Here's my effort at the org-table-plot function, using
R. Since R contains a csv importer that can read from stdin, it's
pretty simple. I've tried to code it so that you can provide an
arbitrary R function as the optional argument, so in principle you can
do to your org-table anything that R is capable of in the realms of
data analysis and visualisation.

(defun org-table-R-plot (&optional R-function)
"Plot the current table using R. The table is transformed into a dataframe in R. Optional
argument R-function is a string which is either the name of an R
function, or an anonymous function definition of the form (function(d) {...}),
requiring a single argument (the dataframe). The default is to use
the R function 'plot' which produces scatter plots of all pairwise
combinations of columns. An example custom plotting function is:
* plot column 3 against column 1, adding least-squares linear regression fit in blue                                                                                                                               (function(df) { plot(x=df[,1], y=df[,3]) ; abline(lm(df[,3] ~ df[,1]), col=\"blue\") })                                                                                                                      "
  (interactive)
  (unless R-function (set 'R-function "plot"))
  (let ((file (make-temp-file "org-table-R-plot")))
    (org-table-export file "orgtbl-to-csv")
    (set-buffer (find-file-noselect file))
    (shell-command-on-region
     (point-min) (point-max)
     (concat "Rscript -e 'X11() ; " R-function "(read.csv(\"stdin\")) ; system(\"sleep 60\")'"))
    (delete-file file)))


R is at www.r-project.org 
(package r-base on ubuntu/debian)

Rscript is a command line non-interactive scripting utility that is
bundled automatically with the R installation. I reckon it'll be OK on
OSX but no idea about Windows.  My function doesn't have to be used
for plotting; the R-function argument can be any function operating on
the data from the org-table, producing numerical or graphical output.

There's several things that need to be sorted out with my function, e.g.

(i) I haven't worked out how to return control to the emacs process
while keeping the plot window there. I tried adding an & to the
shell-command, but that seemed to result in R receiving nothing on
stdin. So I've got that 'sleep 60' hack in there currently; use C-g if
you get bored of your plot.

(ii) If the R-function isn't doing graphics, then the call to X11()
gets in the way. X11() would only work on linux/mac OSX(?) anyway.

(iii) I'm afraid I don't even know yet how to pass the optional string
argument using M-x org-table-R-plot. Is it possible with some sort of
prefix argument, and an (interactive something) declaration? Anyway,
it seems to work if you evaluate e.g.

(org-table-R-plot "(function(df) { plot(x=df[,1], y=df[,3]) ; abline(lm(df[,3] ~ df[,1]), col=\"blue\") })")

Suggestions for improvements welcome!

Dan



On Fri, Jul 25, 2008 at 12:07:00PM -0700, Eric Schulte wrote:
> 
> I had some time waiting for things to execute, so I condensed your
> process into a single command (borrowing heavily from
> org-export-table).
> 
> (defun org-table/gnuplot (&optional x-col)
>   "Plot the current table using gnuplot.  Use a prefix argument
> to specify a column to use for the x-coordinates, to use the row
> number for the x-coordinates provide a prefix argument of 0."
>   (interactive "p")
>   (message (format "%S" x-col))
>   (unless (org-at-table-p)
>     (error "No table at point"))
>   (require 'org-exp)
>   (require 'gnuplot)
>   (org-table-align) ;; make sure we have everything we need
>   (let* ((beg (org-table-begin))
> 	 (end (org-table-end))
> 	 (cols (save-excursion
> 		 (goto-char end)
> 		 (backward-char 3)
> 		 (org-table-current-column)))
> 	 (data-beg (if (and 
> 			(goto-char beg)
> 			(re-search-forward org-table-dataline-regexp end t)
> 			(re-search-forward org-table-hline-regexp end t)
> 			(re-search-forward org-table-dataline-regexp end t))
> 		       (match-beginning 0)
> 		     beg))
> 	 (skip (- (line-number-at-pos data-beg) (line-number-at-pos beg)))
> 	 (exp-format (format "orgtbl-to-tsv :skip %d" skip))
> 	 (file (make-temp-file "org-table-plot")))
>     ;; export table
>     (org-table-export file exp-format)
>     (with-temp-buffer
>       ;; write script
>       (insert (org-table/gnuplot-script file x-col cols))
>       ;; graph table
>       (gnuplot-mode)
>       (gnuplot-send-buffer-to-gnuplot)
>       (bury-buffer (get-buffer "*gnuplot*")))
>     (delete-file file)))
> 
> (defun org-table/gnuplot-script (file x-col num-cols)
>   (let ((plot-str "'%s' using %s:%d with lines title '%d'");; "\\\n    ,"
> 	script)
>     (dotimes (col (+ 1 num-cols))
>       (unless (or (and x-col (equal col x-col)) (equal col 0))
> 	(setf script (cons (format plot-str file (or (and x-col (format "%d" x-col)) "") col col) script))))
>     (concat "plot " (mapconcat 'identity (reverse script) "\\\n    ,"))))
> 
> On Friday, July 25, at 17:25, James TD Smith wrote:
>  > On 2008-07-25 08:53:31(-0700), Eric Schulte wrote:
>  > > 
>  > > Any advice for quick graphing of a table in org-mode?
>  > > 
>  > 
>  > I have a setup for plotting data from tables. I'm not sure if it's exactly what
>  > you want, but yoy may find it useful.
>  > 
>  > 1. Add the following to your .emacs:
>  > 
>  > (defun ahkt-plot-table (script)
>  >   "util function to export and plot a table using the supplied
>  > gnuplot `script'"
>  >   (org-table-export)
>  >   (let ((cbuf (current-buffer))
>  > 	(cwin (selected-window)))
>  >     (save-restriction
>  >       (save-excursion
>  > 	(find-file script)
>  > 	(gnuplot-send-buffer-to-gnuplot)
>  > 	(bury-buffer)
>  > 	(bury-buffer (get-buffer "*gnuplot*"))))
>  >     (and (window-live-p cwin) (select-window cwin))
>  >     (switch-to-buffer cbuf)
>  >     (delete-other-windows)))
>  > 
>  > 2. Create a gnuplot script which plots data from a file.
>  > 
>  > 3. Add the following properties to the headline containing the table.
>  > TABLE_EXPORT_FILE <filename in the gnuploy script>
>  > TABLE_EXPORT_FORMAT orgtbl-to-generic :skip 4 :splice t :sep "\t"
>  > 
>  > 4. Add an org link in the table (it must be in the table otherwise the export
>  > doesn't work) as below:
>  > [[elisp:(ahkt-plot-table "<gnuplot script>")][plot table]] 
>  > 
>  > I suggest you put it at the top of the table.
>  > You will then need to adjust the 'skip' parameter in the export format depending
>  > on the number of lines at the top of the table which should not be exported
>  > (hlines, more than one plotting link etc). 
>  > 
>  > 5. You should then be able to open the link, and get a plot of the table
>  > contents.
>  > 
>  > 
>  > --
>  > |-<James TD Smith>-<email/ahktenzero@mohorovi.cc>-|
>  > 
>  > 
>  > _______________________________________________
>  > Emacs-orgmode mailing list
>  > Remember: use `Reply All' to send replies to the list.
>  > Emacs-orgmode@gnu.org
>  > http://lists.gnu.org/mailman/listinfo/emacs-orgmode
> 
> -- 
> schulte
> 
> 
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: graphing from org-tables
       [not found]         ` <9629760B-7606-42FD-B625-FAC44490221C@uva.nl>
@ 2008-07-28 14:26           ` Eric Schulte
  2008-07-28 21:00             ` Carsten Dominik
  0 siblings, 1 reply; 19+ messages in thread
From: Eric Schulte @ 2008-07-28 14:26 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: Org-mode

It seems that the ability to generate graphs/plots from org files may
be general enough to be useful as either an addition to org-table or
as a new org-plot library.  Carsten has suggested the addition of a
new gnuplot (or maybe R-plot or just plot) link type to be used for
plotting

I would be happy to help write up such a library, but would like to
first push some discussion of what some of the potential uses would
be.  Here are some seed questions/thoughts...

1) Would it ever make sense to graph/plot information which is not in
   an org-table, if so how would that information be stored?
  - function or equation in the gnuplot link
  - array of values in the link
  - latex function (cringe)
  - elsewhere
2) It seems that it may be useful to automatically generate/update
   plots during publication of org-files to latex or html
3) Currently I find it very useful to be able to see an initial plot
   of a table with a single command, however there should be a sliding
   scale from ease of plotting to greater control over the final plot.
   Maybe through specification of gnuplot options through properties,
   or simply specification of a gnuplot script.
4) also clearly R is another plotting library of interest, how should
   different libraries be handled.
5) would application of more powerful libraries like R to org-tables
   be useful as an alternative to calc, or is this beginning to look
   like a kitchen sink

I'm sure there are many other interesting places where this discussion
could lead.  Please see emails below for the genesis of these ideas,
and share your own thoughts.

Thanks -- Eric

On Saturday, July 26, at 13:21, Carsten Dominik wrote:
 > 
 > On Jul 26, 2008, at 12:36 PM, Eric Schulte wrote:
 > 
 > > I am happy to collaborate,
 > >
 > > Ideas for expansion mentioned so far include;
 > >
 > >>> I can think of a couple of extensions which should be worthwhile
 > >>> 1) titling the graph lines, by their column headers (when present)
 > >>> 2) exposing some of the gnuplot options (with lines, with bars,  
 > >>> title,
 > >>>  lt, etc...), the only problem there is how to expose them without
 > >>>  cluttering the interface
 > >>> 3) probably there are some more which would be useful...
 > >>
 > >>
 > >> The best way to make this extensible is to use a property list as a
 > >> parameter to the function that creates the plot.
 > >>
 > >>
 > >> like this:
 > >>
 > >> (defun org-table-gnuplot (params)
 > >>
 > >> and params can be a property list of arbitrary size with properties  
 > >> like
 > >>
 > >> :title "string"
 > >> :xcol N
 > >> :ycols (n1 n2 n3)
 > >> :labelrow N
 > >>
 > >> etc etc.  This you could use to expose any amount of gnuplot options
 > >> you would want.
 > >>
 > >> Other ideas:
 > >>
 > >> - I think it might be better if the link can be just before or  
 > >> after a
 > >> table, so we could make the function search forward to the next table
 > >> and use that.
 > >>
 > >> - instead of using elisp links, we could define a new link type
 > >> gnuplot: to do this.  Not required, but might make things more  
 > >> compact
 > >> and clean.
 > >
 > > Also it looks like this thread is still alive in the mailing list, so
 > > maybe discussion of features/extensions should be moved there?
 > >
 > > What do you think?
 > 
 > Absolutely!
 > 
 > With the new post from Dan, maybe we can create an org-plot.el  
 > extension that will do all kinds of crazy things.
 > 
 > Please go ahead and move our discussion back into the mailing list -  
 > I'll let you do it because I will be out of the game for a month soon.
 > 
 > - Carsten
 > 
 > >
 > >
 > > Thanks -- Eric
 > 

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: graphing from org-tables
  2008-07-26 18:15     ` Dan Davison
@ 2008-07-28 15:32       ` Eric Schulte
  2008-07-28 15:41         ` Dan Davison
  0 siblings, 1 reply; 19+ messages in thread
From: Eric Schulte @ 2008-07-28 15:32 UTC (permalink / raw)
  To: Dan Davison; +Cc: Org-mode

Hi Dan,

One way around the sleep(60) hack may be to create the R graph using
an inferior R process.  See
http://cran.r-project.org/doc/FAQ/R-FAQ.html#R-and-Emacs for
information on running R functions from inside of Emacs.

Best -- Eric

On Saturday, July 26, at 19:15, Dan Davison wrote:
 > R (www.r-project.org) is pretty good for data plotting and statistical
 > analyses. Here's my effort at the org-table-plot function, using
 > R. Since R contains a csv importer that can read from stdin, it's
 > pretty simple. I've tried to code it so that you can provide an
 > arbitrary R function as the optional argument, so in principle you can
 > do to your org-table anything that R is capable of in the realms of
 > data analysis and visualisation.
 > 
 > (defun org-table-R-plot (&optional R-function)
 > "Plot the current table using R. The table is transformed into a dataframe in R. Optional
 > argument R-function is a string which is either the name of an R
 > function, or an anonymous function definition of the form (function(d) {...}),
 > requiring a single argument (the dataframe). The default is to use
 > the R function 'plot' which produces scatter plots of all pairwise
 > combinations of columns. An example custom plotting function is:
 > * plot column 3 against column 1, adding least-squares linear regression fit in blue                                                                                                                               (function(df) { plot(x=df[,1], y=df[,3]) ; abline(lm(df[,3] ~ df[,1]), col=\"blue\") })                                                                                                                      "
 >   (interactive)
 >   (unless R-function (set 'R-function "plot"))
 >   (let ((file (make-temp-file "org-table-R-plot")))
 >     (org-table-export file "orgtbl-to-csv")
 >     (set-buffer (find-file-noselect file))
 >     (shell-command-on-region
 >      (point-min) (point-max)
 >      (concat "Rscript -e 'X11() ; " R-function "(read.csv(\"stdin\")) ; system(\"sleep 60\")'"))
 >     (delete-file file)))
 > 
 > 
 > R is at www.r-project.org 
 > (package r-base on ubuntu/debian)
 > 
 > Rscript is a command line non-interactive scripting utility that is
 > bundled automatically with the R installation. I reckon it'll be OK on
 > OSX but no idea about Windows.  My function doesn't have to be used
 > for plotting; the R-function argument can be any function operating on
 > the data from the org-table, producing numerical or graphical output.
 > 
 > There's several things that need to be sorted out with my function, e.g.
 > 
 > (i) I haven't worked out how to return control to the emacs process
 > while keeping the plot window there. I tried adding an & to the
 > shell-command, but that seemed to result in R receiving nothing on
 > stdin. So I've got that 'sleep 60' hack in there currently; use C-g if
 > you get bored of your plot.
 > 
 > (ii) If the R-function isn't doing graphics, then the call to X11()
 > gets in the way. X11() would only work on linux/mac OSX(?) anyway.
 > 
 > (iii) I'm afraid I don't even know yet how to pass the optional string
 > argument using M-x org-table-R-plot. Is it possible with some sort of
 > prefix argument, and an (interactive something) declaration? Anyway,
 > it seems to work if you evaluate e.g.
 > 
 > (org-table-R-plot "(function(df) { plot(x=df[,1], y=df[,3]) ; abline(lm(df[,3] ~ df[,1]), col=\"blue\") })")
 > 
 > Suggestions for improvements welcome!
 > 
 > Dan
 > 
 > 
 > 
 > On Fri, Jul 25, 2008 at 12:07:00PM -0700, Eric Schulte wrote:
 > > 
 > > I had some time waiting for things to execute, so I condensed your
 > > process into a single command (borrowing heavily from
 > > org-export-table).
 > > 
 > > (defun org-table/gnuplot (&optional x-col)
 > >   "Plot the current table using gnuplot.  Use a prefix argument
 > > to specify a column to use for the x-coordinates, to use the row
 > > number for the x-coordinates provide a prefix argument of 0."
 > >   (interactive "p")
 > >   (message (format "%S" x-col))
 > >   (unless (org-at-table-p)
 > >     (error "No table at point"))
 > >   (require 'org-exp)
 > >   (require 'gnuplot)
 > >   (org-table-align) ;; make sure we have everything we need
 > >   (let* ((beg (org-table-begin))
 > > 	 (end (org-table-end))
 > > 	 (cols (save-excursion
 > > 		 (goto-char end)
 > > 		 (backward-char 3)
 > > 		 (org-table-current-column)))
 > > 	 (data-beg (if (and 
 > > 			(goto-char beg)
 > > 			(re-search-forward org-table-dataline-regexp end t)
 > > 			(re-search-forward org-table-hline-regexp end t)
 > > 			(re-search-forward org-table-dataline-regexp end t))
 > > 		       (match-beginning 0)
 > > 		     beg))
 > > 	 (skip (- (line-number-at-pos data-beg) (line-number-at-pos beg)))
 > > 	 (exp-format (format "orgtbl-to-tsv :skip %d" skip))
 > > 	 (file (make-temp-file "org-table-plot")))
 > >     ;; export table
 > >     (org-table-export file exp-format)
 > >     (with-temp-buffer
 > >       ;; write script
 > >       (insert (org-table/gnuplot-script file x-col cols))
 > >       ;; graph table
 > >       (gnuplot-mode)
 > >       (gnuplot-send-buffer-to-gnuplot)
 > >       (bury-buffer (get-buffer "*gnuplot*")))
 > >     (delete-file file)))
 > > 
 > > (defun org-table/gnuplot-script (file x-col num-cols)
 > >   (let ((plot-str "'%s' using %s:%d with lines title '%d'");; "\\\n    ,"
 > > 	script)
 > >     (dotimes (col (+ 1 num-cols))
 > >       (unless (or (and x-col (equal col x-col)) (equal col 0))
 > > 	(setf script (cons (format plot-str file (or (and x-col (format "%d" x-col)) "") col col) script))))
 > >     (concat "plot " (mapconcat 'identity (reverse script) "\\\n    ,"))))
 > > 
 > > On Friday, July 25, at 17:25, James TD Smith wrote:
 > >  > On 2008-07-25 08:53:31(-0700), Eric Schulte wrote:
 > >  > > 
 > >  > > Any advice for quick graphing of a table in org-mode?
 > >  > > 
 > >  > 
 > >  > I have a setup for plotting data from tables. I'm not sure if it's exactly what
 > >  > you want, but yoy may find it useful.
 > >  > 
 > >  > 1. Add the following to your .emacs:
 > >  > 
 > >  > (defun ahkt-plot-table (script)
 > >  >   "util function to export and plot a table using the supplied
 > >  > gnuplot `script'"
 > >  >   (org-table-export)
 > >  >   (let ((cbuf (current-buffer))
 > >  > 	(cwin (selected-window)))
 > >  >     (save-restriction
 > >  >       (save-excursion
 > >  > 	(find-file script)
 > >  > 	(gnuplot-send-buffer-to-gnuplot)
 > >  > 	(bury-buffer)
 > >  > 	(bury-buffer (get-buffer "*gnuplot*"))))
 > >  >     (and (window-live-p cwin) (select-window cwin))
 > >  >     (switch-to-buffer cbuf)
 > >  >     (delete-other-windows)))
 > >  > 
 > >  > 2. Create a gnuplot script which plots data from a file.
 > >  > 
 > >  > 3. Add the following properties to the headline containing the table.
 > >  > TABLE_EXPORT_FILE <filename in the gnuploy script>
 > >  > TABLE_EXPORT_FORMAT orgtbl-to-generic :skip 4 :splice t :sep "\t"
 > >  > 
 > >  > 4. Add an org link in the table (it must be in the table otherwise the export
 > >  > doesn't work) as below:
 > >  > [[elisp:(ahkt-plot-table "<gnuplot script>")][plot table]] 
 > >  > 
 > >  > I suggest you put it at the top of the table.
 > >  > You will then need to adjust the 'skip' parameter in the export format depending
 > >  > on the number of lines at the top of the table which should not be exported
 > >  > (hlines, more than one plotting link etc). 
 > >  > 
 > >  > 5. You should then be able to open the link, and get a plot of the table
 > >  > contents.
 > >  > 
 > >  > 
 > >  > --
 > >  > |-<James TD Smith>-<email/ahktenzero@mohorovi.cc>-|
 > >  > 
 > >  > 
 > >  > _______________________________________________
 > >  > Emacs-orgmode mailing list
 > >  > Remember: use `Reply All' to send replies to the list.
 > >  > Emacs-orgmode@gnu.org
 > >  > http://lists.gnu.org/mailman/listinfo/emacs-orgmode
 > > 
 > > -- 
 > > schulte
 > > 
 > > 
 > > _______________________________________________
 > > Emacs-orgmode mailing list
 > > Remember: use `Reply All' to send replies to the list.
 > > Emacs-orgmode@gnu.org
 > > http://lists.gnu.org/mailman/listinfo/emacs-orgmode

-- 
schulte

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: graphing from org-tables
  2008-07-28 15:32       ` Eric Schulte
@ 2008-07-28 15:41         ` Dan Davison
  2008-12-21 20:31           ` Dan Davison
  0 siblings, 1 reply; 19+ messages in thread
From: Dan Davison @ 2008-07-28 15:41 UTC (permalink / raw)
  To: Eric Schulte; +Cc: Org-mode

On Mon, Jul 28, 2008 at 08:32:22AM -0700, Eric Schulte wrote:
> Hi Dan,
> 
> One way around the sleep(60) hack may be to create the R graph using
> an inferior R process.  See

Yep, I just this minute asked for help on the Emacs Speaks Statistics
(ESS) mailing list! I'll report back if I make progress with that.

https://stat.ethz.ch/pipermail/ess-help/2008-July/004785.html

Dan




> http://cran.r-project.org/doc/FAQ/R-FAQ.html#R-and-Emacs for
> information on running R functions from inside of Emacs.
> 
> Best -- Eric
> 
> On Saturday, July 26, at 19:15, Dan Davison wrote:
>  > R (www.r-project.org) is pretty good for data plotting and statistical
>  > analyses. Here's my effort at the org-table-plot function, using
>  > R. Since R contains a csv importer that can read from stdin, it's
>  > pretty simple. I've tried to code it so that you can provide an
>  > arbitrary R function as the optional argument, so in principle you can
>  > do to your org-table anything that R is capable of in the realms of
>  > data analysis and visualisation.
>  > 
>  > (defun org-table-R-plot (&optional R-function)
>  > "Plot the current table using R. The table is transformed into a dataframe in R. Optional
>  > argument R-function is a string which is either the name of an R
>  > function, or an anonymous function definition of the form (function(d) {...}),
>  > requiring a single argument (the dataframe). The default is to use
>  > the R function 'plot' which produces scatter plots of all pairwise
>  > combinations of columns. An example custom plotting function is:
>  > * plot column 3 against column 1, adding least-squares linear regression fit in blue                                                                                                                               (function(df) { plot(x=df[,1], y=df[,3]) ; abline(lm(df[,3] ~ df[,1]), col=\"blue\") })                                                                                                                      "
>  >   (interactive)
>  >   (unless R-function (set 'R-function "plot"))
>  >   (let ((file (make-temp-file "org-table-R-plot")))
>  >     (org-table-export file "orgtbl-to-csv")
>  >     (set-buffer (find-file-noselect file))
>  >     (shell-command-on-region
>  >      (point-min) (point-max)
>  >      (concat "Rscript -e 'X11() ; " R-function "(read.csv(\"stdin\")) ; system(\"sleep 60\")'"))
>  >     (delete-file file)))
>  > 
>  > 
>  > R is at www.r-project.org 
>  > (package r-base on ubuntu/debian)
>  > 
>  > Rscript is a command line non-interactive scripting utility that is
>  > bundled automatically with the R installation. I reckon it'll be OK on
>  > OSX but no idea about Windows.  My function doesn't have to be used
>  > for plotting; the R-function argument can be any function operating on
>  > the data from the org-table, producing numerical or graphical output.
>  > 
>  > There's several things that need to be sorted out with my function, e.g.
>  > 
>  > (i) I haven't worked out how to return control to the emacs process
>  > while keeping the plot window there. I tried adding an & to the
>  > shell-command, but that seemed to result in R receiving nothing on
>  > stdin. So I've got that 'sleep 60' hack in there currently; use C-g if
>  > you get bored of your plot.
>  > 
>  > (ii) If the R-function isn't doing graphics, then the call to X11()
>  > gets in the way. X11() would only work on linux/mac OSX(?) anyway.
>  > 
>  > (iii) I'm afraid I don't even know yet how to pass the optional string
>  > argument using M-x org-table-R-plot. Is it possible with some sort of
>  > prefix argument, and an (interactive something) declaration? Anyway,
>  > it seems to work if you evaluate e.g.
>  > 
>  > (org-table-R-plot "(function(df) { plot(x=df[,1], y=df[,3]) ; abline(lm(df[,3] ~ df[,1]), col=\"blue\") })")
>  > 
>  > Suggestions for improvements welcome!
>  > 
>  > Dan
>  > 
>  > 
>  > 
>  > On Fri, Jul 25, 2008 at 12:07:00PM -0700, Eric Schulte wrote:
>  > > 
>  > > I had some time waiting for things to execute, so I condensed your
>  > > process into a single command (borrowing heavily from
>  > > org-export-table).
>  > > 
>  > > (defun org-table/gnuplot (&optional x-col)
>  > >   "Plot the current table using gnuplot.  Use a prefix argument
>  > > to specify a column to use for the x-coordinates, to use the row
>  > > number for the x-coordinates provide a prefix argument of 0."
>  > >   (interactive "p")
>  > >   (message (format "%S" x-col))
>  > >   (unless (org-at-table-p)
>  > >     (error "No table at point"))
>  > >   (require 'org-exp)
>  > >   (require 'gnuplot)
>  > >   (org-table-align) ;; make sure we have everything we need
>  > >   (let* ((beg (org-table-begin))
>  > > 	 (end (org-table-end))
>  > > 	 (cols (save-excursion
>  > > 		 (goto-char end)
>  > > 		 (backward-char 3)
>  > > 		 (org-table-current-column)))
>  > > 	 (data-beg (if (and 
>  > > 			(goto-char beg)
>  > > 			(re-search-forward org-table-dataline-regexp end t)
>  > > 			(re-search-forward org-table-hline-regexp end t)
>  > > 			(re-search-forward org-table-dataline-regexp end t))
>  > > 		       (match-beginning 0)
>  > > 		     beg))
>  > > 	 (skip (- (line-number-at-pos data-beg) (line-number-at-pos beg)))
>  > > 	 (exp-format (format "orgtbl-to-tsv :skip %d" skip))
>  > > 	 (file (make-temp-file "org-table-plot")))
>  > >     ;; export table
>  > >     (org-table-export file exp-format)
>  > >     (with-temp-buffer
>  > >       ;; write script
>  > >       (insert (org-table/gnuplot-script file x-col cols))
>  > >       ;; graph table
>  > >       (gnuplot-mode)
>  > >       (gnuplot-send-buffer-to-gnuplot)
>  > >       (bury-buffer (get-buffer "*gnuplot*")))
>  > >     (delete-file file)))
>  > > 
>  > > (defun org-table/gnuplot-script (file x-col num-cols)
>  > >   (let ((plot-str "'%s' using %s:%d with lines title '%d'");; "\\\n    ,"
>  > > 	script)
>  > >     (dotimes (col (+ 1 num-cols))
>  > >       (unless (or (and x-col (equal col x-col)) (equal col 0))
>  > > 	(setf script (cons (format plot-str file (or (and x-col (format "%d" x-col)) "") col col) script))))
>  > >     (concat "plot " (mapconcat 'identity (reverse script) "\\\n    ,"))))
>  > > 
>  > > On Friday, July 25, at 17:25, James TD Smith wrote:
>  > >  > On 2008-07-25 08:53:31(-0700), Eric Schulte wrote:
>  > >  > > 
>  > >  > > Any advice for quick graphing of a table in org-mode?
>  > >  > > 
>  > >  > 
>  > >  > I have a setup for plotting data from tables. I'm not sure if it's exactly what
>  > >  > you want, but yoy may find it useful.
>  > >  > 
>  > >  > 1. Add the following to your .emacs:
>  > >  > 
>  > >  > (defun ahkt-plot-table (script)
>  > >  >   "util function to export and plot a table using the supplied
>  > >  > gnuplot `script'"
>  > >  >   (org-table-export)
>  > >  >   (let ((cbuf (current-buffer))
>  > >  > 	(cwin (selected-window)))
>  > >  >     (save-restriction
>  > >  >       (save-excursion
>  > >  > 	(find-file script)
>  > >  > 	(gnuplot-send-buffer-to-gnuplot)
>  > >  > 	(bury-buffer)
>  > >  > 	(bury-buffer (get-buffer "*gnuplot*"))))
>  > >  >     (and (window-live-p cwin) (select-window cwin))
>  > >  >     (switch-to-buffer cbuf)
>  > >  >     (delete-other-windows)))
>  > >  > 
>  > >  > 2. Create a gnuplot script which plots data from a file.
>  > >  > 
>  > >  > 3. Add the following properties to the headline containing the table.
>  > >  > TABLE_EXPORT_FILE <filename in the gnuploy script>
>  > >  > TABLE_EXPORT_FORMAT orgtbl-to-generic :skip 4 :splice t :sep "\t"
>  > >  > 
>  > >  > 4. Add an org link in the table (it must be in the table otherwise the export
>  > >  > doesn't work) as below:
>  > >  > [[elisp:(ahkt-plot-table "<gnuplot script>")][plot table]] 
>  > >  > 
>  > >  > I suggest you put it at the top of the table.
>  > >  > You will then need to adjust the 'skip' parameter in the export format depending
>  > >  > on the number of lines at the top of the table which should not be exported
>  > >  > (hlines, more than one plotting link etc). 
>  > >  > 
>  > >  > 5. You should then be able to open the link, and get a plot of the table
>  > >  > contents.
>  > >  > 
>  > >  > 
>  > >  > --
>  > >  > |-<James TD Smith>-<email/ahktenzero@mohorovi.cc>-|
>  > >  > 
>  > >  > 
>  > >  > _______________________________________________
>  > >  > Emacs-orgmode mailing list
>  > >  > Remember: use `Reply All' to send replies to the list.
>  > >  > Emacs-orgmode@gnu.org
>  > >  > http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>  > > 
>  > > -- 
>  > > schulte
>  > > 
>  > > 
>  > > _______________________________________________
>  > > Emacs-orgmode mailing list
>  > > Remember: use `Reply All' to send replies to the list.
>  > > Emacs-orgmode@gnu.org
>  > > http://lists.gnu.org/mailman/listinfo/emacs-orgmode
> 
> -- 
> schulte

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: graphing from org-tables
  2008-07-28 14:26           ` Eric Schulte
@ 2008-07-28 21:00             ` Carsten Dominik
  0 siblings, 0 replies; 19+ messages in thread
From: Carsten Dominik @ 2008-07-28 21:00 UTC (permalink / raw)
  To: Eric Schulte; +Cc: Org-mode


On Jul 28, 2008, at 7:26 AM, Eric Schulte wrote:
> 3) Currently I find it very useful to be able to see an initial plot
>   of a table with a single command, however there should be a sliding
>   scale from ease of plotting to greater control over the final plot.
>   Maybe through specification of gnuplot options through properties,
>   or simply specification of a gnuplot script.

First of all, I do believe it would be good to move the plotting
settings to outside the table, either before or after.

I can see good ways to specify parameters for a plot that would be quite
consistent with how Org deals with such things in similar situations.

1. A special comment line in the buffer, directly before the table, like

#+PLOT: table-to-gnuplot :xcol 1 :ycols (2 4 5 6) :title "Some string"

or

#+PLOT: table-to-R :script "/path/to/script"

The command can read these by macthing

(if (looking-at "#+PLOT: +\\([^ \t\r\n]+\\)\\( +.*\\)?")
     (let* ((func (intern (match-string 1)))
	   (params-plist
	    (if (match-end 2)
		(read (concat "(" (match-string 2 format) ")")))))

2. Use inherited properties to specify plotting parameters in a tree.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: graphing from org-tables
  2008-07-28 15:41         ` Dan Davison
@ 2008-12-21 20:31           ` Dan Davison
  2008-12-22 17:00             ` Eric Schulte
  0 siblings, 1 reply; 19+ messages in thread
From: Dan Davison @ 2008-12-21 20:31 UTC (permalink / raw)
  To: Eric Schulte; +Cc: Org-mode

On Mon, Jul 28, 2008 at 04:41:52PM +0100, Dan Davison wrote:
> On Mon, Jul 28, 2008 at 08:32:22AM -0700, Eric Schulte wrote:
> > Hi Dan,
> > 
> > One way around the sleep(60) hack may be to create the R graph using
> > an inferior R process.  See
> 
> Yep, I just this minute asked for help on the Emacs Speaks Statistics
> (ESS) mailing list! I'll report back if I make progress with that.
> 
> https://stat.ethz.ch/pipermail/ess-help/2008-July/004785.html

I think it's worth keeping this avenue alive, despite so much progress
having been made with gnuplot in the interim. So below is an initial
version of org-table-eval-R, which takes an arbitrary R function, and
applies it to an org table. As suggested above, this uses
inferior-ess-mode and therefore requires ess (emacs speaks statistics)
and R to be installed.

At it's simplest, R-function can be the name of a standard R
function. So with Eric's first example data set on Worg
(http://legito.net/worg/org-tutorials/org-plot.php) i.e. with 2
dependent variables

(org-table-eval-R "summary") produces (in a new buffer)

 first.dependent.var second.dependent.var
 Min.   :0.1560      Min.   :0.1808      
 1st Qu.:0.1773      1st Qu.:0.1985      
 Median :0.1900      Median :0.2146      
 Mean   :0.2110      Mean   :0.2262      
 3rd Qu.:0.2333      3rd Qu.:0.2375      
 Max.   :0.4250      Max.   :0.3750      

and (org-table-eval-R "matplot") produces a rough version of Eric's
gnuplot figure. (see below for a closer reproduction of the figure,
with the legend etc).

It would be simple to provide org-plot/R which would construct a
suitable plotting function and pass it to org-table-eval-R, and some
ready-made R code could be provided for standard plots -- histograms &
barplots, scatter plots, etc. I've given an initial version of
org-plot/R below, which gives the option to produce eps output rather
than a pop-up window (and it would be easy to use any of the R
graphics devices for output: jpeg, png, ps, pdf, tiff, X11, quartz,
etc)

But the power of this approach is that it provides not just plotting
functionality, but the ability to do arbitrary calculations and
statistical analyses of the org table, via the base R packages and the
hundreds of user-written packages.

It would have been better to have developed these ideas along with the
development of org-plot, but unfortunately I let that slip. But
org-table-eval-R provides a lot of power for anyone that's prepared to
write some R code. And if there's interest, then some more
user-friendly functionality could be provided for org users that want
something easier, but can install ess and R.

Dan

and here's a rough version of the "O R G" grid plot

(org-table-eval-R "(function (x) image(t(as.matrix(x))))" t)

Of course, these can be beautified to your heart's content with lots
of extra arguments to the R plotting functions. This comes a bit
closer to the two dependent-variables graph:

(org-table-eval-R "(function(x) { matplot(x, type=\"l\", ylab=\"\") ; legend(\"topright\", legend=c(\"dep var 1\"\,\"dep var 2\"), col=1:2, lty=1:2) })")



--------------------------------------------------------------
(defun org-table-eval-R (R-function &optional rownames-absent)
  "Apply an arbitrary R function to the org table at
point. R-FUNCTION is an R function (a string) taking one
argument, which is the R data frame representation of the org
table. It may be the name of an appropriate R
function (e.g. \"summary\", \"plot\"), or a user-defined
anonymous function of the form \"(function(data.frame)
{...})\". Unless ROWNAMES-ABSENT is non-nil the row names of the
data frame are taken from the first column of the org table"
  (interactive)
  (require 'ess)
  (let ((csv-file (make-temp-file "org-table-eval-R-")))
    (org-table-export csv-file "orgtbl-to-csv")
    (ess-execute (org-table-eval-R-make-expr 
		  R-function csv-file rownames-absent))
    (delete-file csv-file)))

(defun org-table-eval-R-make-expr (R-function csv-file rownames-absent)
  (concat R-function
	  "(read.csv(\"" csv-file "\""
	  (unless rownames-absent ", row.names=1") "))"))


(defun org-plot/R (&optional plot-function file rownames-absent)
  "Use R to create a plot using the org table at point. If FILE
is non-nil then the plot will be written to file (currently as
eps, in future the format could easily be determined by the file
extension.) Unless ROWNAMES-ABSENT is non-nil the row names will
be taken from the first column of the org table."
  (interactive)
  (require 'ess)
  (org-table-eval-R (or plot-function "plot") rownames-absent)
  (if file (ess-execute (concat "dev.copy2eps(\"" file "\")"))))




> 
> Dan
> 
> 
> 
> 
> > http://cran.r-project.org/doc/FAQ/R-FAQ.html#R-and-Emacs for
> > information on running R functions from inside of Emacs.
> > 
> > Best -- Eric
> > 
> > On Saturday, July 26, at 19:15, Dan Davison wrote:
> >  > R (www.r-project.org) is pretty good for data plotting and statistical
> >  > analyses. Here's my effort at the org-table-plot function, using
> >  > R. Since R contains a csv importer that can read from stdin, it's
> >  > pretty simple. I've tried to code it so that you can provide an
> >  > arbitrary R function as the optional argument, so in principle you can
> >  > do to your org-table anything that R is capable of in the realms of
> >  > data analysis and visualisation.
> >  > 
> >  > (defun org-table-R-plot (&optional R-function)
> >  > "Plot the current table using R. The table is transformed into a dataframe in R. Optional
> >  > argument R-function is a string which is either the name of an R
> >  > function, or an anonymous function definition of the form (function(d) {...}),
> >  > requiring a single argument (the dataframe). The default is to use
> >  > the R function 'plot' which produces scatter plots of all pairwise
> >  > combinations of columns. An example custom plotting function is:
> >  > * plot column 3 against column 1, adding least-squares linear regression fit in blue                                                                                                                               (function(df) { plot(x=df[,1], y=df[,3]) ; abline(lm(df[,3] ~ df[,1]), col=\"blue\") })                                                                                                                      "
> >  >   (interactive)
> >  >   (unless R-function (set 'R-function "plot"))
> >  >   (let ((file (make-temp-file "org-table-R-plot")))
> >  >     (org-table-export file "orgtbl-to-csv")
> >  >     (set-buffer (find-file-noselect file))
> >  >     (shell-command-on-region
> >  >      (point-min) (point-max)
> >  >      (concat "Rscript -e 'X11() ; " R-function "(read.csv(\"stdin\")) ; system(\"sleep 60\")'"))
> >  >     (delete-file file)))
> >  > 
> >  > 
> >  > R is at www.r-project.org 
> >  > (package r-base on ubuntu/debian)
> >  > 
> >  > Rscript is a command line non-interactive scripting utility that is
> >  > bundled automatically with the R installation. I reckon it'll be OK on
> >  > OSX but no idea about Windows.  My function doesn't have to be used
> >  > for plotting; the R-function argument can be any function operating on
> >  > the data from the org-table, producing numerical or graphical output.
> >  > 
> >  > There's several things that need to be sorted out with my function, e.g.
> >  > 
> >  > (i) I haven't worked out how to return control to the emacs process
> >  > while keeping the plot window there. I tried adding an & to the
> >  > shell-command, but that seemed to result in R receiving nothing on
> >  > stdin. So I've got that 'sleep 60' hack in there currently; use C-g if
> >  > you get bored of your plot.
> >  > 
> >  > (ii) If the R-function isn't doing graphics, then the call to X11()
> >  > gets in the way. X11() would only work on linux/mac OSX(?) anyway.
> >  > 
> >  > (iii) I'm afraid I don't even know yet how to pass the optional string
> >  > argument using M-x org-table-R-plot. Is it possible with some sort of
> >  > prefix argument, and an (interactive something) declaration? Anyway,
> >  > it seems to work if you evaluate e.g.
> >  > 
> >  > (org-table-R-plot "(function(df) { plot(x=df[,1], y=df[,3]) ; abline(lm(df[,3] ~ df[,1]), col=\"blue\") })")
> >  > 
> >  > Suggestions for improvements welcome!
> >  > 
> >  > Dan
> >  > 
> >  > 
> >  > 
> >  > On Fri, Jul 25, 2008 at 12:07:00PM -0700, Eric Schulte wrote:
> >  > > 
> >  > > I had some time waiting for things to execute, so I condensed your
> >  > > process into a single command (borrowing heavily from
> >  > > org-export-table).
> >  > > 
> >  > > (defun org-table/gnuplot (&optional x-col)
> >  > >   "Plot the current table using gnuplot.  Use a prefix argument
> >  > > to specify a column to use for the x-coordinates, to use the row
> >  > > number for the x-coordinates provide a prefix argument of 0."
> >  > >   (interactive "p")
> >  > >   (message (format "%S" x-col))
> >  > >   (unless (org-at-table-p)
> >  > >     (error "No table at point"))
> >  > >   (require 'org-exp)
> >  > >   (require 'gnuplot)
> >  > >   (org-table-align) ;; make sure we have everything we need
> >  > >   (let* ((beg (org-table-begin))
> >  > > 	 (end (org-table-end))
> >  > > 	 (cols (save-excursion
> >  > > 		 (goto-char end)
> >  > > 		 (backward-char 3)
> >  > > 		 (org-table-current-column)))
> >  > > 	 (data-beg (if (and 
> >  > > 			(goto-char beg)
> >  > > 			(re-search-forward org-table-dataline-regexp end t)
> >  > > 			(re-search-forward org-table-hline-regexp end t)
> >  > > 			(re-search-forward org-table-dataline-regexp end t))
> >  > > 		       (match-beginning 0)
> >  > > 		     beg))
> >  > > 	 (skip (- (line-number-at-pos data-beg) (line-number-at-pos beg)))
> >  > > 	 (exp-format (format "orgtbl-to-tsv :skip %d" skip))
> >  > > 	 (file (make-temp-file "org-table-plot")))
> >  > >     ;; export table
> >  > >     (org-table-export file exp-format)
> >  > >     (with-temp-buffer
> >  > >       ;; write script
> >  > >       (insert (org-table/gnuplot-script file x-col cols))
> >  > >       ;; graph table
> >  > >       (gnuplot-mode)
> >  > >       (gnuplot-send-buffer-to-gnuplot)
> >  > >       (bury-buffer (get-buffer "*gnuplot*")))
> >  > >     (delete-file file)))
> >  > > 
> >  > > (defun org-table/gnuplot-script (file x-col num-cols)
> >  > >   (let ((plot-str "'%s' using %s:%d with lines title '%d'");; "\\\n    ,"
> >  > > 	script)
> >  > >     (dotimes (col (+ 1 num-cols))
> >  > >       (unless (or (and x-col (equal col x-col)) (equal col 0))
> >  > > 	(setf script (cons (format plot-str file (or (and x-col (format "%d" x-col)) "") col col) script))))
> >  > >     (concat "plot " (mapconcat 'identity (reverse script) "\\\n    ,"))))
> >  > > 
> >  > > On Friday, July 25, at 17:25, James TD Smith wrote:
> >  > >  > On 2008-07-25 08:53:31(-0700), Eric Schulte wrote:
> >  > >  > > 
> >  > >  > > Any advice for quick graphing of a table in org-mode?
> >  > >  > > 
> >  > >  > 
> >  > >  > I have a setup for plotting data from tables. I'm not sure if it's exactly what
> >  > >  > you want, but yoy may find it useful.
> >  > >  > 
> >  > >  > 1. Add the following to your .emacs:
> >  > >  > 
> >  > >  > (defun ahkt-plot-table (script)
> >  > >  >   "util function to export and plot a table using the supplied
> >  > >  > gnuplot `script'"
> >  > >  >   (org-table-export)
> >  > >  >   (let ((cbuf (current-buffer))
> >  > >  > 	(cwin (selected-window)))
> >  > >  >     (save-restriction
> >  > >  >       (save-excursion
> >  > >  > 	(find-file script)
> >  > >  > 	(gnuplot-send-buffer-to-gnuplot)
> >  > >  > 	(bury-buffer)
> >  > >  > 	(bury-buffer (get-buffer "*gnuplot*"))))
> >  > >  >     (and (window-live-p cwin) (select-window cwin))
> >  > >  >     (switch-to-buffer cbuf)
> >  > >  >     (delete-other-windows)))
> >  > >  > 
> >  > >  > 2. Create a gnuplot script which plots data from a file.
> >  > >  > 
> >  > >  > 3. Add the following properties to the headline containing the table.
> >  > >  > TABLE_EXPORT_FILE <filename in the gnuploy script>
> >  > >  > TABLE_EXPORT_FORMAT orgtbl-to-generic :skip 4 :splice t :sep "\t"
> >  > >  > 
> >  > >  > 4. Add an org link in the table (it must be in the table otherwise the export
> >  > >  > doesn't work) as below:
> >  > >  > [[elisp:(ahkt-plot-table "<gnuplot script>")][plot table]] 
> >  > >  > 
> >  > >  > I suggest you put it at the top of the table.
> >  > >  > You will then need to adjust the 'skip' parameter in the export format depending
> >  > >  > on the number of lines at the top of the table which should not be exported
> >  > >  > (hlines, more than one plotting link etc). 
> >  > >  > 
> >  > >  > 5. You should then be able to open the link, and get a plot of the table
> >  > >  > contents.
> >  > >  > 
> >  > >  > 
> >  > >  > --
> >  > >  > |-<James TD Smith>-<email/ahktenzero@mohorovi.cc>-|
> >  > >  > 
> >  > >  > 
> >  > >  > _______________________________________________
> >  > >  > Emacs-orgmode mailing list
> >  > >  > Remember: use `Reply All' to send replies to the list.
> >  > >  > Emacs-orgmode@gnu.org
> >  > >  > http://lists.gnu.org/mailman/listinfo/emacs-orgmode
> >  > > 
> >  > > -- 
> >  > > schulte
> >  > > 
> >  > > 
> >  > > _______________________________________________
> >  > > Emacs-orgmode mailing list
> >  > > Remember: use `Reply All' to send replies to the list.
> >  > > Emacs-orgmode@gnu.org
> >  > > http://lists.gnu.org/mailman/listinfo/emacs-orgmode
> > 
> > -- 
> > schulte
> 
> 
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

-- 
http://www.stats.ox.ac.uk/~davison

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: graphing from org-tables
  2008-12-21 20:31           ` Dan Davison
@ 2008-12-22 17:00             ` Eric Schulte
  2009-01-22 16:27               ` Dan Davison
  0 siblings, 1 reply; 19+ messages in thread
From: Eric Schulte @ 2008-12-22 17:00 UTC (permalink / raw)
  To: Dan Davison; +Cc: Org-mode

Hi Dan,

This looks very interesting, and I look forward to playing around with
these functions (when I have some/any free time).

After a quick glance it seems like these functions could be expanded in
two different directions...

1) Adapted to a simple calling mechanism like the one used for org-plot
   which relies solely on a couple of parameters stuffed into a #+PLOT
   line next to a table.  I think this would be a good option for org
   users who are more comfortable w/R than w/gnuplot.

2) Also interesting is the idea of referencing a table from a block of R
   code elsewhere in the org file.  I've worked some on processing
   blocks of R code in org files in a manner similar to Sweave.
   http://github.com/eschulte/org-contrib/tree/master/org-exp-blocks.el
   If we could implement a simple means of referencing tables (either
   whole tables, or cells/cols/rows of tables) from these R-code blocks
   then that seems like it may be fairly powerful.

Thanks -- Eric

Dan Davison <davison@stats.ox.ac.uk> writes:

> On Mon, Jul 28, 2008 at 04:41:52PM +0100, Dan Davison wrote:
>> On Mon, Jul 28, 2008 at 08:32:22AM -0700, Eric Schulte wrote:
>> > Hi Dan,
>> > 
>> > One way around the sleep(60) hack may be to create the R graph using
>> > an inferior R process.  See
>> 
>> Yep, I just this minute asked for help on the Emacs Speaks Statistics
>> (ESS) mailing list! I'll report back if I make progress with that.
>> 
>> https://stat.ethz.ch/pipermail/ess-help/2008-July/004785.html
>
> I think it's worth keeping this avenue alive, despite so much progress
> having been made with gnuplot in the interim. So below is an initial
> version of org-table-eval-R, which takes an arbitrary R function, and
> applies it to an org table. As suggested above, this uses
> inferior-ess-mode and therefore requires ess (emacs speaks statistics)
> and R to be installed.
>
> At it's simplest, R-function can be the name of a standard R
> function. So with Eric's first example data set on Worg
> (http://legito.net/worg/org-tutorials/org-plot.php) i.e. with 2
> dependent variables
>
> (org-table-eval-R "summary") produces (in a new buffer)
>
>  first.dependent.var second.dependent.var
>  Min.   :0.1560      Min.   :0.1808      
>  1st Qu.:0.1773      1st Qu.:0.1985      
>  Median :0.1900      Median :0.2146      
>  Mean   :0.2110      Mean   :0.2262      
>  3rd Qu.:0.2333      3rd Qu.:0.2375      
>  Max.   :0.4250      Max.   :0.3750      
>
> and (org-table-eval-R "matplot") produces a rough version of Eric's
> gnuplot figure. (see below for a closer reproduction of the figure,
> with the legend etc).
>
> It would be simple to provide org-plot/R which would construct a
> suitable plotting function and pass it to org-table-eval-R, and some
> ready-made R code could be provided for standard plots -- histograms &
> barplots, scatter plots, etc. I've given an initial version of
> org-plot/R below, which gives the option to produce eps output rather
> than a pop-up window (and it would be easy to use any of the R
> graphics devices for output: jpeg, png, ps, pdf, tiff, X11, quartz,
> etc)
>
> But the power of this approach is that it provides not just plotting
> functionality, but the ability to do arbitrary calculations and
> statistical analyses of the org table, via the base R packages and the
> hundreds of user-written packages.
>
> It would have been better to have developed these ideas along with the
> development of org-plot, but unfortunately I let that slip. But
> org-table-eval-R provides a lot of power for anyone that's prepared to
> write some R code. And if there's interest, then some more
> user-friendly functionality could be provided for org users that want
> something easier, but can install ess and R.
>
> Dan
>
> and here's a rough version of the "O R G" grid plot
>
> (org-table-eval-R "(function (x) image(t(as.matrix(x))))" t)
>
> Of course, these can be beautified to your heart's content with lots
> of extra arguments to the R plotting functions. This comes a bit
> closer to the two dependent-variables graph:
>
> (org-table-eval-R "(function(x) { matplot(x, type=\"l\", ylab=\"\") ; legend(\"topright\", legend=c(\"dep var 1\"\,\"dep var 2\"), col=1:2, lty=1:2) })")
>
>
>
> --------------------------------------------------------------
> (defun org-table-eval-R (R-function &optional rownames-absent)
>   "Apply an arbitrary R function to the org table at
> point. R-FUNCTION is an R function (a string) taking one
> argument, which is the R data frame representation of the org
> table. It may be the name of an appropriate R
> function (e.g. \"summary\", \"plot\"), or a user-defined
> anonymous function of the form \"(function(data.frame)
> {...})\". Unless ROWNAMES-ABSENT is non-nil the row names of the
> data frame are taken from the first column of the org table"
>   (interactive)
>   (require 'ess)
>   (let ((csv-file (make-temp-file "org-table-eval-R-")))
>     (org-table-export csv-file "orgtbl-to-csv")
>     (ess-execute (org-table-eval-R-make-expr 
> 		  R-function csv-file rownames-absent))
>     (delete-file csv-file)))
>
> (defun org-table-eval-R-make-expr (R-function csv-file rownames-absent)
>   (concat R-function
> 	  "(read.csv(\"" csv-file "\""
> 	  (unless rownames-absent ", row.names=1") "))"))
>
>
> (defun org-plot/R (&optional plot-function file rownames-absent)
>   "Use R to create a plot using the org table at point. If FILE
> is non-nil then the plot will be written to file (currently as
> eps, in future the format could easily be determined by the file
> extension.) Unless ROWNAMES-ABSENT is non-nil the row names will
> be taken from the first column of the org table."
>   (interactive)
>   (require 'ess)
>   (org-table-eval-R (or plot-function "plot") rownames-absent)
>   (if file (ess-execute (concat "dev.copy2eps(\"" file "\")"))))
>
>
>
>
>> 
>> Dan
>> 
>> 
>> 
>> 
>> > http://cran.r-project.org/doc/FAQ/R-FAQ.html#R-and-Emacs for
>> > information on running R functions from inside of Emacs.
>> > 
>> > Best -- Eric
>> > 
>> > On Saturday, July 26, at 19:15, Dan Davison wrote:
>> >  > R (www.r-project.org) is pretty good for data plotting and statistical
>> >  > analyses. Here's my effort at the org-table-plot function, using
>> >  > R. Since R contains a csv importer that can read from stdin, it's
>> >  > pretty simple. I've tried to code it so that you can provide an
>> >  > arbitrary R function as the optional argument, so in principle you can
>> >  > do to your org-table anything that R is capable of in the realms of
>> >  > data analysis and visualisation.
>> >  > 
>> >  > (defun org-table-R-plot (&optional R-function)
>> >  > "Plot the current table using R. The table is transformed into a dataframe in R. Optional
>> >  > argument R-function is a string which is either the name of an R
>> >  > function, or an anonymous function definition of the form (function(d) {...}),
>> >  > requiring a single argument (the dataframe). The default is to use
>> >  > the R function 'plot' which produces scatter plots of all pairwise
>> >  > combinations of columns. An example custom plotting function is:
>> >  > * plot column 3 against column 1, adding least-squares linear regression fit in blue                                                                                                                               (function(df) { plot(x=df[,1], y=df[,3]) ; abline(lm(df[,3] ~ df[,1]), col=\"blue\") })                                                                                                                      "
>> >  >   (interactive)
>> >  >   (unless R-function (set 'R-function "plot"))
>> >  >   (let ((file (make-temp-file "org-table-R-plot")))
>> >  >     (org-table-export file "orgtbl-to-csv")
>> >  >     (set-buffer (find-file-noselect file))
>> >  >     (shell-command-on-region
>> >  >      (point-min) (point-max)
>> >  >      (concat "Rscript -e 'X11() ; " R-function "(read.csv(\"stdin\")) ; system(\"sleep 60\")'"))
>> >  >     (delete-file file)))
>> >  > 
>> >  > 
>> >  > R is at www.r-project.org 
>> >  > (package r-base on ubuntu/debian)
>> >  > 
>> >  > Rscript is a command line non-interactive scripting utility that is
>> >  > bundled automatically with the R installation. I reckon it'll be OK on
>> >  > OSX but no idea about Windows.  My function doesn't have to be used
>> >  > for plotting; the R-function argument can be any function operating on
>> >  > the data from the org-table, producing numerical or graphical output.
>> >  > 
>> >  > There's several things that need to be sorted out with my function, e.g.
>> >  > 
>> >  > (i) I haven't worked out how to return control to the emacs process
>> >  > while keeping the plot window there. I tried adding an & to the
>> >  > shell-command, but that seemed to result in R receiving nothing on
>> >  > stdin. So I've got that 'sleep 60' hack in there currently; use C-g if
>> >  > you get bored of your plot.
>> >  > 
>> >  > (ii) If the R-function isn't doing graphics, then the call to X11()
>> >  > gets in the way. X11() would only work on linux/mac OSX(?) anyway.
>> >  > 
>> >  > (iii) I'm afraid I don't even know yet how to pass the optional string
>> >  > argument using M-x org-table-R-plot. Is it possible with some sort of
>> >  > prefix argument, and an (interactive something) declaration? Anyway,
>> >  > it seems to work if you evaluate e.g.
>> >  > 
>> >  > (org-table-R-plot "(function(df) { plot(x=df[,1], y=df[,3]) ; abline(lm(df[,3] ~ df[,1]), col=\"blue\") })")
>> >  > 
>> >  > Suggestions for improvements welcome!
>> >  > 
>> >  > Dan
>> >  > 
>> >  > 
>> >  > 
>> >  > On Fri, Jul 25, 2008 at 12:07:00PM -0700, Eric Schulte wrote:
>> >  > > 
>> >  > > I had some time waiting for things to execute, so I condensed your
>> >  > > process into a single command (borrowing heavily from
>> >  > > org-export-table).
>> >  > > 
>> >  > > (defun org-table/gnuplot (&optional x-col)
>> >  > >   "Plot the current table using gnuplot.  Use a prefix argument
>> >  > > to specify a column to use for the x-coordinates, to use the row
>> >  > > number for the x-coordinates provide a prefix argument of 0."
>> >  > >   (interactive "p")
>> >  > >   (message (format "%S" x-col))
>> >  > >   (unless (org-at-table-p)
>> >  > >     (error "No table at point"))
>> >  > >   (require 'org-exp)
>> >  > >   (require 'gnuplot)
>> >  > >   (org-table-align) ;; make sure we have everything we need
>> >  > >   (let* ((beg (org-table-begin))
>> >  > > 	 (end (org-table-end))
>> >  > > 	 (cols (save-excursion
>> >  > > 		 (goto-char end)
>> >  > > 		 (backward-char 3)
>> >  > > 		 (org-table-current-column)))
>> >  > > 	 (data-beg (if (and 
>> >  > > 			(goto-char beg)
>> >  > > 			(re-search-forward org-table-dataline-regexp end t)
>> >  > > 			(re-search-forward org-table-hline-regexp end t)
>> >  > > 			(re-search-forward org-table-dataline-regexp end t))
>> >  > > 		       (match-beginning 0)
>> >  > > 		     beg))
>> >  > > 	 (skip (- (line-number-at-pos data-beg) (line-number-at-pos beg)))
>> >  > > 	 (exp-format (format "orgtbl-to-tsv :skip %d" skip))
>> >  > > 	 (file (make-temp-file "org-table-plot")))
>> >  > >     ;; export table
>> >  > >     (org-table-export file exp-format)
>> >  > >     (with-temp-buffer
>> >  > >       ;; write script
>> >  > >       (insert (org-table/gnuplot-script file x-col cols))
>> >  > >       ;; graph table
>> >  > >       (gnuplot-mode)
>> >  > >       (gnuplot-send-buffer-to-gnuplot)
>> >  > >       (bury-buffer (get-buffer "*gnuplot*")))
>> >  > >     (delete-file file)))
>> >  > > 
>> >  > > (defun org-table/gnuplot-script (file x-col num-cols)
>> >  > >   (let ((plot-str "'%s' using %s:%d with lines title '%d'");; "\\\n    ,"
>> >  > > 	script)
>> >  > >     (dotimes (col (+ 1 num-cols))
>> >  > >       (unless (or (and x-col (equal col x-col)) (equal col 0))
>> >  > > 	(setf script (cons (format plot-str file (or (and x-col (format "%d" x-col)) "") col col) script))))
>> >  > >     (concat "plot " (mapconcat 'identity (reverse script) "\\\n    ,"))))
>> >  > > 
>> >  > > On Friday, July 25, at 17:25, James TD Smith wrote:
>> >  > >  > On 2008-07-25 08:53:31(-0700), Eric Schulte wrote:
>> >  > >  > > 
>> >  > >  > > Any advice for quick graphing of a table in org-mode?
>> >  > >  > > 
>> >  > >  > 
>> >  > >  > I have a setup for plotting data from tables. I'm not sure if it's exactly what
>> >  > >  > you want, but yoy may find it useful.
>> >  > >  > 
>> >  > >  > 1. Add the following to your .emacs:
>> >  > >  > 
>> >  > >  > (defun ahkt-plot-table (script)
>> >  > >  >   "util function to export and plot a table using the supplied
>> >  > >  > gnuplot `script'"
>> >  > >  >   (org-table-export)
>> >  > >  >   (let ((cbuf (current-buffer))
>> >  > >  > 	(cwin (selected-window)))
>> >  > >  >     (save-restriction
>> >  > >  >       (save-excursion
>> >  > >  > 	(find-file script)
>> >  > >  > 	(gnuplot-send-buffer-to-gnuplot)
>> >  > >  > 	(bury-buffer)
>> >  > >  > 	(bury-buffer (get-buffer "*gnuplot*"))))
>> >  > >  >     (and (window-live-p cwin) (select-window cwin))
>> >  > >  >     (switch-to-buffer cbuf)
>> >  > >  >     (delete-other-windows)))
>> >  > >  > 
>> >  > >  > 2. Create a gnuplot script which plots data from a file.
>> >  > >  > 
>> >  > >  > 3. Add the following properties to the headline containing the table.
>> >  > >  > TABLE_EXPORT_FILE <filename in the gnuploy script>
>> >  > >  > TABLE_EXPORT_FORMAT orgtbl-to-generic :skip 4 :splice t :sep "\t"
>> >  > >  > 
>> >  > >  > 4. Add an org link in the table (it must be in the table otherwise the export
>> >  > >  > doesn't work) as below:
>> >  > >  > [[elisp:(ahkt-plot-table "<gnuplot script>")][plot table]] 
>> >  > >  > 
>> >  > >  > I suggest you put it at the top of the table.
>> >  > >  > You will then need to adjust the 'skip' parameter in the export format depending
>> >  > >  > on the number of lines at the top of the table which should not be exported
>> >  > >  > (hlines, more than one plotting link etc). 
>> >  > >  > 
>> >  > >  > 5. You should then be able to open the link, and get a plot of the table
>> >  > >  > contents.
>> >  > >  > 
>> >  > >  > 
>> >  > >  > --
>> >  > >  > |-<James TD Smith>-<email/ahktenzero@mohorovi.cc>-|
>> >  > >  > 
>> >  > >  > 
>> >  > >  > _______________________________________________
>> >  > >  > Emacs-orgmode mailing list
>> >  > >  > Remember: use `Reply All' to send replies to the list.
>> >  > >  > Emacs-orgmode@gnu.org
>> >  > >  > http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>> >  > > 
>> >  > > -- 
>> >  > > schulte
>> >  > > 
>> >  > > 
>> >  > > _______________________________________________
>> >  > > Emacs-orgmode mailing list
>> >  > > Remember: use `Reply All' to send replies to the list.
>> >  > > Emacs-orgmode@gnu.org
>> >  > > http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>> > 
>> > -- 
>> > schulte
>> 
>> 
>> _______________________________________________
>> Emacs-orgmode mailing list
>> Remember: use `Reply All' to send replies to the list.
>> Emacs-orgmode@gnu.org
>> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: graphing from org-tables
  2008-12-22 17:00             ` Eric Schulte
@ 2009-01-22 16:27               ` Dan Davison
  2009-01-23  1:37                 ` Eric Schulte
  0 siblings, 1 reply; 19+ messages in thread
From: Dan Davison @ 2009-01-22 16:27 UTC (permalink / raw)
  To: emacs-orgmode

On Mon, Dec 22, 2008 at 12:00:14PM -0500, Eric Schulte wrote:
> Hi Dan,
> 

<...>
 
> 2) Also interesting is the idea of referencing a table from a block of R
>    code elsewhere in the org file.  I've worked some on processing
>    blocks of R code in org files in a manner similar to Sweave.
>    http://github.com/eschulte/org-contrib/tree/master/org-exp-blocks.el
>    If we could implement a simple means of referencing tables (either
>    whole tables, or cells/cols/rows of tables) from these R-code blocks
>    then that seems like it may be fairly powerful.

I agree with this. So in the case of my code, I think we would like to
be able to do something like

#+TBLR: data:<org-table-reference> columns:(1 2) action:tabulate

This would make the output of the analysis/plot of the table pointed
to by <org-table-reference> appear at the location in the file of the
#+TBLR line. Perhaps one design aim would be to have the same
mechanism work for referencing tables for processing by #TBLFM,
org-plot, org-table-R, org-exp-blocks, etc. What ideas to people have
about implementation (and syntax) for this sort of table referencing?

Dan


> 
> Thanks -- Eric
> 
> Dan Davison <davison@stats.ox.ac.uk> writes:
> 
> > On Mon, Jul 28, 2008 at 04:41:52PM +0100, Dan Davison wrote:
> >> On Mon, Jul 28, 2008 at 08:32:22AM -0700, Eric Schulte wrote:
> >> > Hi Dan,
> >> > 
> >> > One way around the sleep(60) hack may be to create the R graph using
> >> > an inferior R process.  See
> >> 
> >> Yep, I just this minute asked for help on the Emacs Speaks Statistics
> >> (ESS) mailing list! I'll report back if I make progress with that.
> >> 
> >> https://stat.ethz.ch/pipermail/ess-help/2008-July/004785.html
> >
> > I think it's worth keeping this avenue alive, despite so much progress
> > having been made with gnuplot in the interim. So below is an initial
> > version of org-table-eval-R, which takes an arbitrary R function, and
> > applies it to an org table. As suggested above, this uses
> > inferior-ess-mode and therefore requires ess (emacs speaks statistics)
> > and R to be installed.
> >
> > At it's simplest, R-function can be the name of a standard R
> > function. So with Eric's first example data set on Worg
> > (http://legito.net/worg/org-tutorials/org-plot.php) i.e. with 2
> > dependent variables
> >
> > (org-table-eval-R "summary") produces (in a new buffer)
> >
> >  first.dependent.var second.dependent.var
> >  Min.   :0.1560      Min.   :0.1808      
> >  1st Qu.:0.1773      1st Qu.:0.1985      
> >  Median :0.1900      Median :0.2146      
> >  Mean   :0.2110      Mean   :0.2262      
> >  3rd Qu.:0.2333      3rd Qu.:0.2375      
> >  Max.   :0.4250      Max.   :0.3750      
> >
> > and (org-table-eval-R "matplot") produces a rough version of Eric's
> > gnuplot figure. (see below for a closer reproduction of the figure,
> > with the legend etc).
> >
> > It would be simple to provide org-plot/R which would construct a
> > suitable plotting function and pass it to org-table-eval-R, and some
> > ready-made R code could be provided for standard plots -- histograms &
> > barplots, scatter plots, etc. I've given an initial version of
> > org-plot/R below, which gives the option to produce eps output rather
> > than a pop-up window (and it would be easy to use any of the R
> > graphics devices for output: jpeg, png, ps, pdf, tiff, X11, quartz,
> > etc)
> >
> > But the power of this approach is that it provides not just plotting
> > functionality, but the ability to do arbitrary calculations and
> > statistical analyses of the org table, via the base R packages and the
> > hundreds of user-written packages.
> >
> > It would have been better to have developed these ideas along with the
> > development of org-plot, but unfortunately I let that slip. But
> > org-table-eval-R provides a lot of power for anyone that's prepared to
> > write some R code. And if there's interest, then some more
> > user-friendly functionality could be provided for org users that want
> > something easier, but can install ess and R.
> >
> > Dan
> >
> > and here's a rough version of the "O R G" grid plot
> >
> > (org-table-eval-R "(function (x) image(t(as.matrix(x))))" t)
> >
> > Of course, these can be beautified to your heart's content with lots
> > of extra arguments to the R plotting functions. This comes a bit
> > closer to the two dependent-variables graph:
> >
> > (org-table-eval-R "(function(x) { matplot(x, type=\"l\", ylab=\"\") ; legend(\"topright\", legend=c(\"dep var 1\"\,\"dep var 2\"), col=1:2, lty=1:2) })")
> >
> >
> >
> > --------------------------------------------------------------
> > (defun org-table-eval-R (R-function &optional rownames-absent)
> >   "Apply an arbitrary R function to the org table at
> > point. R-FUNCTION is an R function (a string) taking one
> > argument, which is the R data frame representation of the org
> > table. It may be the name of an appropriate R
> > function (e.g. \"summary\", \"plot\"), or a user-defined
> > anonymous function of the form \"(function(data.frame)
> > {...})\". Unless ROWNAMES-ABSENT is non-nil the row names of the
> > data frame are taken from the first column of the org table"
> >   (interactive)
> >   (require 'ess)
> >   (let ((csv-file (make-temp-file "org-table-eval-R-")))
> >     (org-table-export csv-file "orgtbl-to-csv")
> >     (ess-execute (org-table-eval-R-make-expr 
> > 		  R-function csv-file rownames-absent))
> >     (delete-file csv-file)))
> >
> > (defun org-table-eval-R-make-expr (R-function csv-file rownames-absent)
> >   (concat R-function
> > 	  "(read.csv(\"" csv-file "\""
> > 	  (unless rownames-absent ", row.names=1") "))"))
> >
> >
> > (defun org-plot/R (&optional plot-function file rownames-absent)
> >   "Use R to create a plot using the org table at point. If FILE
> > is non-nil then the plot will be written to file (currently as
> > eps, in future the format could easily be determined by the file
> > extension.) Unless ROWNAMES-ABSENT is non-nil the row names will
> > be taken from the first column of the org table."
> >   (interactive)
> >   (require 'ess)
> >   (org-table-eval-R (or plot-function "plot") rownames-absent)
> >   (if file (ess-execute (concat "dev.copy2eps(\"" file "\")"))))
> >
> >
> >
> >
> >> 
> >> Dan
> >> 
> >> 
> >> 
> >> 
> >> > http://cran.r-project.org/doc/FAQ/R-FAQ.html#R-and-Emacs for
> >> > information on running R functions from inside of Emacs.
> >> > 
> >> > Best -- Eric
> >> > 
> >> > On Saturday, July 26, at 19:15, Dan Davison wrote:
> >> >  > R (www.r-project.org) is pretty good for data plotting and statistical
> >> >  > analyses. Here's my effort at the org-table-plot function, using
> >> >  > R. Since R contains a csv importer that can read from stdin, it's
> >> >  > pretty simple. I've tried to code it so that you can provide an
> >> >  > arbitrary R function as the optional argument, so in principle you can
> >> >  > do to your org-table anything that R is capable of in the realms of
> >> >  > data analysis and visualisation.
> >> >  > 
> >> >  > (defun org-table-R-plot (&optional R-function)
> >> >  > "Plot the current table using R. The table is transformed into a dataframe in R. Optional
> >> >  > argument R-function is a string which is either the name of an R
> >> >  > function, or an anonymous function definition of the form (function(d) {...}),
> >> >  > requiring a single argument (the dataframe). The default is to use
> >> >  > the R function 'plot' which produces scatter plots of all pairwise
> >> >  > combinations of columns. An example custom plotting function is:
> >> >  > * plot column 3 against column 1, adding least-squares linear regression fit in blue                                                                                                                               (function(df) { plot(x=df[,1], y=df[,3]) ; abline(lm(df[,3] ~ df[,1]), col=\"blue\") })                                                                                                                      "
> >> >  >   (interactive)
> >> >  >   (unless R-function (set 'R-function "plot"))
> >> >  >   (let ((file (make-temp-file "org-table-R-plot")))
> >> >  >     (org-table-export file "orgtbl-to-csv")
> >> >  >     (set-buffer (find-file-noselect file))
> >> >  >     (shell-command-on-region
> >> >  >      (point-min) (point-max)
> >> >  >      (concat "Rscript -e 'X11() ; " R-function "(read.csv(\"stdin\")) ; system(\"sleep 60\")'"))
> >> >  >     (delete-file file)))
> >> >  > 
> >> >  > 
> >> >  > R is at www.r-project.org 
> >> >  > (package r-base on ubuntu/debian)
> >> >  > 
> >> >  > Rscript is a command line non-interactive scripting utility that is
> >> >  > bundled automatically with the R installation. I reckon it'll be OK on
> >> >  > OSX but no idea about Windows.  My function doesn't have to be used
> >> >  > for plotting; the R-function argument can be any function operating on
> >> >  > the data from the org-table, producing numerical or graphical output.
> >> >  > 
> >> >  > There's several things that need to be sorted out with my function, e.g.
> >> >  > 
> >> >  > (i) I haven't worked out how to return control to the emacs process
> >> >  > while keeping the plot window there. I tried adding an & to the
> >> >  > shell-command, but that seemed to result in R receiving nothing on
> >> >  > stdin. So I've got that 'sleep 60' hack in there currently; use C-g if
> >> >  > you get bored of your plot.
> >> >  > 
> >> >  > (ii) If the R-function isn't doing graphics, then the call to X11()
> >> >  > gets in the way. X11() would only work on linux/mac OSX(?) anyway.
> >> >  > 
> >> >  > (iii) I'm afraid I don't even know yet how to pass the optional string
> >> >  > argument using M-x org-table-R-plot. Is it possible with some sort of
> >> >  > prefix argument, and an (interactive something) declaration? Anyway,
> >> >  > it seems to work if you evaluate e.g.
> >> >  > 
> >> >  > (org-table-R-plot "(function(df) { plot(x=df[,1], y=df[,3]) ; abline(lm(df[,3] ~ df[,1]), col=\"blue\") })")
> >> >  > 
> >> >  > Suggestions for improvements welcome!
> >> >  > 
> >> >  > Dan
> >> >  > 
> >> >  > 
> >> >  > 
> >> >  > On Fri, Jul 25, 2008 at 12:07:00PM -0700, Eric Schulte wrote:
> >> >  > > 
> >> >  > > I had some time waiting for things to execute, so I condensed your
> >> >  > > process into a single command (borrowing heavily from
> >> >  > > org-export-table).
> >> >  > > 
> >> >  > > (defun org-table/gnuplot (&optional x-col)
> >> >  > >   "Plot the current table using gnuplot.  Use a prefix argument
> >> >  > > to specify a column to use for the x-coordinates, to use the row
> >> >  > > number for the x-coordinates provide a prefix argument of 0."
> >> >  > >   (interactive "p")
> >> >  > >   (message (format "%S" x-col))
> >> >  > >   (unless (org-at-table-p)
> >> >  > >     (error "No table at point"))
> >> >  > >   (require 'org-exp)
> >> >  > >   (require 'gnuplot)
> >> >  > >   (org-table-align) ;; make sure we have everything we need
> >> >  > >   (let* ((beg (org-table-begin))
> >> >  > > 	 (end (org-table-end))
> >> >  > > 	 (cols (save-excursion
> >> >  > > 		 (goto-char end)
> >> >  > > 		 (backward-char 3)
> >> >  > > 		 (org-table-current-column)))
> >> >  > > 	 (data-beg (if (and 
> >> >  > > 			(goto-char beg)
> >> >  > > 			(re-search-forward org-table-dataline-regexp end t)
> >> >  > > 			(re-search-forward org-table-hline-regexp end t)
> >> >  > > 			(re-search-forward org-table-dataline-regexp end t))
> >> >  > > 		       (match-beginning 0)
> >> >  > > 		     beg))
> >> >  > > 	 (skip (- (line-number-at-pos data-beg) (line-number-at-pos beg)))
> >> >  > > 	 (exp-format (format "orgtbl-to-tsv :skip %d" skip))
> >> >  > > 	 (file (make-temp-file "org-table-plot")))
> >> >  > >     ;; export table
> >> >  > >     (org-table-export file exp-format)
> >> >  > >     (with-temp-buffer
> >> >  > >       ;; write script
> >> >  > >       (insert (org-table/gnuplot-script file x-col cols))
> >> >  > >       ;; graph table
> >> >  > >       (gnuplot-mode)
> >> >  > >       (gnuplot-send-buffer-to-gnuplot)
> >> >  > >       (bury-buffer (get-buffer "*gnuplot*")))
> >> >  > >     (delete-file file)))
> >> >  > > 
> >> >  > > (defun org-table/gnuplot-script (file x-col num-cols)
> >> >  > >   (let ((plot-str "'%s' using %s:%d with lines title '%d'");; "\\\n    ,"
> >> >  > > 	script)
> >> >  > >     (dotimes (col (+ 1 num-cols))
> >> >  > >       (unless (or (and x-col (equal col x-col)) (equal col 0))
> >> >  > > 	(setf script (cons (format plot-str file (or (and x-col (format "%d" x-col)) "") col col) script))))
> >> >  > >     (concat "plot " (mapconcat 'identity (reverse script) "\\\n    ,"))))
> >> >  > > 
> >> >  > > On Friday, July 25, at 17:25, James TD Smith wrote:
> >> >  > >  > On 2008-07-25 08:53:31(-0700), Eric Schulte wrote:
> >> >  > >  > > 
> >> >  > >  > > Any advice for quick graphing of a table in org-mode?
> >> >  > >  > > 
> >> >  > >  > 
> >> >  > >  > I have a setup for plotting data from tables. I'm not sure if it's exactly what
> >> >  > >  > you want, but yoy may find it useful.
> >> >  > >  > 
> >> >  > >  > 1. Add the following to your .emacs:
> >> >  > >  > 
> >> >  > >  > (defun ahkt-plot-table (script)
> >> >  > >  >   "util function to export and plot a table using the supplied
> >> >  > >  > gnuplot `script'"
> >> >  > >  >   (org-table-export)
> >> >  > >  >   (let ((cbuf (current-buffer))
> >> >  > >  > 	(cwin (selected-window)))
> >> >  > >  >     (save-restriction
> >> >  > >  >       (save-excursion
> >> >  > >  > 	(find-file script)
> >> >  > >  > 	(gnuplot-send-buffer-to-gnuplot)
> >> >  > >  > 	(bury-buffer)
> >> >  > >  > 	(bury-buffer (get-buffer "*gnuplot*"))))
> >> >  > >  >     (and (window-live-p cwin) (select-window cwin))
> >> >  > >  >     (switch-to-buffer cbuf)
> >> >  > >  >     (delete-other-windows)))
> >> >  > >  > 
> >> >  > >  > 2. Create a gnuplot script which plots data from a file.
> >> >  > >  > 
> >> >  > >  > 3. Add the following properties to the headline containing the table.
> >> >  > >  > TABLE_EXPORT_FILE <filename in the gnuploy script>
> >> >  > >  > TABLE_EXPORT_FORMAT orgtbl-to-generic :skip 4 :splice t :sep "\t"
> >> >  > >  > 
> >> >  > >  > 4. Add an org link in the table (it must be in the table otherwise the export
> >> >  > >  > doesn't work) as below:
> >> >  > >  > [[elisp:(ahkt-plot-table "<gnuplot script>")][plot table]] 
> >> >  > >  > 
> >> >  > >  > I suggest you put it at the top of the table.
> >> >  > >  > You will then need to adjust the 'skip' parameter in the export format depending
> >> >  > >  > on the number of lines at the top of the table which should not be exported
> >> >  > >  > (hlines, more than one plotting link etc). 
> >> >  > >  > 
> >> >  > >  > 5. You should then be able to open the link, and get a plot of the table
> >> >  > >  > contents.
> >> >  > >  > 
> >> >  > >  > 
> >> >  > >  > --
> >> >  > >  > |-<James TD Smith>-<email/ahktenzero@mohorovi.cc>-|
> >> >  > >  > 
> >> >  > >  > 
> >> >  > >  > _______________________________________________
> >> >  > >  > Emacs-orgmode mailing list
> >> >  > >  > Remember: use `Reply All' to send replies to the list.
> >> >  > >  > Emacs-orgmode@gnu.org
> >> >  > >  > http://lists.gnu.org/mailman/listinfo/emacs-orgmode
> >> >  > > 
> >> >  > > -- 
> >> >  > > schulte
> >> >  > > 
> >> >  > > 
> >> >  > > _______________________________________________
> >> >  > > Emacs-orgmode mailing list
> >> >  > > Remember: use `Reply All' to send replies to the list.
> >> >  > > Emacs-orgmode@gnu.org
> >> >  > > http://lists.gnu.org/mailman/listinfo/emacs-orgmode
> >> > 
> >> > -- 
> >> > schulte
> >> 
> >> 
> >> _______________________________________________
> >> Emacs-orgmode mailing list
> >> Remember: use `Reply All' to send replies to the list.
> >> Emacs-orgmode@gnu.org
> >> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
> 
> 
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

-- 
http://www.stats.ox.ac.uk/~davison

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: graphing from org-tables
  2009-01-22 16:27               ` Dan Davison
@ 2009-01-23  1:37                 ` Eric Schulte
  2009-01-23  7:30                   ` Carsten Dominik
  0 siblings, 1 reply; 19+ messages in thread
From: Eric Schulte @ 2009-01-23  1:37 UTC (permalink / raw)
  To: emacs-orgmode

Dan Davison <davison@stats.ox.ac.uk> writes:

> On Mon, Dec 22, 2008 at 12:00:14PM -0500, Eric Schulte wrote:
>> Hi Dan,
>> 
>
> <...>
>  
>> 2) Also interesting is the idea of referencing a table from a block of R
>>    code elsewhere in the org file.  I've worked some on processing
>>    blocks of R code in org files in a manner similar to Sweave.
>>    http://github.com/eschulte/org-contrib/tree/master/org-exp-blocks.el
>>    If we could implement a simple means of referencing tables (either
>>    whole tables, or cells/cols/rows of tables) from these R-code blocks
>>    then that seems like it may be fairly powerful.
>
> I agree with this. So in the case of my code, I think we would like to
> be able to do something like
>
> #+TBLR: data:<org-table-reference> columns:(1 2) action:tabulate
>
> This would make the output of the analysis/plot of the table pointed
> to by <org-table-reference> appear at the location in the file of the
> #+TBLR line. Perhaps one design aim would be to have the same
> mechanism work for referencing tables for processing by #TBLFM,
> org-plot, org-table-R, org-exp-blocks, etc.

That sounds like a very good idea.  A uniform interface for referencing
table ranges (either globally from outside of the table or locally from
within the table) that could be used for any table formula be it in
calc, R, elisp, etc...  I find the idea of easily accessing and writing
table data from external tools very intriguing

> What ideas to people have about implementation (and syntax) for this
> sort of table referencing?

As for syntax, I believe the syntax currently in use by TBLFM should be
sufficient for addressing ranges inside of a table.  The only need is
the ability to reference a table globally.

Maybe it would make sense to allow the naming of a table, say something
like a line of the following format either above or below the table.

#+TBLNAME: my-table-name

Then portions of the table could be references from inside the same file
using a syntax like

 my-table-name:$1@2

or from outside of the file using a syntax like

 path/to/file.org:my-table-name:$1@2

or something similar.

I don't know how difficult this would be to implement.  I also don't
know how similar the existing calc table interface is to this new R
table interface.  So basically it's not clear to me how difficult this
would be to implement.  When I have briefly looked into the mechanics of
the current org spreadsheet functionality, I remember being intimidated.

Although I don't really use R, I find this discussion very interesting.
The spreadsheet functions are one of my favorite features of org.

Thanks -- Eric

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: graphing from org-tables
  2009-01-23  1:37                 ` Eric Schulte
@ 2009-01-23  7:30                   ` Carsten Dominik
  2009-01-26  8:53                     ` Carsten Dominik
  0 siblings, 1 reply; 19+ messages in thread
From: Carsten Dominik @ 2009-01-23  7:30 UTC (permalink / raw)
  To: Eric Schulte; +Cc: emacs-orgmode


On Jan 23, 2009, at 2:37 AM, Eric Schulte wrote:

> Dan Davison <davison@stats.ox.ac.uk> writes:
>
>> On Mon, Dec 22, 2008 at 12:00:14PM -0500, Eric Schulte wrote:
>>> Hi Dan,
>>>
>>
>> <...>
>>
>>> 2) Also interesting is the idea of referencing a table from a  
>>> block of R
>>>   code elsewhere in the org file.  I've worked some on processing
>>>   blocks of R code in org files in a manner similar to Sweave.
>>>   http://github.com/eschulte/org-contrib/tree/master/org-exp-blocks.el
>>>   If we could implement a simple means of referencing tables (either
>>>   whole tables, or cells/cols/rows of tables) from these R-code  
>>> blocks
>>>   then that seems like it may be fairly powerful.
>>
>> I agree with this. So in the case of my code, I think we would like  
>> to
>> be able to do something like
>>
>> #+TBLR: data:<org-table-reference> columns:(1 2) action:tabulate
>>
>> This would make the output of the analysis/plot of the table pointed
>> to by <org-table-reference> appear at the location in the file of the
>> #+TBLR line. Perhaps one design aim would be to have the same
>> mechanism work for referencing tables for processing by #TBLFM,
>> org-plot, org-table-R, org-exp-blocks, etc.
>
> That sounds like a very good idea.  A uniform interface for  
> referencing
> table ranges (either globally from outside of the table or locally  
> from
> within the table) that could be used for any table formula be it in
> calc, R, elisp, etc...  I find the idea of easily accessing and  
> writing
> table data from external tools very intriguing

For what it is worth,  here is a function that will grab a field value
or a range from a remote table, be it in the current file or in a
different file.  That is not yet a syntax to have such a reference
in a formula, but it is the most important component, and maybe it will
already be useful for you.

This function uses an entry ID to access the table - obviously
other schemes like table naming could be used.

It is not going to be very fast, and multiple accesses
to the same remote table would each cause the same overhead,
but it sure is working.

- Carsten

(defun org-table-remote-range (id form
				  &optional replace keep-empty numbers lispp)
   "Get a field value or a list of values in a range from table at ID.

ID refers to an entry, and the first table in that entry is the one
being looked at.
FORM is a field or range descriptor like \"@2$3\" or \"@I$2..@II$2\".
All the references must be absolute, not relative.
When REPLACE is non-nil, the return value will be a string in which  
the field
descriptor has been replaced, according to the flags KEEP-EMPTY,  
NUMBERS, and
LISPP.  These flags correspond to the E and N flags, and to the fact  
if the
formula is a lisp formula, not a calc formula.

When REPLACE is nil, a single string or a list of strings is returned,
with with the content(s) of the referenced field(s)."
   (save-match-data
     (let ((id-loc (org-id-find id 'marker))
	  org-table-column-names org-table-column-name-regexp
	  org-table-local-parameters org-table-named-field-locations
	  org-table-current-line-types org-table-current-begin-line
	  org-table-current-begin-pos org-table-dlines
	  org-table-hlines org-table-last-alignment
	  org-table-last-column-widths org-table-last-alignment
	  org-table-last-column-widths tbeg rtn)
       (save-excursion
	(switch-to-buffer (marker-buffer id-loc))
	(save-excursion
	  (save-restriction
	    (widen)
	    (goto-char id-loc)
	    (forward-char 1)
	    (unless (and (re-search-forward "^\\(\\*+ \\)\\|[ \t]*|" nil t)
			 (not (match-beginning 1)))
	      (error "Cannot find a table at ID %s" id))
	    (setq tbeg (point-at-bol))
	    (org-table-get-specials)
	    (when (and (string-match org-table-range-regexp form)
		       (> (length (match-string 0 form)) 1))
	      (save-match-data
		(setq rtn (org-table-get-range
			   (match-string 0 form) tbeg 1))))
	    (if replace
		(replace-match
		 (save-match-data
		   (org-table-make-reference rtn keep-empty numbers lispp))
		 t t form)
	      rtn)))))))

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: graphing from org-tables
  2009-01-23  7:30                   ` Carsten Dominik
@ 2009-01-26  8:53                     ` Carsten Dominik
  2009-01-28  3:06                       ` Dan Davison
  0 siblings, 1 reply; 19+ messages in thread
From: Carsten Dominik @ 2009-01-26  8:53 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: emacs-orgmode


On Jan 23, 2009, at 8:30 AM, Carsten Dominik wrote:

>
> On Jan 23, 2009, at 2:37 AM, Eric Schulte wrote:
>
>> Dan Davison <davison@stats.ox.ac.uk> writes:
>>
>>> On Mon, Dec 22, 2008 at 12:00:14PM -0500, Eric Schulte wrote:
>>>> Hi Dan,
>>>>
>>>
>>> <...>
>>>
>>>> 2) Also interesting is the idea of referencing a table from a  
>>>> block of R
>>>>  code elsewhere in the org file.  I've worked some on processing
>>>>  blocks of R code in org files in a manner similar to Sweave.
>>>>  http://github.com/eschulte/org-contrib/tree/master/org-exp-blocks.el
>>>>  If we could implement a simple means of referencing tables (either
>>>>  whole tables, or cells/cols/rows of tables) from these R-code  
>>>> blocks
>>>>  then that seems like it may be fairly powerful.
>>>
>>> I agree with this. So in the case of my code, I think we would  
>>> like to
>>> be able to do something like
>>>
>>> #+TBLR: data:<org-table-reference> columns:(1 2) action:tabulate
>>>
>>> This would make the output of the analysis/plot of the table pointed
>>> to by <org-table-reference> appear at the location in the file of  
>>> the
>>> #+TBLR line. Perhaps one design aim would be to have the same
>>> mechanism work for referencing tables for processing by #TBLFM,
>>> org-plot, org-table-R, org-exp-blocks, etc.
>>
>> That sounds like a very good idea.  A uniform interface for  
>> referencing
>> table ranges (either globally from outside of the table or locally  
>> from
>> within the table) that could be used for any table formula be it in
>> calc, R, elisp, etc...  I find the idea of easily accessing and  
>> writing
>> table data from external tools very intriguing
>
> For what it is worth,  here is a function that will grab a field value
> or a range from a remote table, be it in the current file or in a
> different file.  That is not yet a syntax to have such a reference
> in a formula, but it is the most important component, and maybe it  
> will
> already be useful for you.
>
> This function uses an entry ID to access the table - obviously
> other schemes like table naming could be used.
>
> It is not going to be very fast, and multiple accesses
> to the same remote table would each cause the same overhead,
> but it sure is working.
>
> - Carsten
>
> (defun org-table-remote-range (id form
> 				  &optional replace keep-empty numbers lispp)
>  "Get a field value or a list of values in a range from table at ID.

In 6.19, there is now  built-in unction to access data from arbitrary  
tables.

- Carsten

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: graphing from org-tables
  2009-01-26  8:53                     ` Carsten Dominik
@ 2009-01-28  3:06                       ` Dan Davison
  2009-01-28 10:18                         ` Carsten Dominik
  0 siblings, 1 reply; 19+ messages in thread
From: Dan Davison @ 2009-01-28  3:06 UTC (permalink / raw)
  To: emacs-orgmode

On Mon, Jan 26, 2009 at 09:53:37AM +0100, Carsten Dominik wrote:
>

< ... >

>> (defun org-table-remote-range (id form
>> 				  &optional replace keep-empty numbers lispp)
>>  "Get a field value or a list of values in a range from table at ID.
>
> In 6.19, there is now  built-in unction to access data from arbitrary  
> tables.
>

Thanks a lot for writing that function Carsten.

I've used that code to extend org-table-R.el so that it can reference
remote tables in org files, and also local csv files and csv files on
the web.

The interface with org-table-R is via #+TBLR lines. These can now
occur anywhere in an org file (previously they had to be beneath the
table being analysed), with a reference to a remote table containing
the data. This reference can take the following forms (these are
options to be supplied in the #+TBLR: line)

1. table:name -- The name of a table in the same file (i.e. one named with #+TBLNAME)
2. table:id -- The unique id of an entry in an org file (the first table under that entry is used)
3. csv:path/to/file.csv -- A local csv file
4. csv:"http:www.somewhere.xx/file.csv" -- A csv file on the web

org-table-R-apply makes happen whatever is specified in the #+TBLR:
and #+TBLR:: lines. Tabular output is sent to the org buffer as a new
org table, or alternatively org-table-R may be used to graph the data
in the table. Point must be in the first #+TBLR line (previously point
had to be in the table itself).

The code (with some initial documentation) is at

http://www.stats.ox.ac.uk/~davison/software/org-table-R/org-table-R.el

I'll try to put together some examples of usage for worg.

Dan

> - Carsten
>
>
>
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

-- 
http://www.stats.ox.ac.uk/~davison

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: graphing from org-tables
  2009-01-28  3:06                       ` Dan Davison
@ 2009-01-28 10:18                         ` Carsten Dominik
  0 siblings, 0 replies; 19+ messages in thread
From: Carsten Dominik @ 2009-01-28 10:18 UTC (permalink / raw)
  To: Dan Davison; +Cc: emacs-orgmode

Hi Dan,

I don't know anything about R, but this looks really interesting.
Whenever you are ready, I will distribute this with Org.

- Carsten

On Jan 28, 2009, at 4:06 AM, Dan Davison wrote:

> On Mon, Jan 26, 2009 at 09:53:37AM +0100, Carsten Dominik wrote:
>>
>
> < ... >
>
>>> (defun org-table-remote-range (id form
>>> 				  &optional replace keep-empty numbers lispp)
>>> "Get a field value or a list of values in a range from table at ID.
>>
>> In 6.19, there is now  built-in unction to access data from arbitrary
>> tables.
>>
>
> Thanks a lot for writing that function Carsten.
>
> I've used that code to extend org-table-R.el so that it can reference
> remote tables in org files, and also local csv files and csv files on
> the web.
>
> The interface with org-table-R is via #+TBLR lines. These can now
> occur anywhere in an org file (previously they had to be beneath the
> table being analysed), with a reference to a remote table containing
> the data. This reference can take the following forms (these are
> options to be supplied in the #+TBLR: line)
>
> 1. table:name -- The name of a table in the same file (i.e. one  
> named with #+TBLNAME)
> 2. table:id -- The unique id of an entry in an org file (the first  
> table under that entry is used)
> 3. csv:path/to/file.csv -- A local csv file
> 4. csv:"http:www.somewhere.xx/file.csv" -- A csv file on the web
>
> org-table-R-apply makes happen whatever is specified in the #+TBLR:
> and #+TBLR:: lines. Tabular output is sent to the org buffer as a new
> org table, or alternatively org-table-R may be used to graph the data
> in the table. Point must be in the first #+TBLR line (previously point
> had to be in the table itself).
>
> The code (with some initial documentation) is at
>
> http://www.stats.ox.ac.uk/~davison/software/org-table-R/org-table-R.el
>
> I'll try to put together some examples of usage for worg.
>
> Dan
>
>> - Carsten
>>
>>
>>
>> _______________________________________________
>> Emacs-orgmode mailing list
>> Remember: use `Reply All' to send replies to the list.
>> Emacs-orgmode@gnu.org
>> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>
> -- 
> http://www.stats.ox.ac.uk/~davison
>
>
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2009-01-28 10:18 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-07-25 15:53 graphing from org-tables Eric Schulte
2008-07-25 16:23 ` Carsten Dominik
2008-07-25 16:25 ` James TD Smith
2008-07-25 16:33   ` Carsten Dominik
2008-07-25 17:14   ` Eric Schulte
2008-07-25 19:07   ` Eric Schulte
2008-07-26 18:15     ` Dan Davison
2008-07-28 15:32       ` Eric Schulte
2008-07-28 15:41         ` Dan Davison
2008-12-21 20:31           ` Dan Davison
2008-12-22 17:00             ` Eric Schulte
2009-01-22 16:27               ` Dan Davison
2009-01-23  1:37                 ` Eric Schulte
2009-01-23  7:30                   ` Carsten Dominik
2009-01-26  8:53                     ` Carsten Dominik
2009-01-28  3:06                       ` Dan Davison
2009-01-28 10:18                         ` Carsten Dominik
     [not found]     ` <C8C13077-8B16-4E8C-8425-5782CE1EDC98@uva.nl>
     [not found]       ` <488b7c9b.14be600a.11cc.ffff9150@mx.google.com>
     [not found]         ` <9629760B-7606-42FD-B625-FAC44490221C@uva.nl>
2008-07-28 14:26           ` Eric Schulte
2008-07-28 21:00             ` Carsten Dominik

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).