Find pandoc executable from R

Viewed 194

rmarkdown::find_pandoc helps us to find the pandoc executable w/o the need of specifiying any environmental variable when running form within RStudio:

## in Rstudio
!is.null(rmarkdown::find_pandoc()$dir)
# [1] TRUE

However, when running the same command from a plain R console I get:

### R console
!is.null(rmarkdown::find_pandoc()$dir)
# [1] FALSE

Reading the documentation pf ?rmarkdown::find_pandoc(), explains why I am getting these results:

dir: A character vector of potential directory paths under which
          ‘pandoc’ may be found. If not provided, this function
          searches for ‘pandoc’ from the environment variable
          RSTUDIO_PANDOC (the RStudio IDE will set this variable to the
          directory of Pandoc bundled with the IDE), the environment
          variable PATH, and the directory ‘~/opt/pandoc/’.

I want now to write a script which can be run from the command line (specifically not from within RStudio), which needs pandoc to be found. As per the help I could set my PATH to assure that pandoc is also found from the command line, but as soon as I want a colleague of mine to use the script, I have to make sure that his/her PATH is set accordingly and I want to avoid that.

However, I do know that everybody has Rstudio installed (not at the same location though), so if I knew the location of RStudio I could derive pandoc's location too.

Is there any reliable way to get the path of Rstudio from the console (i.e. also when Rstudio is not running)?

To make a long story short: how can I find Rstudio even if it is not running`?

1 Answers

This could work, it is based on linux, but I guess if you are in other system, it could be adapted:

pandoc_path <- system2("find", args="/usr/lib/rstudio -name pandoc -type f", stdout = T)

path_sep <- ":"

Sys.setenv(PATH = paste0(Sys.getenv("PATH"),path_sep, sub(".pandoc$","",pandoc_path )))
Related