rapport: a report templating system in r

23
rapport: a report templating system in R Literate programming with global options and local arguments Gergely Daróczi & Aleksandar Blagotić [email protected] @daroczig useR! 2014, Los Angeles, USA

Upload: rapporternet

Post on 23-Nov-2015

6.720 views

Category:

Documents


0 download

DESCRIPTION

Talk on one of the Rapporter R packages at the useR! 2014 conference in Los Angeles.

TRANSCRIPT

  • rapport: a report templating system in RLiterate programming with global options and local arguments

    Gergely Darczi & Aleksandar [email protected]

    @daroczig

    useR! 2014, Los Angeles, USA

  • Some custom R packages for reportingR report templates with inputs

    1 rapportoolsHelper functions to be used inside of textual reports.

    2 panderTurning R objects into markdown.

    3 rapport

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 2 / 23

  • What is pander?A collection of helper functions to print markdown syntax

    > ?pandoc.(footnote|header|horizontal.rule|image|link|p)(.return)?> ?pandoc.(emphasis|strikeout|strong|verbatim)(.return)?

    > pandoc.strong(foobar)**foobar**

    > pandoc.strong.return(foobar)[1] "**foobar**"

    > pandoc.header(foobar, level = 2)

    ## foobar

    > pandoc.header(foobar, style = setext)

    foobar======

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 3 / 23

  • What is pander?Collection of helper functions to map R objects to markdown

    > ?pandoc.(list|table)(.return)?

    > pandoc.list(list(foo, list(bar)))

    * foo* bar

    > pandoc.table(head(iris, 2), split.table = Inf)

    -------------------------------------------------------------------Sepal.Length Sepal.Width Petal.Length Petal.Width Species

    -------------- ------------- -------------- ------------- ---------5.1 3.5 1.4 0.2 setosa

    4.9 3 1.4 0.2 setosa-------------------------------------------------------------------

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 4 / 23

  • What is pander?Collection of helper functions to map R objects to various markdown languages

    > pandoc.table(head(iris, 2), split.table = Inf, style = rmarkdown)

    | Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species ||:--------------:|:-------------:|:--------------:|:-------------:|:---------:|| 5.1 | 3.5 | 1.4 | 0.2 | setosa || 4.9 | 3 | 1.4 | 0.2 | setosa |

    > pandoc.table(head(iris, 2), split.table = Inf, style = simple)

    Sepal.Length Sepal.Width Petal.Length Petal.Width Species-------------- ------------- -------------- ------------- ---------

    5.1 3.5 1.4 0.2 setosa4.9 3 1.4 0.2 setosa

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 5 / 23

  • What is pander?Collection of helper functions to map R objects to various markdown languages

    > iris$Species

  • What is pander?S3 method to map R objects to markdown

    > ?pander(.return)?

    > methods(pander)

    [1] pander.anova* pander.aov* pander.cast_df* pander.character*

    [5] pander.data.frame* pander.default* pander.density* pander.evals*

    [9] pander.factor* pander.glm* pander.htest* pander.image*

    [13] pander.list* pander.lm* pander.logical* pander.matrix*

    [17] pander.NULL* pander.numeric* pander.option pander.POSIXct*

    [21] pander.POSIXt* pander.prcomp* pander.rapport* pander.table*

    Non-visible functions are asterisked

    > pander(head(iris, 1), split.table = Inf)

    -------------------------------------------------------------------

    Sepal.Length Sepal.Width Petal.Length Petal.Width Species

    -------------- ------------- -------------- ------------- ---------

    5.1 3.5 1.4 0.2 setosa

    -------------------------------------------------------------------

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 7 / 23

  • What is pander?S3 method to map R objects to markdown

    > pander(letters[1:7])

    _a_, _b_, _c_, _d_, _e_, _f_ and _g_

    > pander(ks.test(runif(50), runif(50))

    ---------------------------------------------------

    Test statistic P value Alternative hypothesis

    ---------------- --------- ------------------------

    0.18 _0.3959_ two-sided

    ---------------------------------------------------

    Table: Two-sample Kolmogorov-Smirnov test: runif(50) and runif(50)

    > pander(chisq.test(table(mtcars$am, mtcars$gear)))

    ---------------------------------------

    Test statistic df P value

    ---------------- ---- -----------------

    20.94 2 _2.831e-05_ * * *

    ---------------------------------------

    Table: Pearsons Chi-squared test: table(mtcars$am, mtcars$gear)

    Warning message:In chisq.test(table(mtcars$am, mtcars$gear)) :Chi-squared approximation may be incorrect

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 8 / 23

  • What is pander?S3 method to map R objects to markdown

    > pander(lm(mtcars$wt ~ mtcars$hp), summary = TRUE)

    --------------------------------------------------------------

    Estimate Std. Error t value Pr(>|t|)

    ----------------- ---------- ------------ --------- ----------

    **mtcars$hp** 0.009401 0.00196 4.796 4.146e-05

    **(Intercept)** 1.838 0.3165 5.808 2.389e-06

    --------------------------------------------------------------

    -------------------------------------------------------------

    Observations Residual Std. Error $R^2$ Adjusted $R^2$

    -------------- --------------------- ------- ----------------

    32 0.7483 0.4339 0.4151

    -------------------------------------------------------------

    Table: Fitting linear model: mtcars$wt ~ mtcars$hp

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 9 / 23

  • What is pander?S3 method to map multiple R objects to markdown

    > mtable123 pander(mtable123)

    --------------------------------------------------

    Model 1 Model 2 Model 3

    ----------------- ---------- ---------- ----------

    **(Intercept)** -1.821 20.556*** 18.875***

    (32.325) (0.542) (1.103)

    **wt** 46.160*** -0.319

    (9.625) (0.328)

    **hp** -0.018***

    (0.003)

    **R-squared** 0.434 0.502 0.031

    **F** 22.999 30.190 0.945

    **p** 0.000 0.000 0.339

    **N** 32 32 32

    --------------------------------------------------

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 10 / 23

  • What is pander?S3 method to map R objects to pretty formatted markdown

    > panderOptions(table.split.table, Inf)

    > panderOptions(table.style, grid)

    > emphasize.cells(which(iris > 1.3, arr.ind = TRUE))

    > pander(iris)

    +----------------+---------------+----------------+---------------+------------+

    | Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |

    +================+===============+================+===============+============+

    | *5.1* | *3.5* | *1.4* | 0.2 | setosa |

    +----------------+---------------+----------------+---------------+------------+

    | *4.9* | *3* | *1.4* | 0.2 | setosa |

    +----------------+---------------+----------------+---------------+------------+

    | *4.7* | *3.2* | 1.3 | 0.2 | setosa |

    +----------------+---------------+----------------+---------------+------------+

    | *4.6* | *3.1* | *1.5* | 0.2 | setosa |

    +----------------+---------------+----------------+---------------+------------+

    | *5* | *3.6* | *1.4* | 0.2 | setosa |

    +----------------+---------------+----------------+---------------+------------+

    | *5.4* | *3.9* | *1.7* | 0.4 | setosa |

    +----------------+---------------+----------------+---------------+------------+

    | *4.6* | *3.4* | *1.4* | 0.3 | setosa |

    +----------------+---------------+----------------+---------------+------------+

    | *5* | *3.4* | *1.5* | 0.2 | setosa |

    +----------------+---------------+----------------+---------------+------------+

    | *4.4* | *2.9* | *1.4* | 0.2 | setosa |

    +----------------+---------------+----------------+---------------+------------+

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 11 / 23

  • What does pander do inside of rapport?A tool for literate programming that automatically transforms R objects into markdown

    # A quick analyis on mtcars

    The mean of is and the standarddeviation is . Let us also check thefrequency table:

    ## Tables are boring!

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 12 / 23

  • What does pander do inside of rapport?Pandoc.brew: markdown results

    # A quick analyis on mtcars

    The mean of am is _0.4062_ and the standarddeviation is _0.499_. Let us also check thefrequency table:

    -------0 1

    --- ---19 13-------

    ## Tables are boring!

    ![Histogram of am](/tmp/RtmphL0K2Q/plots/f2457fb575.png)

    ...

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 13 / 23

  • What does pander do inside of rapport?Pandoc.brew: calling Pandoc to convert the results to HTML

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 14 / 23

  • What does pander do inside of rapport?Pandoc.brew: calling Pandoc to convert the results to MS docx

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 15 / 23

  • rapport overviewThe overall structure of a reusable report template

    ## Subtitle with

    http://rapport-package.info/Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 16 / 23

  • rapport demoHeader: meta information on the reporting template and input(s) specification

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 17 / 23

  • rapport demoBody: normal text with brew-style code chunks

    # A quick analyis on

    The mean of is and thestandard deviation is . Let us alsocheck the frequency table:

    ## Tables are boring!

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 18 / 23

  • rapport demo

    > rapport(rapport-demo, data = mtcars, v = am)

    # A quick analyis on _am_

    The mean of am is _0.4062_ and thestandard deviation is _0.499_. Let us alsocheck the frequency table:

    -------0 1

    --- ---19 13-------

    ## Tables are boring!

    ![Histogram of am](/tmp/RtmphL0K2Q/plots/f2457fb575.png)

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 19 / 23

  • A bit more complex demoAnalysing the results of a dialect survey with Google Maps and ordinary English language

    http://blog.rapporter.net/2013/07/uk-dialect-maps.html

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 20 / 23

  • A bit more complex demoAnalysing the results of a dialect survey with Google Maps and ordinary English language

    http://blog.rapporter.net/2013/07/uk-dialect-maps.html

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 21 / 23

  • Rapporter packagesAll released under AGPL, designed to be deployed in web applications

    pander: A Pandocs markdown writer in Rrapport: A report templating system with dynamic inputs

    rapportools: Helpers functionssandboxR: Filtering malicious R calls

    Further documentation:

    http://rapport-package.info/

    http://rapporter.github.io/pander/

    http://hackme.rapporter.net

    http://blog.rapporter.net

    Q & A: [email protected]

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 22 / 23

  • Why pander?Custom featres

    brew loops and conditional parts of a report just like with brew,capturing plots and images with automatically applied theme,render all R objects automatically in Pandocs markdown,recording all warning/error messages plus the raw R objects alongwith anything printed to stdout and the printed results,custom caching mechanism to disk or RAM with auto-dependecy,convert to HTML/pdf/odt/docx at one go,no chunk options (only workaround),building reports also in interactive session with an R5 reference class.

    http://rapporter.github.io/pander/#brew-to-pandoc

    Gergely Darczi (rapporter.net) rapport: a report templating system in R useR! 2014 23 / 23