background preloader

R Markdown V2

Facebook Twitter

R Quick Tip: parameter re-use within rmarkdown YAML. Ever wondered how to make an rmarkdown title dynamic?

R Quick Tip: parameter re-use within rmarkdown YAML

Reporting in a Repeatable, Parameterised, Transparent Way. Earlier this week, I spent a day chatting to folk from the House of Commons Library as a part of a bit of temporary day-a-week-or-so bit of work I’m doing with the Parliamentary Digital Service.

Reporting in a Repeatable, Parameterised, Transparent Way

During one of the conversations on matters loosely geodata-related with Carl Baker, Carl mentioned an NHS Digital data set describing the number of people on a GP Practice list who live within a particular LSOA (Lower Super Output Area). There are possible GP practice closures on the Island at the moment, so I thought this might be an interesting dataset to play with in that respect.

Another thing Carl is involved with is producing a regularly updated briefing on Accident and Emergency Statistics. Excel and QGIS templates do much of the work in producing the updated documents, so much of the data wrangling side of the report generation is automated using those tools. How to set up your own R blog with Github pages and Jekyll Bootstrap.

This post is in reply to a request: How did I set up this R blog?

How to set up your own R blog with Github pages and Jekyll Bootstrap

I have wanted to have my own R blog for a while before I actually went ahead and realised this page. I had seen all the cool things people do with R by following R-bloggers and reading their newsletter every day! While I am using R every day at work, the thematic scope there is of course very bioinformatics-centric (with a little bit of Machine Learning and lots of visualization and statistics), I wanted to have an incentive to regularly explore other types of analyses and other types of data that I don’t normally work with.

I have also very often benefited from other people’s published code in that it gave me ideas for my own work and I hope that sharing my own analyses will inspire others as much as I often am by what can be be done with data. Jupyter And R Markdown: Notebooks With R. When working on data science problems, you might want to set up an interactive environment to work and share your code for a project with others.

Jupyter And R Markdown: Notebooks With R

You can easily set this up with a notebook. In other cases, you’ll just want to communicate about the workflow and the results that you have gathered for the analysis of your data science problem. For a transparent and reproducible report, a notebook can also come in handy. That's right; notebooks are perfect for situations where you want to combine plain text with rich text elements such as graphics, calculations, etc. The topic of today's blog post focuses on the two notebooks that are popular with R users, namely, the Jupyter Notebook and, even though it's still quite new, the R Markdown Notebook. R And The Jupyter Notebook Contrary to what you might think, Jupyter doesn’t limit you to working solely with Python: the notebook application is language agnostic, which means that you can also work with other languages. R Markdown: How to number and reference tables. R Markdown is a great tool to make research results reproducible.

R Markdown: How to number and reference tables

However, in scientific research papers or reports, tables and figures usually need to be numbered and referenced. Unfortunately, R Markdown has no “native” method to number and reference table and figure captions. Dynamic Documents with RMarkdown and Knitr. Escape the Land of LaTeX/Word for Statistical Reporting: The Ecosystem of R Markdown Webinar. Getting started with R Markdown. “Mail merge” with RMarkdown. The term “mail merge” might not be familiar to those who have not worked in an office setting, but here is the Wikipedia definition: Mail merge is a software operation describing the production of multiple (and potentially large numbers of) documents from a single template form and a structured data source.

“Mail merge” with RMarkdown

The letter may be sent out to many “recipients” with small changes, such as a change of address or a change in the greeting line.Source: The other day I was working on creating personalized handouts for a workshop. That is, each handout contained some standard text (including some R code) and some fields that were personalized for each participant (login information for our RStudio server). I wanted to do this in RMarkdown so that the R code on the handout could be formatted nicely. Turns our this is a pretty straightforward task. All relevant files for this example can also be found here. Makefiles and RMarkdown. Quite some time ago (October 2013, according to Amazon), I bought a copy of “Reproducible Research with R and RStudio” by Christopher Gandrud.

Makefiles and RMarkdown

And it was awesome. Since then, I’ve been using knitr and RMarkdown quite a lot. However, until recently, I never bothered with a makefile. At the time, I had assumed that it was something only available to people on *nix systems and back then I was developing exclusively on PC. I even wrote some R scripts that were more or less makefiles; reading the contents of a directory, checking for output and running render or knit or whatever. So, enough preamble. The benefit A makefile ensures that all of your work gets done, but only when it needs to and that each step has the raw material it needs to work.

The assumptions Use RMarkdown files as your default. The steps. One weird trick to compile multipartite dynamic... < biochemistries. This afternoon I stumbled across this one weird trick an undocumented part of the YAML headers that get processed when you click the ‘knit’ button in RStudio.

One weird trick to compile multipartite dynamic... < biochemistries

Knitting turns an Rmarkdown document into a specified format, using the rmarkdown package’s render function to call pandoc (a universal document converter written in Haskell). If you specify a knit: field in an Rmarkdown YAML header you can replace the default function (rmarkdown::render) that the input file and encoding are passed to with any arbitrarily complex function. Make prettier documents by reusing chunks in RMarkdown.

No revelations here, just a little R tip for generating more readable documents.

Make prettier documents by reusing chunks in RMarkdown

Original with lots of code at the top There are times when I want to show code in a document, but I don’t want it to be the first thing that people see. What I want to see first is the output from that code. In this silly example, I want the reader to focus their attention on the result of myFunction(), which is 49. Function use before definition = error I could define myFunction() later in the document but of course that leads to an error when the function is called before it has been defined. Solution: use the chunk option ref.label to call chunk 2 from chunk 1. The result of calling chunk2 from chunk1 And here’s the result. Rmarkdown: Alter Action Depending on Document. Can I see a show of hands for those who love rmarkdown?

rmarkdown: Alter Action Depending on Document

Yeah me too. One nifty feature is the ability to specify various document prettifications in the YAML of a .Rmd document and then use: Lot of reports with a single click! Suppose you want to create a huge number of pdf files through RMarkdown and pandoc, each of them including a statistical analysis on a part of your data, for example on each row of your data frame. You need to write a .R file with cycles from 1 to the number of rows of your data set the instructions contained in a .Rmd file. Suppose that: your data set name is data.csv and its path is datapath/data.csv’’your .Rmd file name is report.Rmd and its path is basepath/report.Rmd’’ Embedding RData files in Rmarkdown files for more reproducible analyses. For those of us interested in reproducible analysis, Rmarkdown is a great way of communicating our code to other researchers.

Rstudio, in particular, makes it very easy to create attractive HTML document containing text, code, and figures, which can then be sent to colleagues or put on the internet for anyone to see. If you aren't using Rmarkdown for your statistical analyses, I recommend you start; you'll never go back to simple script files again (and your colleagues won't want you to).

Templates

Pander: A Pandoc writer in R. Pander is an R package containing helpers to return Pandoc's markdown even automatically from several type of R objects with a general S3 method. The package is also capable of exporting/converting complex Pandoc documents (reports) in three ways at the moment: create somehow a markdown text file (e.g. with brew, knitr or any scripts of yours, maybe with Pandoc.brew - see just below) and transform that to other formats (like HTML, odt, pdf, docx etc.) with Pandoc.convert -- just like pandoc function in knitr,users might write some reports in a forked version of brew syntax resulting in a pretty Pandoc document (where each R object are automatically transformed to nicely formatted table, list etc.) and also in a bunch of other formats (like HTML, odt, pdf, docx etc.)

,Example: this README.md is cooked with Pandoc.brew based on inst/README.brew and also exported to HTML. Rmarkdown/pdf_document.R at master · rstudio/rmarkdown · GitHub. Pandoc - Pandoc User’s Guide. Pandoc [options] [input-file]… Pandoc is a Haskell library for converting from one markup format to another, and a command-line tool that uses this library. It can read markdown and (subsets of) Textile, reStructuredText, HTML, LaTeX, MediaWiki markup, Haddock markup, OPML, Emacs Org-mode and DocBook; and it can write plain text, markdown, reStructuredText, XHTML, HTML 5, LaTeX (including beamer slide shows), ConTeXt, RTF, OPML, DocBook, OpenDocument, ODT, Word docx, GNU Texinfo, MediaWiki markup, EPUB (v2 or v3), FictionBook2, Textile, groff man pages, Emacs Org-Mode, AsciiDoc, InDesign ICML, and Slidy, Slideous, DZSlides, reveal.js or S5 HTML slide shows.

It can also produce PDF output on systems where LaTeX is installed. Using pandoc If no input-file is specified, input is read from stdin. Pandoc -o output.html input.txt. Jimhester/knitrBootstrap · GitHub. Fast-track publishing using the new R markdown – a tutorial and a quick look behind the scenes. The new R Markdown (rmarkdown-package) introduced in Rstudio 0.98.978 provides some neat features by combining the awesome knitr-package and the pandoc-system. The system allows for some neat simplifications of the fast-track-publishing (ftp) idea using so called formats.

I’ve created a new package, the Grmd-package, with an extension to the html_document format, called the docx_document. The formatter allows an almost pain-free preparing of MS Word compatible web-pages. In this post I’ll (1) give a tutorial on how to use the docx_document, (2) go behind the scenes of the new rmarkdown-package and RStudio ≥ 0.98.978, (3) show what problems currently exists when skipping some of the steps outlined in the tutorial.

Tutorial on how to use ftp with the rmarkdown implementation A major improvement in the new rmarkdown is the YAML set-up. As mentioned above, the Grmd-package also contains a formatter, the docx_document format that is a wrapper around the html_document. Interactive documents: An incredibly easy way to use Shiny. R Markdown’s new interactive documents provide a quick, light-weight way to use Shiny. Floating table of contents for your html reports using knitr. If you love knitr and rstudio and use them to produce long reports, you probably know that you can produce a table of contents in your html (and pdf) documents. In the newer rstudio (Version 0.98.801 or later) you do it by requesting a toc in the doc header, something like this. title: "cssTest" output: html_document: toc: yes But wouldn’t it be nice if the table of contents, instead of being stuck at the top of the document, was available at the edge of the browser window and doesn’t move when you scroll?

This post: shows a live example of a floating toc and also tells you how easily to produce one automatically by adding a custom css file. Comments and improvements are welcome! R Markdown v2. Ly — Plot with ggplot2 and plotly within knitr reports.