CDSoft / pp

PP - Generic preprocessor (with pandoc in mind) - macros, literate programming, diagrams, scripts...
http://cdelord.fr/pp
GNU General Public License v3.0
252 stars 21 forks source link

R support? #43

Closed lf-araujo closed 6 years ago

lf-araujo commented 6 years ago

Are there any plans for R programming language support? R allows for very complex graphic capabilities and would be a nice addition!

Thanks

lf-araujo commented 6 years ago

Thanks, will try to create one for R and report back...

CDSoft commented 6 years ago

Just to clarify things. pp uses graphviz and plantuml to produce diagrams from a simple text file embedded in a markdown document. In the output, the source of the diagram is replaced by an image. I'll soon add a support for Asymptote.

R may be a bit more complex because I guess it can do much more than just producing an image.

What kind of usage would like?

In both cases it is possible to add R support. In fact you can already call R scripts as shell scripts (not tested!):

!sh
~~~~~
#!/usr/bin/Rscript --slave
....
~~~~~
lf-araujo commented 6 years ago

Thanks,

Two functionalities:

  1. Run a script and capture all text (so to source into a mardkown document);
  2. Run a script and capture the image from the R device.

Best, lf

CDSoft commented 6 years ago

Could you please give small examples (scripts and command lines) for both functionalities?

lf-araujo commented 6 years ago

Absolutelly,

Here is a small example using knitr. The code I enter within the block gets printed at the pdf as well:

```{r lm}
model = lm(dist~speed,data = cars)
summary(model)

Produces:

Call:
lm(formula = dist ~ speed, data = cars)

Residuals:
    Min      1Q  Median      3Q     Max 
-29.069  -9.525  -2.272   9.215  43.201 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) -17.5791     6.7584  -2.601   0.0123 *  
speed         3.9324     0.4155   9.464 1.49e-12 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 15.38 on 48 degrees of freedom
Multiple R-squared:  0.6511,    Adjusted R-squared:  0.6438 
F-statistic: 89.57 on 1 and 48 DF,  p-value: 1.49e-12

The code below is executed with knitr and printed out as a graph:

```{r pressure, echo=FALSE, fig.cap='test plot'}
plot(pressure)

image

Here is how it looks in the pdf:

tela

CDSoft commented 6 years ago

Ok. The first example could be implemented as a script macro that calls Rscript --slave.

The second example, called with Rscript --slave produces a PDF file (Rplots.pdf). Do you know how to change the name and the format of the output?

lf-araujo commented 6 years ago

Thanks for the interest. A quick investigation and I found a possible approach. If you have R installed, send the following to bash (or run in the command line):

Rscript -e 'pdf("foo.pdf"); plot(cumsum(rnorm(100)), type="l"); dev.off()' This will generate a foo.pdf with a nice figure that pandoc will be able to import. Other functions exists as well, but they are not in base R (png(), jpeg(), and so on)... I am almost sure pandoc can handle pdfs as figures for latex output, less sure about other formats...

UPDATE: just tested, pdf figures are accepted in latex and html, probably all non-plain text formats as well.

CDSoft commented 6 years ago

Rscript -e 'png("foo.png"); plot(cumsum(rnorm(100)), type="l");' produces a PNG image.

This could be executed by a Rplot macro:

!Rplot(foo)
~~~~
plot(cumsum(rnorm(100)), type="l");
~~~~

The Rscript will just execute a script and print its output.

CDSoft commented 6 years ago

I have added both !Rscript and !Rplot to the last release (2.1.2):

It works on Linux (Fedora with the default R package). I can not test it on MacOS or Windows.

lf-araujo commented 6 years ago

Thank you so much. Will test it, as soon as I can!

I will try to incorporate pp into my pandoc building system with make.

Best, lf

lf-araujo commented 6 years ago

Tested, both work as expected in Ubuntu 17.04. Thank you.

It would be an extra if I were able to pass {width = 30%} to the figure environment as per the other issue I opened.

You may want to close this issue.

CDSoft commented 6 years ago

With diagrams macros you can pass the attributes with the image name:

!Rplot(IMAGE{attributes})(LEGEND)(GRAPH DESCRIPTION)

e.g.

!Rplot(img/foo{width=30%})(legend...)(R script...)
robinrosenstock commented 6 years ago

@CDSoft please close issues, whenever possible