[-- 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 --]
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
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
> 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 #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 --]