Fast-track publishing using the new R markdown – a tutorial and a quick look behind the scenes

The new rmarkdown revolution has started. The image is CC by Jonathan Cohen.
The new rmarkdown revolution has started. The image is CC by Jonathan Cohen.

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. It is now much easier to set-up environments for your documents, all you need to look at is the function arguments in the documentation and provide those in the file. You have four different default document types where some options shared while other are output-specific: html_document, pdf_document, word_document, or markdown_document.

As mentioned above, the Grmd-package also contains a formatter, the docx_document format that is a wrapper around the html_document. It has the same options as the html_document with a few additions/defaults adapted to the concept of fast-track-publishing. As the package depends on rmarkdown it can currently only installed from Github (CRAN does not allow dependencies on packages outside CRAN) and in order to install the package you need to use the devtools-package:

After this you simply put at the top of your Rmd-document:

If you may notice that after adding the above change from html_document to the custom Gmisc::docx_document-format the choice knit-box intelligently changes from:




As RStudio is uncertain of how to approach this new format. Note: interestingly this also occurs if you happen to set the rstudio.mardownToHTML option using options().

For this tutorial we will use the Rmd document found in the Github ftp-repository. It is a simple example using my two main packages. Thus the new Rmd file is:

with the accompanying setup_and_munge.R script:

Will provide the following browser output:


Copy-paste directly from browser

Copy-pasting directly from the web-browser works! The current compatibility that I’ve checked are (Windows 8.1):

  • RStudio viewer ≤ 0.98.978: works for headers, text, and tables but not for images.
  • Internet explorer ≥ v.11: works for all (headers, text, tables, and images).
  • Chrome ≥ v.36: works for all (headers, text, tables, and images).
  • Firefox ≤ v.31: works for no elements.

Just choose a compatible browser from the above list, open the .html-file, select everything, copy->paste it directly into Word, and you’ll get the following beauty:

The end result in MS Word
The end result in MS Word

Go through LibreOffice

Going through LibreOffice will produce a very similar result to the above but you will additionally also have the image at the bottom (that is if you are not copy-pasting from Chrome/IE).

To open the file, navigate to it using the explorer, right click on the html-file and open it in LibreOffice as below (click on the image to enlarge):


And you will get the following:


Staying here and working in LibreOffice is probably an excellent alternative but if you still want the .docx-file for MS Word then go to File > Save As.. (or press Ctrl+Shift+S) and choose the .docx format as below:


Now simply open the .docx-file in Word

I hope you found this tutorial useful and good luck with getting your masterpiece published!

Behind the scenes with the rmarkdown-package and RStudio

The R markdown v2 took some time getting used to. One of the first changes was that the environment no longer includes the knitr-package. To access the knitr options we now have to either include the package manually or use the :: operator. E.g. when setting the knitr chunk options:

As LibreOffice ignores some of the formatting presented in the CSS the current alternative is to specify at the element-level the option through post-processing the html-document. This was actually a little tricky to figure out, the rmarkdown runs the render() function that has the option of applying a post-processor to the output. Unfortunately this can not be provided to the html_document as this has it’s own post-processor that it attaches to the output_format object. As I didn’t want to replace the default post-processor with my own, I needed to write a rather complex formatter that runs the new post-processor after the first, hence core of the docx_document() formatter does this (the actual code also does some additional cleaning):

Dealing with high-resolution images

High-resolution images (DPI ≥ 300) are frequently needed for press. As in the example you need to specify the dip=300 in the knitr-chunk. Doing this will unfortunately blow upp the image to an uncomfortable large size and it may be therefore interesting limiting the screen output size. For this you use the out.width and the out.height. Since these elements are not available in markdown knitr inserts a plain <img src=”…” /> element without captions. To remedy this you can set force_captions=true as in the example. It will use the XML package and replace <p><img …/></p> with an element identical to the pandoc image with caption (see the function that is invoked by docx_document below).

A few minor RStudio-tips

For those of you developing packages in RStudio, here are some neat functions that I currently like (not specific to the latest version):

  • Ctrl+Shift+L Calls the devtools::load_all(“.”) from within a package. This gives you access to all the private functions and is much faster than rebuilding the full package. Note: if you haven’t installed the package the key combination does not work and you won’t even get an error.
  • Ctrl+Shift+T Runs the package tests.
  • Ctrl+Alt+left/right arow Quickly switches between tabs.
  • Ctrl+Shift+F find in all files, very handy for navigating.

Issues that the current solution resolves

I’ve gotten quite a lot of response to the ftp-concept, especially now that the new functionality that caused some new bugs to appear with the old ftp-approach. In this section we’ll look a little at what happens when skipping some of the steps.

Direct pandoc Word output

I believe that this will be the future but unfortunately pandoc’s table abilities lacks the finesse that I like. In order to get reviewers quickly acquainted with the study results I think that nice tables can’t hurt. E.g. the table below is far from satisfactory in my opinion:


Opening html in Word

Importing html-documents into Word has for some unknown reason not been a priority for the Microsoft developers. I’m surprised that the import isn’t more advanced more than two decades since the web began. Currently the major problem is that you loose the table cell borders:


Opening in LibreOffice and copy-pasting from there

Somewhat odd that this doesn’t work. When copy-pasting from LibreOffice to Word the formatting of the headers suddenly end up with a 14pt margin below, see below image:


Opening in Firefox and copy-pasting into Word

As you see below, most of the formatting is lost when using this approach:


Flattr this!

This entry was posted in R, Tutorial and tagged , . Bookmark the permalink.

23 Responses to Fast-track publishing using the new R markdown – a tutorial and a quick look behind the scenes

  1. George Fisher says:, she’s a-no work

  2. Andy Barbour says:

    This is a very nice way to both maintain reproducibility and deal with silly journal requirements (i.e., using MS Word). Thanks!

    Would just like to note that the Rmd knitting failed until I upgraded Gmisc to 0.6.7, which fixed the missing function (figCapNoNext) error.

  3. Mu-Yang Hsieh says:


    I am a new-learner of R, and I recently discovered your site by R-blogger.

    Your Gmisc package and this tutorial are fantastic! These files and tutorials really helped a lot! Your work in this field is much appreciated!

    The R rocks!

  4. Jon Chung says:

    Hi Max,

    Nice work with the Grmd package.

    I have a question about table formatting. Is it possible to not have the row.names show in the final table? Your htmlTable function is really handy and I am trying to figure out how to remove row.names from the final table.


    • Max Gordon says:

      Thank you. I can add the option of having rowname = FALSE in the next Gmisc version, I haven’t thought of it being an issue but it’s a simple fix. The easiest way is to simply use rownames(my_matrix) < - NULL before calling htmlTable and then it will automatically leave out any row names unless you specify the rowname argument.

      By the way, I'm currently working on version 1.0 of the Gmisc-package where I'm considering doing some API-changes to the htmlTable-function. As the author it is difficult to understand what sections are difficult/illogical. I have so far built most of the functionality to be as similar to the Hmisc::latex function but it for instance uses the rowname and rowlabel arguments which I find confusing and I'm considering changing the argument rowname to the plural form, rownames. If you have thoughts on the function design please let me know.

  5. Andrius says:

    I have some problems, during reproducing your example.
    1. I installed all packages;
    2. I copied Setup_and_munge.R and saved into the working directory;
    3. I copied Rmd file and saved into the working directory;
    4. knitr

    I have the following error message:

    Quitting from lines 31-73 (test.Rmd)
    Fehler in htmlTable.default(output_data, align = "rrrr", rgroup = rgroup, :
    You have set both the old parameter name: 'rgroupCSSseparator' and the new parameter name: 'css.rgroup.sep'.
    Calls: ... withVisible -> eval -> eval -> htmlTable -> htmlTable.default

    Can you help me to solve this problem?

    • Max Gordon says:

      I recently updated the package to 1.0. There are a few API-changes that I’ve been wanting to do for a while and this bug occurs because there already is a default value for the rgroupCSSseparator. Just remove that argument.

      • Chris says:

        Max – Thanks, your suggestion helped with Andrius’s problem (which I was having too), but I’m now getting the following problem:

        Error in getDescriptionStatsBy(x = ds[ == FALSE, vn], by = outcome[ == : You have set both the old parameter name: 'show_missing' and the new parameter name: 'useNA''.

        Any ideas?

        • Chris says:

          Sorry, missing this:

          You have set both the old aprameter name: 'show_missing' and the new parameter name: 'useNA'.

        • Michael G says:

          Chris, how did you achive that? I am failing to find rgroupCSSseparator in “printCrudeAndAdjustedModel” function so that I remove the arguement as suggested

          • Chris says:

            Just delete the line rgroupCSSseparator="", in the main Rmarkdown file with the title "A fast-track-publishing demo".

          • Chris says:

            Sorry, struggling with these html tags, trying again!

            Just delete the line:’rgroupCSSseparator=””,’
            in the main Rmarkdown file with the title “A fast-track-publishing demo”.

  6. Michael G says:

    I have the following error: You have set both the old parameter name: ‘rgroupCSSseparator’ and the new parameter name: ‘css.rgroup.sep’.

    and you recomend removing rgroupCSSseparator, how can I do this in installed package?

    • Max Gordon says:

      Sorry, I haven’t had time to fix the Greg-package to work properly with the 1.0 versions. I’ve now updated both the Greg-package and the ftp-code to work with the new 1.0 versions of htmlTable and Gmisc. There is still an annoying issue associated with knitr that I need to solve with the plus-minus sign not being converted.

  7. devitto says:

    Dear Max,

    I’ve tried to run this code. I installed first Gmisc, Greg, htmlTable – all with ref=”v1.0″.

    And I’m still getting this error: “Error in getDescriptionStatsBy(x = ds[ == FALSE, vn], by = outcome[ == :
    You have set both the old parameter name: ‘show_missing’ and the new parameter name: ‘useNA’.”

    I even removed the code for checking this, but this didn’t cancel the error.

    I looked for this in GetDescriptiontatsBy, but couldn’t find any “show_missing”, it must be passed from some parent function.

    I’m giving up 🙁

    • devitto says:

      I’ve tried also ref=”devevelop”, as stated here
      With no success. I installed:
      1. devtools::install_github(“gforge/Greg”, ref=”v1.0″)
      2. devtools::install_github(“gforge/htmlTable”, ref=”develop”)
      3. devtools::install_github(“gforge/forestplot”, ref=”develop”)
      4. devtools::install_github(“gforge/Gmisc”, ref=”develop”)

      any combination of v1.0 and develop doesn’t work. Issue still remains.

      • Max Gordon says:

        Sorry to hear that my packages are giving you a headache. The Greg-package has a lot of dependencies and is therefore sensitive to change. I’ve changed all the show_missing to useNA and it should hopefully work now, it seems to work for me when running with the CRAN/master versions of the packages. Try and download the Greg 1.0.1 version and see if you still get the error.

  8. devitto says:

    By the way, I even cannot run the examples for printCrudeAndAdjustedModel from the page:

    I prepared data and run all codes one by one.

    > printCrudeAndAdjustedModel(fit, desc_digits=0,
    + caption=”Crude and adjusted estimates”,
    + desc_column=TRUE,
    + add_references=TRUE,
    + ctable=TRUE)
    Error in value.chk(at, i, factors[[jf]], 0, Limval) :
    illegal levels for categorical variable: 54 55
    Possible levels: Male Female

    I would really like to show this library to my boss as it produces great output (looking at the screenshots), but I’m unable even to run your examples.

    • Max Gordon says:

      Odd, it seems that stats::get_all_vars() that I use for retrieving the regression variables can’t handle when the model is specified with the dataset. It works after changing the model to:

      fit < - cph(Surv(time, status=="Melanoma death") ~ sex + age + thickness + ulcer, data=melanoma)

      You can find an updated version here.

  9. Calum says:

    Hi Max

    Thanks for your amazing work on this: there are not enough tools like this available for medical researchers.

    I have a question about the object generated by printCrudeAndAdjustedModel. I would like to ‘edit’ the rows so that not all the variables are shown, but I can’t work out how to manipulate the object. Can you help?

    The reason that I am asking is because I would like, ideally, to be able to adjust for specific variables. For example: I might have a number of interesting variables that I would like to just adjust with standard demographics. The columns would then be: unadjusted | adjusted(by demographics). Without seriously altering your package I considered ‘stacking’ a series of individual regression outputs. Which brings me back to the question of how I can manipulate the output of printCrudeAndAdjustedModel!

    Thanks again.

    • Max Gordon says:

      Thanks! The returned object is actually just a matrix with some extra attributes set. You can easily manipulate the object just as any matrix object, see below examples:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.