RMarkdownWord

Andreas Busjahn
2016-01-28

Reporting

Creating output for collaborators/customers:

  • Word document with background and methods
  • Excel file with sheets for all tables
  • PDF file with all figures

R Markdown

  • Dynamic documents created by script
  • can create HTML, PDF, LaTeX, and Word
  • apparently under active development, surpring changes once in a while

How to create

In RStudio:

File->New File->R Markdown

Will ask for some basic information and type (can be changed later!)

Creates a prefilled document with examples

OR

load your template xxx.Rmd

Change content to your needs

Click on the clew of yarn with needles and knit your docx

This saves your script!!

Type of content in script

  • Plain text
    • here you put things like methods, explanations …
    • basic formatting like bold, italics, list etc
    • can contain (small) parts of R code, output will be integrated in text
  • Code chunks
    • like ordinary scripts
    • defined by 3backticks{r} for begin and 3backticks for end
    • header may contain name (strongly suggested) and options

'Normal' script vs. chunk

  • Within chunk, commands can be run the usual way
  • Ctrl-A Ctrl-Enter will NOT work (runs plain text as well)
  • You may run a complete chunk (Ctrl-Alt-C) or all chunks (Ctrl-Alt-R)
  • Reindenting code (Ctrl-A Ctrl-I) will change plain text and global header (BEWARE!)

Chunk options

  • Can be set for each chunk in header:

    {r import, echo=FALSE,warning=FALSE, results='hide',message=FALSE}

  • Can be set globally

    opts_chunk$set(message=F,warning=F,comment=NA,echo=F, dpi=300,fig.width=plotsize*1.5, fig.height=plotsize*1.2)

  • I tend to suppress source code echoing in reports

Working with template

  1. Create Word file with R Markdown
  2. Change formats (e.g. A4, margins)
  3. Add header / footer …
  4. Save
  5. Use as template:

output:

  word_document:

      reference_docx: template.docx

  • Currently, I loose formatting for headers :-(

To Dos...

  • testing of different table packages
  • solving template problem
  • dynamic page orientation (landscape)
  • testing grids with ggplots
  • defining output folder