Installing Pandoc from R (on Windows) – using the {installr} package

The R blogger Rolf Fredheim has recently wrote a great piece called “Reproducible research with R, Knitr, Pandoc and Word“, where he advocates for Pandoc as an essential part of reproducible research workflow in R, in helping to turn documents which are knitted in R into high quality Word for exchanging with our colleagues. It is a great post, with many useful bits of code, and I wanted to supplement it with one missing function: “install.pandoc“.

Update: the install.pandoc function is now part of the {installr} package.

The install.pandoc() function, as the name implies, will recognize the latest version of pandoc, download its installer to your computer, and run it. Making it even simpler for you to experiment with adding pandoc into your workflow.

Here is how to use it:

 
# installing/loading the package:
if(!require(installr)) { install.packages("installr"); require(installr)} #load / install+load installr
 
# Installing pandoc
install.pandoc()

Note: the code in this function is based on GERGELY DARÓCZI’s coding in his answer on the Q&A forum StackOverflow, and also G. Grothendieck for the non-XML addition to the function. I thank them both!

pandoc_diagram

  • http://twitter.com/RolfFredheim Rolf Fredheim

    This is brilliant, thank you!

    • http://www.r-statistics.com/ Tal Galili

      Thanks Rolf :)
      What do you think, do you think there is a need for more such functions?

  • gokhan

    Thanks very good idea! how about mac and linux?

    • http://www.r-statistics.com/ Tal Galili

      Thanks gokhan,
      I would have loved adding mac/linux to the mix. However, my main tool these days is Windows OS, so I can’t develop/debug the relevant code for other OS systems. If you (or someone else) is ready to prepare the code – I’d be happy to add it to the post.

  • Gergely Daróczi

    Thank you Tal for sharing that piece of code.

    I just wanted to let you know that Tyler Rinker has also published the function in his reports package: https://github.com/trinker/reports

    And I would be really happy to extend the function with a Linux solution, but installing an up-to-date version of Pandoc on Linux can be really pain in the @ss as `cabal-install` is needed to compile the binary – not like on Windows.

  • Pingback: Updating R from R (on Windows) – using the {installr} packageR-statistics blog | R-statistics blog

  • Pingback: Write MS-Word documents using R (with as little overhead as possible)R-statistics blog | R-statistics blog

  • Rees

    I am running R 2.15.3 and tried your code.

    > # installing/loading the package:
    > if(!require(installr)) { install.packages(“installr”); require(installr)} #load / install+load installr
    >
    > # Installing pandoc
    > install.pandoc()
    Error in download.file(exe_URL, destfile = exe_filename, mode = “wb”) :
    unsupported URL scheme

    As a newbie, I don’t know what this error tells me, except that it seems R cannot find the website (URL) that has Pandoc. What can I do?

    Thank you.

    • http://www.r-statistics.com/ Tal Galili

      Thank you for the bug report Rees.

      I see that the guys at pandoc have changed their installer type:

      http://code.google.com/p/pandoc/downloads/list

      From now on, it appears they no longer offer a .exe file, but a .msi file.

      I will fix the function to be able to handle it, and also make a change to the error massage produced.

      Thank you for the bug report, I will update here with another comment once I’m done.

      • http://www.r-statistics.com/ Tal Galili

        o.k., I finished fixing the code and updating it to github.

        It will take another month or so before I am allowed to push this version to CRAN again. In the meantime, you (and others) can install the latest version of {installr} by running:

        if (!require(‘devtools’)) install.packages(‘devtools’); require(‘devtools’)
        install_github(‘installr’, ‘talgalili’)

        And then you can run:
        require(installr)
        install.pandoc()

        And it’s now supposed to work (it works for me)

        With regards,
        Tal

    • http://www.r-statistics.com/ Tal Galili

      p.s: in the meantime, you can run the function in this way:
      install.pandoc(use_regex = FALSE)
      And it will download and run the installer with no problem.