R doesn't recognize Pandoc Linux Mint

…衆ロ難τιáo~ 提交于 2019-11-28 01:40:44

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

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.

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