繁体   English   中英

从 R 中查找 pandoc 可执行文件

[英]Find pandoc executable from R

rmarkdown::find_pandoc帮助我们在 RStudio 中运行表单时找到pandoc可执行文件,无需指定任何环境变量:

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

但是,当从普通的 R 控制台运行相同的命令时,我得到:

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

阅读文档 pf ?rmarkdown::find_pandoc() ,解释了为什么我得到这些结果:

 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/'.

我现在想编写一个可以从命令行运行的脚本(特别是不能RStudio中运行),它需要找到pandoc 根据帮助,我可以设置我的PATH以确保也可以从命令行找到pandoc ,但是一旦我希望我的同事使用该脚本,我必须确保他/她的PATH已相应设置并且我想避免这种情况。

但是,我知道每个人都安装了Rstudio (虽然不在同一个位置),所以如果我知道RStudio的位置,我也可以推导出pandoc's位置。

是否有任何可靠的方法可以从控制台获取Rstudio的路径(即,当Rstudio运行时)?

长话短说:即使Rstudio没有运行,我如何才能找到它?

这可以工作,它基于 linux,但我想如果你在其他系统中,它可以适应:

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

Sys.setenv(PATH = paste0(Sys.getenv("PATH"),":",gsub("/pandoc","",pandoc_path )))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM