首页 > 解决方案 > 从 R 中查找 pandoc 可执行文件

问题描述

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

## 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即使它没有运行,我怎么能找到呢?

标签: rrstudiopandoc

解决方案


推荐阅读