emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Berry, Charles" <ccberry@health.ucsd.edu>
To: Giuseppe Pagnoni <gpagnoni@gmail.com>
Cc: "emacs-orgmode@gnu.org" <emacs-orgmode@gnu.org>
Subject: Re: Output of R code block: only text or plot but not both? And only one "result" can be output?
Date: Wed, 5 Jun 2024 17:36:58 +0000	[thread overview]
Message-ID: <279D1AD1-F38A-4442-AE06-60CFA0798874@health.ucsd.edu> (raw)
In-Reply-To: <CAAdktEx-=8DvG7LNkkKg7fJC06QoSNY_buKDaqDbKKmeBK9EBA@mail.gmail.com>

Giuseppe,



> On Jun 3, 2024, at 6:47 AM, Giuseppe Pagnoni <gpagnoni@gmail.com> wrote:
> 
> Hi,
> 
> I am trying to have an R code block output both the results of a numerical expression AND a plot, but if I set up the header arguments to display the plot, the numerical outcome is not displayed.  A simple example:
> 
> #+begin_src R :file example.png :results output graphics file
>   x <- rnorm(100)
>   print(mean(x))
>   hist(x)
> #+end_src
> 


Babel is a great tool, but has some inherent limitations.

Whilst you can work around this by breaking the above block into multiple src blocks, you might have an easier time using ox-ravel [1]. Basically, you export your *.org document to one of the knitr/Rmarkdown/quarto formats (e.g. *.Rmd) and then render the final output using one of those engines. This gives you access to the capabilities of any of those engines.  The downside, of course, is that you need to learn a bit about the formatting rules in one of them to fine tune your final output.

FWIW, I copy and pasted your query to a `*.org` file, exported it to `*.Rmd` with the latest ox-ravel version and rendered it as html - displaying all the printed output and all three plots. That did not need any modifications to your markup.


> Here I get the histogram plot back in the buffer, but NOT the result of "print(mean(x))".   Is it not possible to get both?
> 
> Furthermore, what if I wanted to display *two* or more plots that get built by the code, for instance:
> 
> #+begin_src R :file example.png :results output graphics file
>   x <- rnorm(100)
>   print(mean(x))
>   hist(x)
>   hist(x^2)
> #+end_src
> 
> Here, only the last plot gets displayed.  Is it not possible to display more than 1 plot?
> 


If you want to do this with babel, do the computations in an initial block and then use subsequent blocks for each of the displayed outputs/graphs. You will need to use the `:session` header arg to retain the data in `x' across blocks. 

HTH,

Chuck

[1] https://github.com/chasberry/orgmode-accessories


  reply	other threads:[~2024-06-05 17:37 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-03 13:47 Output of R code block: only text or plot but not both? And only one "result" can be output? Giuseppe Pagnoni
2024-06-05 17:36 ` Berry, Charles [this message]
2024-06-05 18:17   ` [FR] Org babel: mixing multiple outputs (was: Output of R code block: only text or plot but not both? And only one "result" can be output?) Ihor Radchenko
2024-06-05 20:34     ` Berry, Charles
2024-06-07 10:46       ` Ideas for improving ob-R (was: [FR] Org babel: mixing multiple outputs (was: Output of R code block: only text or plot but not both? And only one "result" can be output?)) Ihor Radchenko
2024-06-05 18:37 ` Output of R code block: only text or plot but not both? And only one "result" can be output? Ihor Radchenko
2024-06-07  8:56   ` Giuseppe Pagnoni
2024-06-07  9:10   ` Giuseppe Pagnoni
2024-06-07  9:19     ` Ihor Radchenko
2024-06-07  9:31       ` Giuseppe Pagnoni
2024-06-07  9:39       ` Giuseppe Pagnoni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=279D1AD1-F38A-4442-AE06-60CFA0798874@health.ucsd.edu \
    --to=ccberry@health.ucsd.edu \
    --cc=emacs-orgmode@gnu.org \
    --cc=gpagnoni@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).