【问题标题】:R doesn't recognize Pandoc Linux MintR 无法识别 Pandoc Linux Mint
【发布时间】:2013-02-04 18:15:33
【问题描述】:

我问了一个相关问题:check if a program is installed

但在我在所有三个系统上为自己测试了解决方案之前,我不会回答。我可以让 pandoc 在 Windows 机器上的 R 中工作,但在 linux 上,我从 R 终端收到每个方法的错误/响应:

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 
"" 

您可能认为我没有安装 pandoc,但我相信我有。从干净的终端会话:

> 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

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.

如何让 Linux Mint 上的 R 识别 pandoc?(我是 Linux 新手)

【问题讨论】:

  • 在 Ubuntu 12.04 上为我工作。但是我在 /usr/bin/ 中有 pandoc。
  • 迪托。 system("which pandoc") 返回/usr/bin/pandoc。我认为您在那里使某些事情过于复杂。这不是 Windows。一旦你安装了一个二进制文件,“它就在那里”。
  • 我试图通过 cabal 更新使用更新版本的 pandoc,如 here 所示。然而它在那个页面上说它把它放在~/.cabal/bin。我想如果我将这些文件放到usr 目录中,它会按预期工作,但我什至不知道在哪里可以找到 usr 目录。但是使用 sudo apt-get install pandoc 它现在可以在 R 上运行。
  • @DirkEddelbuettel 就像 Tyler 提到的,如果你通过 cabal 安装最新版本,它不在 /usr/bin 中。 Tyler - 当 R 无法通过 Sys.which 找到 pandoc 时,您是在使用 RStudio,还是只是在使用终端?对我来说,它适用于终端,但不适用于 RStudio。似乎 RStudio 没有在 .bashrc 中读取,所以使用 RStudio 时 ~/.cabal/bin 不在您的路径中。
  • @TylerRinker,我不明白。我建议的解决方案检测到 Pandoc。它只是意味着当你编写一个 Pandoc 系统调用时,你不能只做类似system("pandoc -o etc...") 的事情,而必须做类似system("~/.cabal/bin/pandoc -o etc...") 的事情,换句话说,你需要修改系统调用以使用完整路径。

标签: r pandoc linux-mint


【解决方案1】:

这就是我的想法。我删除了你的 html5 函数中的所有其他内容,只是为了看看它会返回什么,并让你大致了解我的思考过程:

首先,创建一个函数来确定 Pandoc 的安装位置。如果匹配多个位置(在您的情况下很可能是“pandoc”和“~/.cabal/bin/pandoc”,如果它正确检测到路径)它将只选择第一个选项。

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
}

单独运行该函数如下所示:

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

因此,您可以在 html5 函数中使用它的输出来构造您的 system“动作”。

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"

这可能会使事情过于复杂,但如果您认为您的用户精通技术或在有趣的位置安装程序(并记住他们安装程序的位置)的用户类型,您可以考虑将 wheresPandoc 更改为类似下列。我已经注释掉了典型的阴谋集团位置,所以你可以看到它是如何工作的。

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
}

在我的系统上,运行它看起来像这样。对于第一个问题,我回答“y”,然后它要求我指定 Pandoc 的未引用路径并使用它来构建您的 system 调用。

> 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"

我认识的大多数普通用户如果看到这样的问题就会直接关闭,但我认识的大多数 R 用户都比较注重技术,所以他们可能不会被它吓到。

【讨论】:

    【解决方案2】:

    我也遇到了这个问题。我也通过 cabal 安装了 pandoc。如果你通过 apt-get 安装应该没有问题。如果我从终端启动 R,我没有问题,但尝试从 RStudio 中检测 pandoc 会遇到一些麻烦。原因是 RStudio 不会读取您的 bash 环境变量,因此如果您修改 .bashrc 中的路径,RStudio 将不会检测到这一点。一个解决方案是改为通过 .profile 修改路径。

    将此添加到 .profile 文件的底部并删除 .bashrc 文件中的路径修改,您应该能够从 R 中识别 pandoc。

    if [ -d "$HOME/.cabal/bin" ] ; then
        PATH="$PATH:$HOME/.cabal/bin"
    fi
    

    【讨论】:

    • 这个作为一般解决方案的问题是,当他们很可能已经使用 .bashrc ,因此我建议将Sys.which() 与一组指向 Pandoc 的典型路径一起使用。换句话说,也许它会为他自己解决 Tyler 的问题,但不会为任何可能使用他的功能的其他人解决。
    • @Ananda 问题不在于确定是否安装了 pandoc,而是在 RStudio 中识别出它已安装。如果我从终端启动 Rstudio 就认出来了。如果我单击 .Rproj 文件打开 RStudio,它不会识别出 pandoc 已安装。一些更有经验的 linux 用户了解我还不太了解的关于 bash 的内容。
    • @AnandaMahto 我同意您不能强迫用户从使用 .bashrc 更改为 .profile 但这确实解决了 RStudio 无法识别来自system 的 pandoc 的问题。我认为您的解决方案可能是最好的选择,因为它更强大,因此在 Tyler 希望将其部署给普通观众的情况下,这是一个很好的措施。但对于我们这些只希望 RStudio 识别我们修改后的路径的人来说……这很有效。
    • @Dason,另类发布。当我有空闲时间时,我肯定会尝试检查这个 RStudio 问题,如果发现任何问题,我会回复。
    猜你喜欢
    • 1970-01-01
    • 2015-01-03
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多