emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Referring to results rather than code block
@ 2014-05-19 14:11 Loris Bennett
  2014-05-19 15:25 ` Nick Dokos
  0 siblings, 1 reply; 3+ messages in thread
From: Loris Bennett @ 2014-05-19 14:11 UTC (permalink / raw)
  To: emacs-orgmode

Hi,

I have a code block like this

#+NAME: users_per_month
#+HEADER: :results append
#+BEGIN_SRC sh :dir /root@sadmin:
sacct=/cm/shared/apps/slurm/current/bin/sacct
for y in {2014..2014}; do
    for m in {03..04}; do
        month=$y-$m
        first=$y-$m-01
        last=`date -d "$first + 1 month - 1 day" +"%Y-%m-%d"`
        n=`$sacct -S $first -E $last  -o user -X -n | sort | uniq | wc -l`
        echo $month $n
    done
done
#+END_SRC

which produces something like this

#+RESULTS: users_per_month
| 2012-01 |     1 |
| 2012-02 |    10 |
| 2012-03 |   100 |
| 2012-04 |  1000 |

I'm using append because the generation of a datapoint takes a while.

I'd like to plot the data with something like:

#+NAME: plot_users_per_month
#+HEADER: var data=users_per_month
#+HEADER: :results output graphics
#+HEADER: :file ./users_per_month.pdf :exports both
#+HEADER: :session *r*
#+BEGIN_SRC R
library(ggplot2)

bar_colour  <- "#69B4D8" # steely blue

month <- data$V1
users <- data$V2
df <- data.frame(month,users)
p <- ggplot(df,aes(x=month,y=users)) +
  geom_bar(stat="identity",alpha=0.5,fill=bar_colour) +
      xlab("date") +
        ylab("users")
p
#+END_SRC

However, this is just generating a plot of the data generated by the
source block and not of the total results table.

Can I give the results block a different name to the source block, so
that I can refer to it directly, or should I be doing something
completely different?

Cheers,

Loris
 
-- 
This signature is currently under construction.

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

* Re: Referring to results rather than code block
  2014-05-19 14:11 Referring to results rather than code block Loris Bennett
@ 2014-05-19 15:25 ` Nick Dokos
  2014-05-20  8:55   ` Loris Bennett
  0 siblings, 1 reply; 3+ messages in thread
From: Nick Dokos @ 2014-05-19 15:25 UTC (permalink / raw)
  To: emacs-orgmode

"Loris Bennett" <loris.bennett@fu-berlin.de> writes:

> Hi,
>
> I have a code block like this
>
> #+NAME: users_per_month
> #+HEADER: :results append
> #+BEGIN_SRC sh :dir /root@sadmin:
> sacct=/cm/shared/apps/slurm/current/bin/sacct
> for y in {2014..2014}; do
>     for m in {03..04}; do
>         month=$y-$m
>         first=$y-$m-01
>         last=`date -d "$first + 1 month - 1 day" +"%Y-%m-%d"`
>         n=`$sacct -S $first -E $last  -o user -X -n | sort | uniq | wc -l`
>         echo $month $n
>     done
> done
> #+END_SRC
>
> which produces something like this
>
> #+RESULTS: users_per_month
> | 2012-01 |     1 |
> | 2012-02 |    10 |
> | 2012-03 |   100 |
> | 2012-04 |  1000 |
>
> I'm using append because the generation of a datapoint takes a while.
>
> I'd like to plot the data with something like:
>
> #+NAME: plot_users_per_month
> #+HEADER: var data=users_per_month
> #+HEADER: :results output graphics
> #+HEADER: :file ./users_per_month.pdf :exports both
> #+HEADER: :session *r*
> #+BEGIN_SRC R
> library(ggplot2)
>
> bar_colour  <- "#69B4D8" # steely blue
>
> month <- data$V1
> users <- data$V2
> df <- data.frame(month,users)
> p <- ggplot(df,aes(x=month,y=users)) +
>   geom_bar(stat="identity",alpha=0.5,fill=bar_colour) +
>       xlab("date") +
>         ylab("users")
> p
> #+END_SRC
>
> However, this is just generating a plot of the data generated by the
> source block and not of the total results table.
>
> Can I give the results block a different name to the source block, so
> that I can refer to it directly, or should I be doing something
> completely different?
>

I believe so: the source name ties the source block to the same named
result block - that allows the source block to find the result block
and modify it appropriately. The results block can be named and then
that name can be used in the plotting block, e.g.


--8<---------------cut here---------------start------------->8---
#+name: foo
#+BEGIN_SRC sh :results output table append :var n=5
for x in $(seq $n)
do
echo $x $(expr $x \* $x)
done
#+END_SRC

#+name: foo_results
#+RESULTS: foo
| 1 |  1 |
| 2 |  4 |
| 3 |  9 |
| 4 | 16 |
| 5 | 25 |


#+NAME: plot_foo_results
#+HEADER: :var data=foo_results
#+HEADER: :file ./foo.pdf :exports both
#+BEGIN_SRC gnuplot
plot data
#+END_SRC
--8<---------------cut here---------------end--------------->8---

Nick

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

* Re: Referring to results rather than code block
  2014-05-19 15:25 ` Nick Dokos
@ 2014-05-20  8:55   ` Loris Bennett
  0 siblings, 0 replies; 3+ messages in thread
From: Loris Bennett @ 2014-05-20  8:55 UTC (permalink / raw)
  To: emacs-orgmode

Hi Nick,

Nick Dokos <ndokos@gmail.com> writes:

> "Loris Bennett" <loris.bennett@fu-berlin.de> writes:
>
>> Hi,
>>
>> I have a code block like this
>>
>> #+NAME: users_per_month
>> #+HEADER: :results append
>> #+BEGIN_SRC sh :dir /root@sadmin:
>> sacct=/cm/shared/apps/slurm/current/bin/sacct
>> for y in {2014..2014}; do
>>     for m in {03..04}; do
>>         month=$y-$m
>>         first=$y-$m-01
>>         last=`date -d "$first + 1 month - 1 day" +"%Y-%m-%d"`
>>         n=`$sacct -S $first -E $last  -o user -X -n | sort | uniq | wc -l`
>>         echo $month $n
>>     done
>> done
>> #+END_SRC
>>
>> which produces something like this
>>
>> #+RESULTS: users_per_month
>> | 2012-01 |     1 |
>> | 2012-02 |    10 |
>> | 2012-03 |   100 |
>> | 2012-04 |  1000 |
>>
>> I'm using append because the generation of a datapoint takes a while.
>>
>> I'd like to plot the data with something like:
>>
>> #+NAME: plot_users_per_month
>> #+HEADER: var data=users_per_month
>> #+HEADER: :results output graphics
>> #+HEADER: :file ./users_per_month.pdf :exports both
>> #+HEADER: :session *r*
>> #+BEGIN_SRC R
>> library(ggplot2)
>>
>> bar_colour  <- "#69B4D8" # steely blue
>>
>> month <- data$V1
>> users <- data$V2
>> df <- data.frame(month,users)
>> p <- ggplot(df,aes(x=month,y=users)) +
>>   geom_bar(stat="identity",alpha=0.5,fill=bar_colour) +
>>       xlab("date") +
>>         ylab("users")
>> p
>> #+END_SRC
>>
>> However, this is just generating a plot of the data generated by the
>> source block and not of the total results table.
>>
>> Can I give the results block a different name to the source block, so
>> that I can refer to it directly, or should I be doing something
>> completely different?
>>
>
> I believe so: the source name ties the source block to the same named
> result block - that allows the source block to find the result block
> and modify it appropriately. The results block can be named and then
> that name can be used in the plotting block, e.g.
>
>
> #+name: foo
> #+BEGIN_SRC sh :results output table append :var n=5
> for x in $(seq $n)
> do
> echo $x $(expr $x \* $x)
> done
> #+END_SRC
>
> #+name: foo_results
> #+RESULTS: foo
> | 1 |  1 |
> | 2 |  4 |
> | 3 |  9 |
> | 4 | 16 |
> | 5 | 25 |
>
>
> #+NAME: plot_foo_results
> #+HEADER: :var data=foo_results
> #+HEADER: :file ./foo.pdf :exports both
> #+BEGIN_SRC gnuplot
> plot data
> #+END_SRC
>
> Nick

Oh, that's rather obvious *blush*.  I suppose I was thinking that the
whole results block is generated and it didn't occur to me to just give
it a name.

Thanks,

Loris  

-- 
This signature is currently under construction.

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

end of thread, other threads:[~2014-05-20  8:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-19 14:11 Referring to results rather than code block Loris Bennett
2014-05-19 15:25 ` Nick Dokos
2014-05-20  8:55   ` Loris Bennett

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).