1

I asked a related question: check if a program is installed

But am refraining from answering until I've tested the solutions for myself on all three systems. I can get pandoc to work from within R on a windows machine but on linux I get this error/response for each method from the R terminal:

1:

> system('pandoc -v')
sh: 1: pandoc: not found

2:

> myPaths <- c("pandoc", 
+              "~/.cabal/bin/pandoc", 
+              "~/Library/Haskell/bin/pandoc", 
+              "C:\\PROGRA~1\\Pandoc\\bin\\pandoc") 

> Sys.which(myPaths)
                           pandoc               ~/.cabal/bin/pandoc 
                               ""   "/home/tyler/.cabal/bin/pandoc" 
     ~/Library/Haskell/bin/pandoc C:\\PROGRA~1\\Pandoc\\bin\\pandoc 
                               ""                                "" 

3:

> Sys.which("pandoc")
pandoc 
"" 

You may think I don't have pandoc installed and on the path but I believe I do. From a clean terminal session:

> tyler@trinker ~ $ echo $PATH
> /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/tyler/.cabal/bin

and

tyler@trinker ~ $ pandoc -v
pandoc 1.10.1
Compiled with citeproc-hs 0.3.7, texmath 0.6.1.3, highlighting-kate 0.5.3.6.
Syntax highlighting is supported for the following languages:
    Actionscript, Ada, Alert, Alert_indent, Apache, Asn1, Asp, Awk, Bash,
    Bibtex, Boo, C, Changelog, Clojure, Cmake, Coffee, Coldfusion, Commonlisp,
    Cpp, Cs, Css, Curry, D, Diff, Djangotemplate, Doxygen, Doxygenlua, Dtd,
    Eiffel, Email, Erlang, Fortran, Fsharp, Gnuassembler, Go, Haskell, Haxe,
    Html, Ini, Java, Javadoc, Javascript, Json, Jsp, Julia, Latex, Lex,
    LiterateCurry, LiterateHaskell, Lua, Makefile, Mandoc, Matlab, Maxima,
    Metafont, Mips, Modula2, Modula3, Monobasic, Nasm, Noweb, Objectivec,
    Objectivecpp, Ocaml, Octave, Pascal, Perl, Php, Pike, Postscript, Prolog,
    Python, R, Relaxngcompact, Rhtml, Ruby, Scala, Scheme, Sci, Sed, Sgml, Sql,
    SqlMysql, SqlPostgresql, Tcl, Texinfo, Verilog, Vhdl, Xml, Xorg, Xslt, Xul,
    Yacc, Yaml
Copyright (C) 2006-2013 John MacFarlane
Web:  http://johnmacfarlane.net/pandoc
This is free software; see the source for copying conditions.  There is no
warranty, not even for merchantability or fitness for a particular purpose.

How can I make R on Linux Mint recognize pandoc? (I'm newer to Linux)

Community
  • 1
  • 1
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • works for me on Ubuntu 12.04. However I have pandoc in /usr/bin/. – EDi Feb 19 '13 at 21:54
  • 2
    Dito. `system("which pandoc")` returns `/usr/bin/pandoc`. I think you overcomplicate somethings there. This isn't Windows. Once you installed a binary, "it is there". – Dirk Eddelbuettel Feb 19 '13 at 21:55
  • I was trying to use a newer version of pandoc via the cabal update as seen [here](http://johnmacfarlane.net/pandoc/installing.html). However it says on that page it puts it in `~/.cabal/bin`. I imagine if I dropped those files to the `usr` directory it would work as expected but I don't know where to even find the usr directory. However using `sudo apt-get install pandoc` it now works on R. – Tyler Rinker Feb 20 '13 at 00:11
  • @DirkEddelbuettel Like Tyler mentioned if you install the latest version via cabal it isn't in /usr/bin. Tyler - Are you using RStudio when R won't find pandoc via Sys.which or are you just using a terminal? For me it works in the terminal but not with RStudio. It seems RStudio doesn't read in .bashrc so ~/.cabal/bin isn't in your path when using RStudio. – Dason Feb 20 '13 at 00:58
  • @Dason thanks for the response. R terminal – Tyler Rinker Feb 20 '13 at 01:00
  • WTF is `cabal` and why would anyone use it when you could just `sudo apt-get install` ? – Dirk Eddelbuettel Feb 20 '13 at 01:08
  • 1
    @TylerRinker, I don't understand. The solution I suggested detects Pandoc. It just means that when you write a Pandoc system call, you can't just do something like `system("pandoc -o etc...")` but would have to do something like `system("~/.cabal/bin/pandoc -o etc...")` In other words, you need to modify the system call to use the full path. – A5C1D2H2I1M1N2O1R2T1 Feb 20 '13 at 02:22
  • Oh I get you. THat's a bit above my pay grade of understanding at the moment. – Tyler Rinker Feb 20 '13 at 02:37
  • Probably take the piece you supply and paste it together appropriately? What I'm doing currently [(LINK)](https://github.com/trinker/reports/blob/master/R/html5.R) is what you say is not good across systems. If I get what you're saying your approach means the user doesn't have to worry about setting things up in the .profile? – Tyler Rinker Feb 20 '13 at 02:38
  • @TylerRinker, I've posted a skeleton of my answer below. – A5C1D2H2I1M1N2O1R2T1 Feb 20 '13 at 03:17

2 Answers2

4

I was having issues with this as well. I installed pandoc via cabal as well. If you install via apt-get there shouldn't be an issue. If I launched R from a terminal I had no issues but attempting to detect pandoc from within RStudio gave some troubles. The reason is that RStudio doesn't read in your bash environment variables so if you modify the path in .bashrc RStudio won't detect that. A solution is to modify the path via .profile instead.

Add this to the bottom of your .profile file and remove the path modfication in your .bashrc file and you should be able to recognize pandoc from within R.

if [ -d "$HOME/.cabal/bin" ] ; then
    PATH="$PATH:$HOME/.cabal/bin"
fi
Dason
  • 60,663
  • 9
  • 131
  • 148
  • The problem with this as a general solution is that you can't really go around advising everyone who would be using your package to do this when they are most likely to have already done it using `.bashrc`, hence my suggestion to use `Sys.which()` with a set of typical paths to Pandoc. In other words, maybe it will solve Tyler's problem for himself, but not for any others who might be using his function. – A5C1D2H2I1M1N2O1R2T1 Feb 20 '13 at 02:29
  • @Ananda the problem wasn't in determining if pandoc was installed but in RStudio recognizing it was installed. If I launched from the temrinal Rstudio recognized it. If I clicked the .Rproj file to open RStudio it didn't recognize that pandoc was installed. Something about bash that more experienced linux users understand that I don't grasp quite yet. – Tyler Rinker Feb 20 '13 at 02:35
  • @AnandaMahto I agree that you can't force a user to change from using .bashrc to .profile but this does solve the issue of RStudio not recognizing pandoc from `system`. I think your solution is probably the best option because it's more robust so in Tyler's case of wishing to deploy this to a general audience it's a good measure to take. But for those of us that just want RStudio to recognize our modified path... this works wonders. – Dason Feb 20 '13 at 02:38
  • @Dason, alternative posted. I'll definitely be trying to check up on this RStudio issue when I have a free moment, and will post back if I find anything out. – A5C1D2H2I1M1N2O1R2T1 Feb 20 '13 at 03:18
3

This is what I had in mind. I stripped out all the other stuff in your html5 function, just to see what it would return and give you the general idea of my thought process:

First, create a function that will figure out where Pandoc is installed. If multiple locations are matched (most likely "pandoc" and "~/.cabal/bin/pandoc" in your case, if it detects the path correctly) it will just select the first option.

wheresPandoc <- function() {
  myPaths <- c("pandoc", 
               "~/.cabal/bin/pandoc", 
               "~/Library/Haskell/bin", 
               "C:\\PROGRA~1\\Pandoc\\bin\\pandoc.exe")
  temp <- Sys.which(myPaths)
  temp <- names(temp[temp != ""])[1]
  if (is.na(temp)) stop("Pandoc not installed in one of the typical locations")
  else temp
}

Running that function by itself looks like this:

wheresPandoc()
# [1] "~/.cabal/bin/pandoc"

So, you can use the output of that in your html5 function to construct your system "action".

html5 <- function(in.file = NULL, out.file = NULL) {
  action <- paste0(wheresPandoc(), 
                   " -s -S -i -t dzslides --mathjax ", 
                   in.file, " -o ", out.file)
  action
}

html5(in.file = "this.txt", out.file = "that.html")
# [1] "~/.cabal/bin/pandoc -s -S -i -t dzslides --mathjax this.txt -o that.html"

This might be over-complicating things, but if you think your users are tech-savvy or the type of users who install programs in funny locations (and remember where they install them) you can consider changing wheresPandoc to something like the following. I've commented out the typical cabal location so you can see how it would work.

wheresPandoc <- function() {
  myPaths <- c("pandoc", 
  #            "~/.cabal/bin/pandoc", 
               "~/Library/Haskell/bin", 
               "C:\\PROGRA~1\\Pandoc\\bin\\pandoc.exe")
  temp <- Sys.which(myPaths)
  temp <- names(temp[temp != ""])[1]
  if (is.na(temp)) {
    ans <- readline("Pandoc not installed in one of the typical locations.\n 
                    Do you know where Pandoc is installed? (y/n) ")
    if (ans == "y") temp <- readline("Enter the (unquoted) path to Pandoc: ")
    else if (ans == "n") stop("Pandoc not installed or not found.")
  } 
  temp
}

On my system, running it looks something like this. To the first question, I answered "y", then it asked me to specify the unquoted path to Pandoc and uses that to construct your system call.

> html5(in.file = "this.txt", out.file = "that.html")
Pandoc not installed in one of the typical locations.

Do you know where Pandoc is installed? (y/n) y
Enter the (unquoted) path to Pandoc: ~/.cabal/install/pandoc
[1] "~/.cabal/install/pandoc -s -S -i -t dzslides --mathjax this.txt -o that.html"

Most general users I know would simply shut down if they see such a question, but most of the R users I know are a little bit more technically oriented, so they might not be too scared by it.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485