emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Problem whit with code evaluation
@ 2010-08-19  0:12 Blanchette, Marco
  2010-08-19  2:37 ` Dan Davison
  0 siblings, 1 reply; 6+ messages in thread
From: Blanchette, Marco @ 2010-08-19  0:12 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

Sorry again if this is common knowledge, but the road to orgmode power user
is seeded with challenges!

I am trying to write an R function that would take 3 arguments, hopefully
set up in an org table and have a function run every rows, taking every
column as arguments to produce a set of report files... But I am going a bit
ahead of myself as I am stuck quite early in the development...

My problem is quite basic. For some reason, I can¹t seems to be able to
execute the following lines in the org buffer using org-babel-execute-buffer
or to export an html of the files. The code block just don't execute and
return errors.

* Examples take from
[[http://orgmode.org/worg/org-contrib/babel/intro.php#sec-7]]

* The python example
#+source: square(x)
#+begin_src python
x*x
#+end_src

#+call: square(x=6)

* The elisp example of the fibonacci series using a table as argument

#+tblname: fibonacci-inputs
| 1 | 2 | 3 | 4 |  5 |  6 |  7 |  8 |  9 | 10 |
| 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 |

#+srcname: fibonacci-seq(fib-inputs=fibonacci-inputs)
#+begin_src emacs-lisp
  (defun fibonacci (n)
    (if (or (= n 0) (= n 1))
        n
      (+ (fibonacci (- n 1)) (fibonacci (- n 2)))))
  
  (mapcar (lambda (row)
            (mapcar #'fibonacci row)) fib-inputs)
#+end_src

* Then my own trial in R
#+srcname: test(x y)
#+begin_src R
   p <- x*y
#+end_src

#+call test(x=4 y=9)
#+function test(x=3 y=8)

Is there something I am missing?

My current setup is:
GNU Emacs 23.1.50.1
org-mode (v7.01g)
R v2.11.1
XServer running Snow Leopard Server 10.6.4

Thanks for the help
-- 
Marco Blanchette, Ph.D.
Assistant Investigator
Stowers Institute for Medical Research
1000 East 50th St.

Kansas City, MO 64110

Tel: 816-926-4071 
Cell: 816-726-8419 
Fax: 816-926-2018 

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

* Re: Problem whit with code evaluation
  2010-08-19  0:12 Problem whit with code evaluation Blanchette, Marco
@ 2010-08-19  2:37 ` Dan Davison
  2010-08-19  3:01   ` Blanchette, Marco
  0 siblings, 1 reply; 6+ messages in thread
From: Dan Davison @ 2010-08-19  2:37 UTC (permalink / raw)
  To: Blanchette, Marco; +Cc: emacs-orgmode@gnu.org

Hi Marco,

"Blanchette, Marco" <MAB@stowers.org> writes:

> Sorry again if this is common knowledge, but the road to orgmode power user
> is seeded with challenges!
>
> I am trying to write an R function that would take 3 arguments, hopefully
> set up in an org table and have a function run every rows, taking every
> column as arguments to produce a set of report files... But I am going a bit
> ahead of myself as I am stuck quite early in the development...
>
> My problem is quite basic. For some reason, I can¹t seems to be able to
> execute the following lines in the org buffer using org-babel-execute-buffer
> or to export an html of the files. The code block just don't execute and
> return errors.
>
> * Examples take from
> [[http://orgmode.org/worg/org-contrib/babel/intro.php#sec-7]]
>
> * The python example
> #+source: square(x)
> #+begin_src python
> x*x
> #+end_src

This one's our fault. That should be
#+begin_src python
return x*x
#+end_src

I've changed it on Worg.

> #+call: square(x=6)
>
> * The elisp example of the fibonacci series using a table as argument
>
> #+tblname: fibonacci-inputs
> | 1 | 2 | 3 | 4 |  5 |  6 |  7 |  8 |  9 | 10 |
> | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 |
>
> #+srcname: fibonacci-seq(fib-inputs=fibonacci-inputs)
> #+begin_src emacs-lisp
>   (defun fibonacci (n)
>     (if (or (= n 0) (= n 1))
>         n
>       (+ (fibonacci (- n 1)) (fibonacci (- n 2)))))
>   
>   (mapcar (lambda (row)
>             (mapcar #'fibonacci row)) fib-inputs)
> #+end_src

OK, that works for me.

>
> * Then my own trial in R
> #+srcname: test(x y)
                   ^
missing comma

> #+begin_src R
>    p <- x*y
> #+end_src
>
> #+call test(x=4 y=9)
        ^        ^

missing colon, and missing comma

> #+function test(x=3 y=8)

That's not correct usage of #+function; you wanted #+lob: / #+call:
there.

There are two sets of synonymous terms. *Within* each of these sets, the
terms are synonymous:

{#+function,#+source,#+srcname}
{#+lob,#+call}

My edited version of your input is below.

Hope that helps, do get back to the list if you have further questions.

Dan


--8<---------------cut here---------------start------------->8---
* Examples take from
[[http://orgmode.org/worg/org-contrib/babel/intro.php#sec-7]]

* The python example
#+source: square(x)
#+begin_src python
return x*x
#+end_src

#+call: square(x=6)

#+results: square(x=6)
: 36

* The elisp example of the fibonacci series using a table as argument

#+tblname: fibonacci-inputs
| 1 | 2 | 3 | 4 |  5 |  6 |  7 |  8 |  9 | 10 |
| 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 |

#+srcname: fibonacci-seq(fib-inputs=fibonacci-inputs)
#+begin_src emacs-lisp
  (defun fibonacci (n)
    (if (or (= n 0) (= n 1))
        n
      (+ (fibonacci (- n 1)) (fibonacci (- n 2)))))
  
  (mapcar (lambda (row)
            (mapcar #'fibonacci row)) fib-inputs)
#+end_src

#+results: fibonacci-seq
| 1 | 1 | 2 |  3 |  5 |   8 |  13 |  21 |   34 |   55 |
| 1 | 3 | 8 | 21 | 55 | 144 | 377 | 987 | 2584 | 6765 |

* Then my own trial in R
#+srcname: test(x, y)
#+begin_src R
   p <- x*y
#+end_src

#+call: test(x=4, y=9)

#+results: test(x=4, y=9)
: 36

#+lob: test(x=3, y=8)

#+results: test(x=3, y=8)
: 24
--8<---------------cut here---------------end--------------->8---

>
> Is there something I am missing?
>
> My current setup is:
> GNU Emacs 23.1.50.1
> org-mode (v7.01g)
> R v2.11.1
> XServer running Snow Leopard Server 10.6.4
>
> Thanks for the help

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

* Re: Problem whit with code evaluation
  2010-08-19  2:37 ` Dan Davison
@ 2010-08-19  3:01   ` Blanchette, Marco
  2010-08-19  4:23     ` Dan Davison
  2010-08-19  5:05     ` Nick Dokos
  0 siblings, 2 replies; 6+ messages in thread
From: Blanchette, Marco @ 2010-08-19  3:01 UTC (permalink / raw)
  To: Dan Davison; +Cc: emacs-orgmode@gnu.org

Hmm... Thanks Dan.

Do we have a different version of org-mode? I just pasted your code in emacs
and try to execute it with M-x org-babel-execute-buffer RET without success.

The emacs-lisp code works but the python and the R crashes with the
following errors taken from the *Org-Babel Error* Output buffer

Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
  File "<stdin>", line 3, in main
NameError: global name 'x' is not defined
Error in main() : object 'x' not found
Calls: write.table -> is.data.frame -> inherits -> main
Execution halted

The first 4 lines comes from the python script while the last 3 come from
the R script

Here is my .emacs org-mode configs in case you are wondering...

;;Load org-mode and set global keys
(require 'org-install)
(add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
(add-hook 'org-mode-hook 'turn-on-font-lock) ; not needed when
global-font-lock-mode is on
(define-key global-map "\C-cl" 'org-store-link)
(define-key global-map "\C-ca" 'org-agenda)
(setq org-log-done t)
(setq org-startup-indented t)


;; active Babel languages
(org-babel-do-load-languages
 'org-babel-load-languages
 '(
   (R . t)
   (sh .t)
   (python . t)
   (emacs-lisp . t)
   )
 )

Thanks again


On 8/18/10 9:37 PM, "Dan Davison" <davison@stats.ox.ac.uk> wrote:

> * The python example
> #+source: square(x)
> #+begin_src python
> return x*x
> #+end_src
> 
> #+call: square(x=6)
> 
> #+results: square(x=6)
> : 36
> 
> * The elisp example of the fibonacci series using a table as argument
> 
> #+tblname: fibonacci-inputs
> | 1 | 2 | 3 | 4 |  5 |  6 |  7 |  8 |  9 | 10 |
> | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 |
> 
> #+srcname: fibonacci-seq(fib-inputs=fibonacci-inputs)
> #+begin_src emacs-lisp
>   (defun fibonacci (n)
>     (if (or (= n 0) (= n 1))
>         n
>       (+ (fibonacci (- n 1)) (fibonacci (- n 2)))))
>  
>   (mapcar (lambda (row)
>             (mapcar #'fibonacci row)) fib-inputs)
> #+end_src
> 
> #+results: fibonacci-seq
> | 1 | 1 | 2 |  3 |  5 |   8 |  13 |  21 |   34 |   55 |
> | 1 | 3 | 8 | 21 | 55 | 144 | 377 | 987 | 2584 | 6765 |
> 
> * Then my own trial in R
> #+srcname: test(x, y)
> #+begin_src R
>    p <- x*y
> #+end_src
> 
> #+call: test(x=4, y=9)
> 
> #+results: test(x=4, y=9)
> : 36
> 
> #+lob: test(x=3, y=8)
> 
> #+results: test(x=3, y=8)
> : 24

--
Marco Blanchette, Ph.D.
Assistant Investigator
Stowers Institute for Medical Research
1000 East 50th St.

Kansas City, MO 64110

Tel: 816-926-4071
Cell: 816-726-8419
Fax: 816-926-2018

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

* Re: Problem whit with code evaluation
  2010-08-19  3:01   ` Blanchette, Marco
@ 2010-08-19  4:23     ` Dan Davison
  2010-08-19  5:05     ` Nick Dokos
  1 sibling, 0 replies; 6+ messages in thread
From: Dan Davison @ 2010-08-19  4:23 UTC (permalink / raw)
  To: Blanchette, Marco; +Cc: emacs-orgmode@gnu.org

"Blanchette, Marco" <MAB@stowers.org> writes:

> Hmm... Thanks Dan.
>
> Do we have a different version of org-mode? I just pasted your code in emacs
> and try to execute it with M-x org-babel-execute-buffer RET without success.

Hi Marco,

Sorry, I was rushing and missed that you were using
org-babel-execute-buffer. I'll look into it.

Dan

>
> The emacs-lisp code works but the python and the R crashes with the
> following errors taken from the *Org-Babel Error* Output buffer
>
> Traceback (most recent call last):
>   File "<stdin>", line 5, in <module>
>   File "<stdin>", line 3, in main
> NameError: global name 'x' is not defined
> Error in main() : object 'x' not found
> Calls: write.table -> is.data.frame -> inherits -> main
> Execution halted
>
> The first 4 lines comes from the python script while the last 3 come from
> the R script
>
> Here is my .emacs org-mode configs in case you are wondering...
>
> ;;Load org-mode and set global keys
> (require 'org-install)
> (add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
> (add-hook 'org-mode-hook 'turn-on-font-lock) ; not needed when
> global-font-lock-mode is on
> (define-key global-map "\C-cl" 'org-store-link)
> (define-key global-map "\C-ca" 'org-agenda)
> (setq org-log-done t)
> (setq org-startup-indented t)
>
>
> ;; active Babel languages
> (org-babel-do-load-languages
>  'org-babel-load-languages
>  '(
>    (R . t)
>    (sh .t)
>    (python . t)
>    (emacs-lisp . t)
>    )
>  )
>
> Thanks again
>
>
> On 8/18/10 9:37 PM, "Dan Davison" <davison@stats.ox.ac.uk> wrote:
>
>> * The python example
>> #+source: square(x)
>> #+begin_src python
>> return x*x
>> #+end_src
>> 
>> #+call: square(x=6)
>> 
>> #+results: square(x=6)
>> : 36
>> 
>> * The elisp example of the fibonacci series using a table as argument
>> 
>> #+tblname: fibonacci-inputs
>> | 1 | 2 | 3 | 4 |  5 |  6 |  7 |  8 |  9 | 10 |
>> | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 |
>> 
>> #+srcname: fibonacci-seq(fib-inputs=fibonacci-inputs)
>> #+begin_src emacs-lisp
>>   (defun fibonacci (n)
>>     (if (or (= n 0) (= n 1))
>>         n
>>       (+ (fibonacci (- n 1)) (fibonacci (- n 2)))))
>>  
>>   (mapcar (lambda (row)
>>             (mapcar #'fibonacci row)) fib-inputs)
>> #+end_src
>> 
>> #+results: fibonacci-seq
>> | 1 | 1 | 2 |  3 |  5 |   8 |  13 |  21 |   34 |   55 |
>> | 1 | 3 | 8 | 21 | 55 | 144 | 377 | 987 | 2584 | 6765 |
>> 
>> * Then my own trial in R
>> #+srcname: test(x, y)
>> #+begin_src R
>>    p <- x*y
>> #+end_src
>> 
>> #+call: test(x=4, y=9)
>> 
>> #+results: test(x=4, y=9)
>> : 36
>> 
>> #+lob: test(x=3, y=8)
>> 
>> #+results: test(x=3, y=8)
>> : 24
>
> --
> Marco Blanchette, Ph.D.
> Assistant Investigator
> Stowers Institute for Medical Research
> 1000 East 50th St.
>
> Kansas City, MO 64110
>
> Tel: 816-926-4071
> Cell: 816-726-8419
> Fax: 816-926-2018
>
>
> _______________________________________________
> Emacs-orgmode mailing list
> Please 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] 6+ messages in thread

* Re: Re: Problem whit with code evaluation
  2010-08-19  3:01   ` Blanchette, Marco
  2010-08-19  4:23     ` Dan Davison
@ 2010-08-19  5:05     ` Nick Dokos
  2010-08-19 13:38       ` Dan Davison
  1 sibling, 1 reply; 6+ messages in thread
From: Nick Dokos @ 2010-08-19  5:05 UTC (permalink / raw)
  To: Blanchette, Marco; +Cc: Dan Davison, nicholas.dokos, emacs-orgmode@gnu.org

Blanchette, Marco <MAB@stowers.org> wrote:

> Hmm... Thanks Dan.
> 
> Do we have a different version of org-mode? I just pasted your code in emacs
> and try to execute it with M-x org-babel-execute-buffer RET without success.
> 
> The emacs-lisp code works but the python and the R crashes with the
> following errors taken from the *Org-Babel Error* Output buffer
> 
> Traceback (most recent call last):
>   File "<stdin>", line 5, in <module>
>   File "<stdin>", line 3, in main
> NameError: global name 'x' is not defined
> Error in main() : object 'x' not found
> 
> On 8/18/10 9:37 PM, "Dan Davison" <davison@stats.ox.ac.uk> wrote:
> 
> > * The python example
> > #+source: square(x)
> > #+begin_src python
> > return x*x
> > #+end_src
> > 
> > #+call: square(x=6)
> > 
> > #+results: square(x=6)
> > : 36
> > 

I think that's because the first source block cannot be evaluated: it
only makes sense when it is #+called. If you place the cursor in that
first source block and press C-c C-c, you get exactly the same error. If
you do the same on the #+call, it works. Since org-babel-execute-buffer
just steps through the buffer and executes every source block, it's not
too surprising that you get those errors.

So I guess the question is whether org-babel-execute-buffer should be
smarter about which source blocks to execute.

Nick

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

* Re: Problem whit with code evaluation
  2010-08-19  5:05     ` Nick Dokos
@ 2010-08-19 13:38       ` Dan Davison
  0 siblings, 0 replies; 6+ messages in thread
From: Dan Davison @ 2010-08-19 13:38 UTC (permalink / raw)
  To: nicholas.dokos; +Cc: Blanchette, Marco, emacs-orgmode@gnu.org

Nick Dokos <nicholas.dokos@hp.com> writes:

> Blanchette, Marco <MAB@stowers.org> wrote:
>
>> Hmm... Thanks Dan.
>> 
>> Do we have a different version of org-mode? I just pasted your code in emacs
>> and try to execute it with M-x org-babel-execute-buffer RET without success.
>> 
>> The emacs-lisp code works but the python and the R crashes with the
>> following errors taken from the *Org-Babel Error* Output buffer
>> 
>> Traceback (most recent call last):
>>   File "<stdin>", line 5, in <module>
>>   File "<stdin>", line 3, in main
>> NameError: global name 'x' is not defined
>> Error in main() : object 'x' not found
>> 
>> On 8/18/10 9:37 PM, "Dan Davison" <davison@stats.ox.ac.uk> wrote:
>> 
>> > * The python example
>> > #+source: square(x)
>> > #+begin_src python
>> > return x*x
>> > #+end_src
>> > 
>> > #+call: square(x=6)
>> > 
>> > #+results: square(x=6)
>> > : 36
>> > 
>
> I think that's because the first source block cannot be evaluated: it
> only makes sense when it is #+called. If you place the cursor in that
> first source block and press C-c C-c, you get exactly the same error. If
> you do the same on the #+call, it works. Since org-babel-execute-buffer
> just steps through the buffer and executes every source block, it's not
> too surprising that you get those errors.
>
> So I guess the question is whether org-babel-execute-buffer should be
> smarter about which source blocks to execute.

[By the way, lob/call lines were not exporting results correctly in any
case; I've just pushed a fix.]


Nick is right. 

So first a couple of workarounds:

1. For export, you can use ':exports none' or ':exports code' on the
function blocks which cannot be executed on their own.

2. You can supply default arguments to function blocks, which permits
them to be executed: #+source: square(x=0)

But (1) doesn't solve the `org-babel-execute-buffer' issue, and (2)
isn't a nice solution.

I've pasted a version of your examples below which exports without error
using workaround (1). It requires a fresh pull of Org from the git repo.

So what is a good solution here? I may be missing an existing solution
but two that come to mind are

1. Extend the existing :eval header arg, introducing a new value, say
   ':eval called' or ':eval passive' that says that while the block may
   not be executed itself, it may be called as a function.

2. Demand that such blocks use the keyword #+function: as opposed
   to #+source or #+srcname.

I'm initially attracted to (2).

Dan

--8<---------------cut here---------------start------------->8---
#+babel: :exports both

Examples take from
[[http://orgmode.org/worg/org-contrib/babel/intro.php#sec-7]]

* The python example
#+source: square(x)
#+begin_src python :exports code
return x*x
#+end_src

#+call: square(x=6)

* The elisp example of the fibonacci series using a table as argument

#+tblname: fibonacci-inputs
| 1 | 2 | 3 | 4 |  5 |  6 |  7 |  8 |  9 | 10 |
| 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 |

#+srcname: fibonacci-seq(fib-inputs=fibonacci-inputs)
#+begin_src emacs-lisp :exports both
  (defun fibonacci (n)
    (if (or (= n 0) (= n 1))
        n
      (+ (fibonacci (- n 1)) (fibonacci (- n 2)))))
  
  (mapcar (lambda (row)
            (mapcar #'fibonacci row)) fib-inputs)
#+end_src

* Then my own trial in R
#+srcname: test(x, y)
#+begin_src R :exports code
   p <- x*y
#+end_src

#+call: test(x=4, y=9)

text inbetween to force line break.

#+lob: test(x=3, y=8)
--8<---------------cut here---------------end--------------->8---



>
> Nick
>
> _______________________________________________
> Emacs-orgmode mailing list
> Please 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] 6+ messages in thread

end of thread, other threads:[~2010-08-19 13:38 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-19  0:12 Problem whit with code evaluation Blanchette, Marco
2010-08-19  2:37 ` Dan Davison
2010-08-19  3:01   ` Blanchette, Marco
2010-08-19  4:23     ` Dan Davison
2010-08-19  5:05     ` Nick Dokos
2010-08-19 13:38       ` Dan Davison

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