How to make R Markdown use an updated version of pandoc on my Mac

Hi, all,

first of all, my system versions:

devtools::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.5.1 (2018-07-02)
#>  os       macOS High Sierra 10.13.6   
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  it_IT.UTF-8                 
#>  ctype    it_IT.UTF-8                 
#>  tz       Europe/Rome                 
#>  date     2018-12-13                  
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version date       lib source        
#>  assertthat    0.2.0   2017-04-11 [1] CRAN (R 3.5.0)
#>  backports     1.1.2   2017-12-13 [1] CRAN (R 3.5.0)
#>  callr         3.1.0   2018-12-10 [1] CRAN (R 3.5.0)
#>  cli           1.0.1   2018-09-25 [1] CRAN (R 3.5.0)
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 3.5.0)
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 3.5.0)
#>  devtools      2.0.1   2018-10-26 [1] CRAN (R 3.5.1)
#>  digest        0.6.18  2018-10-10 [1] CRAN (R 3.5.0)
#>  evaluate      0.12    2018-10-09 [1] CRAN (R 3.5.0)
#>  fs            1.2.6   2018-08-23 [1] CRAN (R 3.5.0)
#>  glue          1.3.0   2018-07-17 [1] CRAN (R 3.5.0)
#>  highr         0.7     2018-06-09 [1] CRAN (R 3.5.0)
#>  htmltools     0.3.6   2017-04-28 [1] CRAN (R 3.5.0)
#>  knitr         1.21    2018-12-10 [1] CRAN (R 3.5.1)
#>  magrittr      1.5     2014-11-22 [1] CRAN (R 3.5.0)
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 3.5.0)
#>  pkgbuild      1.0.2   2018-10-16 [1] CRAN (R 3.5.0)
#>  pkgload       1.0.2   2018-10-29 [1] CRAN (R 3.5.0)
#>  prettyunits   1.0.2   2015-07-13 [1] CRAN (R 3.5.0)
#>  processx      3.2.1   2018-12-05 [1] CRAN (R 3.5.0)
#>  ps            1.2.1   2018-11-06 [1] CRAN (R 3.5.0)
#>  R6            2.3.0   2018-10-04 [1] CRAN (R 3.5.0)
#>  Rcpp          1.0.0   2018-11-07 [1] CRAN (R 3.5.0)
#>  remotes       2.0.2   2018-10-30 [1] CRAN (R 3.5.0)
#>  rlang         0.3.0.1 2018-10-25 [1] CRAN (R 3.5.0)
#>  rmarkdown     1.11    2018-12-08 [1] CRAN (R 3.5.0)
#>  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 3.5.0)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 3.5.0)
#>  stringi       1.2.4   2018-07-20 [1] CRAN (R 3.5.0)
#>  stringr       1.3.1   2018-05-10 [1] CRAN (R 3.5.0)
#>  testthat      2.0.1   2018-10-13 [1] CRAN (R 3.5.0)
#>  usethis       1.4.0   2018-08-14 [1] CRAN (R 3.5.0)
#>  withr         2.1.2   2018-03-15 [1] CRAN (R 3.5.0)
#>  xfun          0.4     2018-10-23 [1] CRAN (R 3.5.0)
#>  yaml          2.2.0   2018-07-25 [1] CRAN (R 3.5.0)
#> 
#> [1] /Library/Frameworks/R.framework/Versions/3.5/Resources/library

Now, a bit of context: I'm having some difficulties with rendering a Markdown file generated by RStudio, outside of RStudio;

Table in Markdown file generated by pandoc doesn't render correctly - #5 by technocrat

Following a suggestion by technocrat in the above thread, I'd like to verify if updating pandoc makes any difference. However, even if the version of pandoc installed on my Mac is (nearly) up to date:

$ which pandoc
/anaconda2/bin/pandoc
$ pandoc -v
pandoc 2.2.3.2
Compiled with pandoc-types 1.17.5.1, texmath 0.11.0.1, skylighting 0.7.2
Default user data directory: /Users/andreapanizza/.pandoc
Copyright (C) 2006-2018 John MacFarlane
Web:  http://pandoc.org
This is free software; see the source for copying conditions.
There is no warranty, not even for merchantability or fitness
for a particular purpose.

the one used by RStudio is quite older. When I click the Knit to HTML button, I get the following messages in the R Markdown console:

/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m -RTS test.utf8.md --to html4 --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rmarkdown/rmd/h/default.html --no-highlight --variable highlightjs=1 --variable 'theme:bootstrap' --include-in-header /var/folders/6m/3vm9vws14tzb0nyh0yldv56r0000gn/T//RtmpiHKw3r/rmarkdown-str1611123f8f80.html --mathjax --variable 'mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'

which I take as to imply that the version of pandoc used by RStudio is not the Anaconda one, but the one in /Applications/RStudio.app/Contents/MacOS/pandoc/. This version is 2 years older:

$ /Applications/RStudio.app/Contents/MacOS/pandoc/pandoc -v
pandoc 1.19.2.1
Compiled with pandoc-types 1.17.0.4, texmath 0.9, skylighting 0.1.1.4
Default user data directory: /Users/andreapanizza/.pandoc
Copyright (C) 2006-2016 John MacFarlane
Web:  http://pandoc.org
This is free software; see the source for copying conditions.
There is no warranty, not even for merchantability or fitness
for a particular purpose.

Now, I'd like to have RStudio use the newer pandoc version. However, this document says that RStudio uses the first version in my $PATH:

https://support.rstudio.com/hc/en-us/community/posts/239075008-pandoc

My $PATH is

$ echo $PATH
/anaconda2/bin:/usr/local/bin:/usr/bin:/bin:[...]

Thus RStudio should already be using the Anaconda version of pandoc...What's wrong here?

PS @yihui sorry for mentioning you, but wouldn't it be better for RStudio/R Markdown to use the current version of Pandoc, rather than one which is two years old? Just an idea, I may of course be wrong here.

1 Like

Dear Andrea,

A possible solution is to use a .Renviron file (located in your R project)

# .Renviron content 

RSTUDIO_PANDOC="/anaconda2/bin/pandoc"
PATH="/anaconda2/bin/pandoc:${PATH}"

Hope this helps

David

3 Likes

Hi, David,

thanks for the suggestion, but it doesn't work:

$ cat .Renviron
# .Renviron content

RSTUDIO_PANDOC="/anaconda2/bin/pandoc"
PATH="/anaconda2/bin/pandoc:${PATH}"

When I Knit to HTML, I get the following error:

Errore: pandoc version 1.12.3 or higher is required and was not found (see the help page ?rmarkdown::pandoc_available).
Esecuzione interrotta

Running rmarkdown::pandoc_available as suggested by the informative error message, I get:

> rmarkdown::pandoc_available()
[1] FALSE

Thus, it looks like modifying the .Renviron file doesn't work.

From your output of echo $PATH, it appears that you added /anaconda2/bin to PATH in something like .bash_profile, which usually won't be recognized by RStudio or R. A couple of possible solutions:

  1. Symlink (i.e., ln -s) pandoc and pandoc-citeproc to a common path in PATH, such as /usr/local/bin. This path should be recognized by all packages in your system. Personally I haven't succeeded in tweaking PATH in .Renviron (and I don't really know how R handles this file; I feel it is different with .bash_profile).

  2. Add /anaconda2/bin to the PATH variable in a way that R can recognize, e.g. do Sys.setenv(PATH = ...) in your .Rprofile.

  3. The RStudio Preview version has actually bundled Pandoc 2.x: https://www.rstudio.com/products/rstudio/download/preview/

1 Like

Thanks Yihui! I finally found the solution: the one thing which wasn't working in David Gohel's solution was that pandoc in /anaconda2/bin is not a directory containing pandoc and pandoc-citeproc, but it's actually a binary file. So the correct solution is:

# .Renviron content 

RSTUDIO_PANDOC="/anaconda2/bin/pandoc"
PATH="/anaconda2/bin/:${PATH}"

Now RStudio is using both the up-to-date pandoc and pandoc-citeproc. Yay!

/anaconda2/bin/pandoc +RTS -K512m -RTS test2.utf8.md --to html4 --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash+smart --output test2.html --email-obfuscation none --self-contained --standalone --section-divs --template /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rmarkdown/rmd/h/default.html --no-highlight --variable highlightjs=1 --variable 'theme:bootstrap' --include-in-header /var/folders/6m/3vm9vws14tzb0nyh0yldv56r0000gn/T//Rtmpdyp7IM/rmarkdown-strd8d961269278.html --mathjax --variable 'mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' --filter /anaconda2/bin/pandoc-citeproc

PS good to know that RStudio 1.2.x will bundle Pandoc 2.x!

2 Likes

Great. I guess you only need to tweak the PATH variable. There is no need to define RSTUDIO_PANDOC. If rmarkdown finds a newer version from PATH, it will just use the newer version.

3 Likes

Indeed, you are right! Just this single line in the .Renviron file

# .Renviron content 

PATH="/anaconda2/bin/:${PATH}"

is enough to get RStudio to use the Anaconda version of pandoc. Great! pandoc-citeproc also works flawlessly, and the bibliography is generated correctly.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.