* [babel] multiple result outputs from function
@ 2010-03-16 18:26 Graham Smith
2010-03-16 18:35 ` Dan Davison
2010-03-16 18:39 ` Erik Iverson
0 siblings, 2 replies; 7+ messages in thread
From: Graham Smith @ 2010-03-16 18:26 UTC (permalink / raw)
To: emacs-orgmode
Below is a function that I am trying to run in orgmode/babel.
It seems to run OK, but instead of printing out three values, its only
printing the final result.
Once again, i would appreciate some help with what I am missing.
Thanks,
Graham
#+srcname: CI_function
#+begin_src R :session daf
summary.ci<-function(x){
xbar<-mean(x)
sigma<-sd(x)
n<-length(x)
sem<-sigma/sqrt(n)
lower.ci<-xbar+sem*qnorm(0.025)
upper.ci<-xbar+sem*qnorm(0.975)
print (xbar)
print (lower.ci)
print (upper.ci)
}
#+end_src
#+srcname: SumFlowering2005
#+begin_src R :session daf
summary.ci(daf$Flower[daf$YEAR=="2005"])
#+end_src
#+results: SumFlowering2005
: 1.97860201016947
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [babel] multiple result outputs from function
2010-03-16 18:26 [babel] multiple result outputs from function Graham Smith
@ 2010-03-16 18:35 ` Dan Davison
2010-03-16 18:43 ` Graham Smith
2010-03-16 18:39 ` Erik Iverson
1 sibling, 1 reply; 7+ messages in thread
From: Dan Davison @ 2010-03-16 18:35 UTC (permalink / raw)
To: Graham Smith; +Cc: emacs-orgmode
Graham Smith <myotisone@gmail.com> writes:
> Below is a function that I am trying to run in orgmode/babel.
>
> It seems to run OK, but instead of printing out three values, its only
> printing the final result.
>
> Once again, i would appreciate some help with what I am missing.
Hi Graham,
Use ':results output' to get the printed output. The default mode
is ':results value' which returns the value of the last expression in
the block.
#+srcname: CI_function
#+begin_src R :session daf
summary.ci<-function(x){
print ("value 1")
print ("value 2")
print ("value 3")
}
#+end_src
#+begin_src R :session daf :results output
summary.ci()
#+end_src
#+results:
: [1] "value 1"
: [1] "value 2"
: [1] "value 3"
#+begin_src R :session daf
summary.ci()
#+end_src
#+results:
: value 3
Dan
>
> Thanks,
>
> Graham
>
>
> #+srcname: CI_function
> #+begin_src R :session daf
> summary.ci<-function(x){
> xbar<-mean(x)
> sigma<-sd(x)
> n<-length(x)
> sem<-sigma/sqrt(n)
> lower.ci<-xbar+sem*qnorm(0.025)
> upper.ci<-xbar+sem*qnorm(0.975)
> print (xbar)
> print (lower.ci)
> print (upper.ci)
> }
> #+end_src
>
> #+srcname: SumFlowering2005
> #+begin_src R :session daf
> summary.ci(daf$Flower[daf$YEAR=="2005"])
> #+end_src
>
> #+results: SumFlowering2005
> : 1.97860201016947
>
>
> _______________________________________________
> 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] 7+ messages in thread
* Re: [babel] multiple result outputs from function
2010-03-16 18:26 [babel] multiple result outputs from function Graham Smith
2010-03-16 18:35 ` Dan Davison
@ 2010-03-16 18:39 ` Erik Iverson
2010-03-16 18:53 ` Graham Smith
1 sibling, 1 reply; 7+ messages in thread
From: Erik Iverson @ 2010-03-16 18:39 UTC (permalink / raw)
To: Graham Smith; +Cc: emacs-orgmode
Graham -
Graham Smith wrote:
> Below is a function that I am trying to run in orgmode/babel.
>
> It seems to run OK, but instead of printing out three values, its only
> printing the final result.
>
> Once again, i would appreciate some help with what I am missing.
>
> Thanks,
>
> Graham
>
>
> #+srcname: CI_function
> #+begin_src R :session daf
> summary.ci<-function(x){
> xbar<-mean(x)
> sigma<-sd(x)
> n<-length(x)
> sem<-sigma/sqrt(n)
> lower.ci<-xbar+sem*qnorm(0.025)
> upper.ci<-xbar+sem*qnorm(0.975)
> print (xbar)
> print (lower.ci)
> print (upper.ci)
> }
> #+end_src
>
> #+srcname: SumFlowering2005
> #+begin_src R :session daf
> summary.ci(daf$Flower[daf$YEAR=="2005"])
> #+end_src
>
> #+results: SumFlowering2005
> : 1.97860201016947
>
From org-babel docs...
The following options are mutually exclusive, and specify how the
results should be collected from the source code block.
------------------------------------------------------
value
This is the default. The result is the value of the last statement
in the source code block. This header argument places Org-babel in
functional mode. Note that in some languages, e.g., python, use of this
result type requires that a return statement be included in the body of
the source code block. E.g., :results value.
output
The result is the collection of everything printed to stdout during
the execution of the source code block. This header argument places
Org-babel in scripting mode. E.g., :results output.
-------------------------------------------------------
So, add ":results output" to your source header block.
Or, you might want to define your R function to return a list of the
three components you're after for further processing, and then not have
to include ":results output"
Instead of the 3 print statements, which don't create objects, you could
substitute
list(xbar, lower.ci, upper.ci)
--Erik
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [babel] multiple result outputs from function
2010-03-16 18:35 ` Dan Davison
@ 2010-03-16 18:43 ` Graham Smith
0 siblings, 0 replies; 7+ messages in thread
From: Graham Smith @ 2010-03-16 18:43 UTC (permalink / raw)
To: emacs-orgmode
Dan
> Use ':results output' to get the printed output. The default mode
> is ':results value' which returns the value of the last expression in
> the block.
Thanks, that's great.
At least I managed to sort out my other two problems today on my own !!
Graham
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [babel] multiple result outputs from function
2010-03-16 18:39 ` Erik Iverson
@ 2010-03-16 18:53 ` Graham Smith
2010-03-16 19:26 ` Dan Davison
0 siblings, 1 reply; 7+ messages in thread
From: Graham Smith @ 2010-03-16 18:53 UTC (permalink / raw)
To: Erik Iverson; +Cc: emacs-orgmode
Erik
> So, add ":results output" to your source header block.
Dan beat you to it, but..
> list(xbar, lower.ci, upper.ci)
This is useful, but the output isn't exactly as I expected
#+results: SumFlowering2005
: [[1]]
: [1] 1.513333
:
: [[2]]
: [1] 1.048065
:
: [[3]]
: [1] 1.978602
is there a way of replacing the [1] [2] and [3] with the object names
(I know this is really an R question)
Thanks,
Graham
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [babel] multiple result outputs from function
2010-03-16 18:53 ` Graham Smith
@ 2010-03-16 19:26 ` Dan Davison
2010-03-16 19:38 ` Graham Smith
0 siblings, 1 reply; 7+ messages in thread
From: Dan Davison @ 2010-03-16 19:26 UTC (permalink / raw)
To: Graham Smith; +Cc: emacs-orgmode
Graham Smith <myotisone@gmail.com> writes:
> Erik
>
>> So, add ":results output" to your source header block.
>
> Dan beat you to it, but..
>
>> list(xbar, lower.ci, upper.ci)
>
> This is useful, but the output isn't exactly as I expected
>
> #+results: SumFlowering2005
> : [[1]]
> : [1] 1.513333
> :
> : [[2]]
> : [1] 1.048065
> :
> : [[3]]
> : [1] 1.978602
>
> is there a way of replacing the [1] [2] and [3] with the object names
I think you meant replacing the [[1]], [[2]], [[3]]. That would be
list(myname1=xbar, myname2=lower.ci, myname3=upper.ci)
> (I know this is really an R question)
yep :)
Dan
>
> Thanks,
>
> Graham
>
>
> _______________________________________________
> 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] 7+ messages in thread
* Re: [babel] multiple result outputs from function
2010-03-16 19:26 ` Dan Davison
@ 2010-03-16 19:38 ` Graham Smith
0 siblings, 0 replies; 7+ messages in thread
From: Graham Smith @ 2010-03-16 19:38 UTC (permalink / raw)
To: Dan Davison; +Cc: emacs-orgmode
Dan,
>> is there a way of replacing the [1] [2] and [3] with the object names
>
> I think you meant replacing the [[1]], [[2]], [[3]]. That would be
>
> list(myname1=xbar, myname2=lower.ci, myname3=upper.ci)
>
>> (I know this is really an R question)
Thanks again :-)
Graham
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2010-03-16 19:40 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-03-16 18:26 [babel] multiple result outputs from function Graham Smith
2010-03-16 18:35 ` Dan Davison
2010-03-16 18:43 ` Graham Smith
2010-03-16 18:39 ` Erik Iverson
2010-03-16 18:53 ` Graham Smith
2010-03-16 19:26 ` Dan Davison
2010-03-16 19:38 ` Graham Smith
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).