emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* rmarkdown-like production of multiple plots in org
@ 2020-03-31 19:23 Matt Price
  2020-03-31 20:12 ` John Hendy
  2020-04-02 17:39 ` Berry, Charles via General discussions about Org-mode.
  0 siblings, 2 replies; 5+ messages in thread
From: Matt Price @ 2020-03-31 19:23 UTC (permalink / raw)
  To: Org Mode

[-- Attachment #1: Type: text/plain, Size: 996 bytes --]

I'm completely new to R.

I've started working with a project that creates plots using the ggplot
package -- so by default it creates grid objects, rather than writing to
files.

In rmarkdown/rstudio, I can write something like this in a SOMEFILE.Rmd :

```
install_github('eeholmes/CoV19')
library(CoV19)
getdata();
plot4(world, 'Ontario Canada')
plot2(world, 'Italy')
plot4(states, "WA")
```

I sort of love how the rmarkdown package will just create all 3 of those
plots, save them to auto-named files, and render to HTML.  In RStudio,
running just that block will also create all three blocks and display them
in the editor.

By contrast, creating a series of many plots in org is fairly tedious.  I
have to name the plot individually & put each function call in its own src
block. Is there any way to mimic the behaviour of rmarkdown instead? I
odn't understand babel or R enough to really even see how something like
that could be implemented, but I'd appreciate some pointers.  Thank you!

[-- Attachment #2: Type: text/html, Size: 1303 bytes --]

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

* Re: rmarkdown-like production of multiple plots in org
@ 2020-03-31 20:02 Thomas S. Dye
  0 siblings, 0 replies; 5+ messages in thread
From: Thomas S. Dye @ 2020-03-31 20:02 UTC (permalink / raw)
  To: moptop99; +Cc: emacs-org list

Aloha Matt,

A guess based on my experience with ggplot2 is that the 
auto-naming and saving take place in the functions plot2() and 
plot4().  If so, then calling them from Org mode babel blocks will 
also get you auto-naming and saving.

I think you're right that you'll need to put each of the plots in 
its own babel source code block and then set up the html export to 
your satisfaction.  IIUC, these are the prices one pays for 
working in a system like Org mode which is both language agnostic 
and export target agnostic.  The rmarkdown/rstudio solution is 
nifty, but it is tied to one language and one export target.

hth,
Tom
--
Thomas S. Dye
https://tsdye.online/tsdye

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

* Re: rmarkdown-like production of multiple plots in org
  2020-03-31 19:23 rmarkdown-like production of multiple plots in org Matt Price
@ 2020-03-31 20:12 ` John Hendy
  2020-04-02 17:39 ` Berry, Charles via General discussions about Org-mode.
  1 sibling, 0 replies; 5+ messages in thread
From: John Hendy @ 2020-03-31 20:12 UTC (permalink / raw)
  To: Matt Price; +Cc: Org Mode

On Tue, Mar 31, 2020 at 2:22 PM Matt Price <moptop99@gmail.com> wrote:
>
> I'm completely new to R.
>
> I've started working with a project that creates plots using the ggplot package -- so by default it creates grid objects, rather than writing to files.
>
> In rmarkdown/rstudio, I can write something like this in a SOMEFILE.Rmd :
>
> ```
> install_github('eeholmes/CoV19')
> library(CoV19)
> getdata();
> plot4(world, 'Ontario Canada')
> plot2(world, 'Italy')
> plot4(states, "WA")
> ```
>

Interesting. I hadn't really thought that approach through. For
exploratory analysis, this sounds awesome and I don't think I've ever
tried multiple plots in a single chunk in RStudio. Only after seeing
Thomas' reply just now did I realize this isn't just plot(), though...
where are those functions from? There might be hidden conveniences
that don't apply to pure ggplot() calls... dunno.

I will add that as soon as you want to start tailoring sizes or how
these appear in the resultant file, I think you'd have to split these
into separate chunks in order to set the options, no?

> I sort of love how the rmarkdown package will just create all 3 of those plots, save them to auto-named files, and render to HTML.  In RStudio, running just that block will also create all three blocks and display them in the editor.
>
> By contrast, creating a series of many plots in org is fairly tedious.  I have to name the plot individually & put each function call in its own src block. Is there any way to mimic the behaviour of rmarkdown instead? I odn't understand babel or R enough to really even see how something like that could be implemented, but I'd appreciate some pointers.  Thank you!

Perhaps an alternative is running all your plots in one block, but
using ggsave() (or similar) to save out the files directly (vs. using
:output/:file to do it). Then you could have file links in the org
file instead of the typical 1:1 match-up of a single block to a single
result. I did this once during an effort to optimize a neural network.
I had a big loop iterating through parameters, and would
programmatically save out one residual plot per combination, e.g.
model_var1-value_var2-value_etc.png, I also generated an org-mode
section and exported headers for each combination, embedding the
corresponding [[./plots/foo.png]] image link within that heading.
Exporting to pdf let me page through all my residual plots handily to
compare them.

Anyway, maybe something helpful in there?

John

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

* Re: rmarkdown-like production of multiple plots in org
  2020-03-31 19:23 rmarkdown-like production of multiple plots in org Matt Price
  2020-03-31 20:12 ` John Hendy
@ 2020-04-02 17:39 ` Berry, Charles via General discussions about Org-mode.
  2020-04-02 17:52   ` Matt Price
  1 sibling, 1 reply; 5+ messages in thread
From: Berry, Charles via General discussions about Org-mode. @ 2020-04-02 17:39 UTC (permalink / raw)
  To: Matt Price; +Cc: Org Mode



> On Mar 31, 2020, at 12:23 PM, Matt Price <moptop99@gmail.com> wrote:
> 
> I'm completely new to R.
> 
> I've started working with a project that creates plots using the ggplot package -- so by default it creates grid objects, rather than writing to files.  
> 
> In rmarkdown/rstudio, I can write something like this in a SOMEFILE.Rmd : 
> 
> ```
> install_github('eeholmes/CoV19')
> library(CoV19)
> getdata();
> plot4(world, 'Ontario Canada') 
> plot2(world, 'Italy') 
> plot4(states, "WA")
> ```
> 
> I sort of love how the rmarkdown package will just create all 3 of those plots, save them to auto-named files, and render to HTML.

Actually, this is knitr (which rmarkdown Imports) at work.  There are options as to how knitr will handle multiple plots in a chunk as described in

https://yihui.org/knitr/options/#plots

(which include `fig.show="animate"' to create an animation based on multiple plots!)

So this applies to various filetypes in addition to *.Rmd (*.Rnw, for one). 


>  In RStudio, running just that block will also create all three blocks and display them in the editor.  
> 
> By contrast, creating a series of many plots in org is fairly tedious.  I have to name the plot individually & put each function call in its own src block. Is there any way to mimic the behaviour of rmarkdown instead? I odn't understand babel or R enough to really even see how something like that could be implemented, but I'd appreciate some pointers.  Thank you!

Getting babel to handle this seamlessly would be a significant effort.

You can use ox-ravel (https://github.com/chasberry/orgmode-accessories.git) to export to *.Rmd and then render the result.  However, that does not have the interactivity of `org-babel-execute-src-block' and does not insert the graphics into the *.org file. 

I suppose that a function could be created to narrow to the src block, export it as *.Rmd to a buffer, run that buffer as the `text' arg of knitr::knit, then add links for the png's back to the *.org file. I haven't thought much about this - getting this to work in a simple case would not be too hard, but there may be a can of worms that this approach opens.

HTH,

Chuck

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

* Re: rmarkdown-like production of multiple plots in org
  2020-04-02 17:39 ` Berry, Charles via General discussions about Org-mode.
@ 2020-04-02 17:52   ` Matt Price
  0 siblings, 0 replies; 5+ messages in thread
From: Matt Price @ 2020-04-02 17:52 UTC (permalink / raw)
  To: Berry, Charles; +Cc: Org Mode

[-- Attachment #1: Type: text/plain, Size: 2587 bytes --]

Thanks Chuck and all other respondents.  Huh. I'll keep experimenting. I
like Chuck's idea of helper function that pipes theoutput as described but
for now, it might actually beeasier to stay with Rmarkdown (!). thanks, Matt

On Thu, Apr 2, 2020 at 1:39 PM Berry, Charles <ccberry@health.ucsd.edu>
wrote:

>
>
> > On Mar 31, 2020, at 12:23 PM, Matt Price <moptop99@gmail.com> wrote:
> >
> > I'm completely new to R.
> >
> > I've started working with a project that creates plots using the ggplot
> package -- so by default it creates grid objects, rather than writing to
> files.
> >
> > In rmarkdown/rstudio, I can write something like this in a SOMEFILE.Rmd
> :
> >
> > ```
> > install_github('eeholmes/CoV19')
> > library(CoV19)
> > getdata();
> > plot4(world, 'Ontario Canada')
> > plot2(world, 'Italy')
> > plot4(states, "WA")
> > ```
> >
> > I sort of love how the rmarkdown package will just create all 3 of those
> plots, save them to auto-named files, and render to HTML.
>
> Actually, this is knitr (which rmarkdown Imports) at work.  There are
> options as to how knitr will handle multiple plots in a chunk as described
> in
>
> https://yihui.org/knitr/options/#plots
>
> (which include `fig.show="animate"' to create an animation based on
> multiple plots!)
>
> So this applies to various filetypes in addition to *.Rmd (*.Rnw, for
> one).
>
>
> >  In RStudio, running just that block will also create all three blocks
> and display them in the editor.
> >
> > By contrast, creating a series of many plots in org is fairly tedious.
> I have to name the plot individually & put each function call in its own
> src block. Is there any way to mimic the behaviour of rmarkdown instead? I
> odn't understand babel or R enough to really even see how something like
> that could be implemented, but I'd appreciate some pointers.  Thank you!
>
> Getting babel to handle this seamlessly would be a significant effort.
>
> You can use ox-ravel (https://github.com/chasberry/orgmode-accessories.git)
> to export to *.Rmd and then render the result.  However, that does not have
> the interactivity of `org-babel-execute-src-block' and does not insert the
> graphics into the *.org file.
>
> I suppose that a function could be created to narrow to the src block,
> export it as *.Rmd to a buffer, run that buffer as the `text' arg of
> knitr::knit, then add links for the png's back to the *.org file. I haven't
> thought much about this - getting this to work in a simple case would not
> be too hard, but there may be a can of worms that this approach opens.
>
> HTH,
>
> Chuck
>
>
>

[-- Attachment #2: Type: text/html, Size: 3409 bytes --]

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

end of thread, other threads:[~2020-04-02 17:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-31 19:23 rmarkdown-like production of multiple plots in org Matt Price
2020-03-31 20:12 ` John Hendy
2020-04-02 17:39 ` Berry, Charles via General discussions about Org-mode.
2020-04-02 17:52   ` Matt Price
  -- strict thread matches above, loose matches on Subject: below --
2020-03-31 20:02 Thomas S. Dye

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