R doesn't recognize Pandoc Linux Mint

不羁的心 提交于 2019-11-27 04:49:09

问题


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)


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/14968103/r-doesnt-recognize-pandoc-linux-mint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!