简体   繁体   English

Python子进程调用不会安装R包

[英]Python subprocess call won't install R package

I have a Python subprocess to call R:我有一个 Python 子进程来调用 R:

cmd = ['Rscript', 'Rcode.R', 'file_to_process.txt']
out = subprocess.run(cmd, universal_newlines = True, stdout = subprocess.PIPE)
lines = out.stdout.splitlines() #split stdout

My R code first checks if the 'ape' package is installed before proceeding:在继续之前,我的 R 代码首先检查是否安装了“ape”包:

if (!require("ape")) install.packages("ape")
library(ape)
do_R_stuff.......
return_output_to_Python

Previously, the whole process from Python to R worked perfectly - R was called and processed output was returned to Python - until I added the first line ( if (!require("ape")) install.packages("ape") ).以前,从 Python 到 R 的整个过程运行良好——调用 R 并将处理后的输出返回给 Python——直到我添加了第一行( if (!require("ape")) install.packages("ape") )。 Now Python reports: " there is no package called 'ape' " (ie when I uninstall ape in R).现在 Python 报告:“没有名为 'ape' 的包”(即当我在 R 中卸载ape时)。 I have tried wait instructions in both the R and Python scripts but I can't get it working.我在 R 和 Python 脚本中都尝试过等待指令,但我无法让它工作。 When checking, the R code works in isolation.检查时,R 代码是独立工作的。

The full error output from Python is: Python 的完整错误输出为:

Traceback (most recent call last):

  File ~\Documents\GitHub\wolPredictor\wolPredictor_MANUAL_parallel.py:347 in <module>
    if __name__ == '__main__': main()

  File ~\Documents\GitHub\wolPredictor\wolPredictor_MANUAL_parallel.py:127 in main
    cophen, purge, pge_incr, z, _ = R_cophen('{}/{}'.format(dat_dir, tree), path2script) #get Dist Mat from phylogeny in R

  File ~\Documents\GitHub\wolPredictor\wolPredictor_MANUAL_parallel.py:214 in R_cophen
    purge = int(np.max(cophen) * 100) + 1 #max tree pw distance

  File <__array_function__ internals>:5 in amax

  File ~\anaconda3\lib\site-packages\numpy\core\fromnumeric.py:2754 in amax
    return _wrapreduction(a, np.maximum, 'max', axis, None, out,

  File ~\anaconda3\lib\site-packages\numpy\core\fromnumeric.py:86 in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)

ValueError: zero-size array to reduction operation maximum which has no identity


Loading required package: ape
Installing package into 'C:/Users/Windows/Documents/R/win-library/4.1'
(as 'lib' is unspecified)
Error in contrib.url(repos, "source") : 
  trying to use CRAN without setting a mirror
Calls: install.packages -> contrib.url
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'ape'
Execution halted

Point 1: The path to R libraries when using R in a standalone mode may not be the same when using Rscript.第 1 点:在独立模式下使用 R 时,R 库的路径可能与使用 Rscript 时不同。

Point 2: The error says there was difficulty in finding the CRAN repository, so perhaps the options that set the repos were not set for the Rscript environment.第 2 点:错误表明很难找到 CRAN 存储库,因此可能没有为 Rscript 环境设置设置存储库的选项。 They can be set in the call to install packages or with a Sys.setenv() call.它们可以在安装包的调用或 Sys.setenv() 调用中设置。

Ther OP wrote: "Thanks @IRTFM I had to set a new sub process in a new .R file specifically for the install, but the CRAN mirror was key, I never realised it would be an issue as it's not a requirement on my local machine (not sure why it becomes an issue thru subprocess)." OP 写道:“感谢@IRTFM,我必须在新的 .R 文件中设置一个新的子进程,专门用于安装,但 CRAN 镜像是关键,我从未意识到这将是一个问题,因为这不是我本地的要求机器(不知道为什么它通过子进程成为问题)。”

The places to find more information are the ?Startup help page and the ?Rscript page.查找更多信息的地方是?Startup帮助页面和?Rscript页面。 Rscript has many fewer defaults. Rscript 的默认值要少得多。 Even the usual set of recommended packages may not get loaded by default.默认情况下,即使是通常的推荐包集也可能不会加载。 The Rscript help page includes these flags which could be used for debugging and setting a proper path to the libraries desired.: Rscript 帮助页面包括这些标志,可用于调试和设置所需库的正确路径。:

--verbose gives details of what Rscript is doing. --verbose 给出了 Rscript 正在做什么的细节。

--default-packages=list where list is a comma-separated list of package names or NULL. --default-packages=list 其中 list 是以逗号分隔的包名称列表或 NULL。 Sets the environment variable R_DEFAULT_PACKAGES which determines the packages loaded on startup.设置环境变量 R_DEFAULT_PACKAGES 确定启动时加载的包。

Here is a previous similar SO question with an answer that includes some options for construction of a proper working environment: Rscript: There is no package called ...?这是先前类似的 SO 问题,其答案包括构建适当工作环境的一些选项: Rscript:没有名为...的包?

There are a few R packages on CRAN that aid in overcoming some of the differences between programming for standalone R and Rsript. CRAN 上有一些 R 包可以帮助克服独立 R 和 Rsript 编程之间的一些差异。

getopt: https://cran.r-project.org/web/packages/getopt/index.html获取选择: https ://cran.r-project.org/web/packages/getopt/index.html

optparse: https://cran.r-project.org/web/packages/optparse/index.html (styled after a similar Python package.) optparse: https ://cran.r-project.org/web/packages/optparse/index.html(以类似的 Python 包为样式。)

argparse: https://cran.r-project.org/web/packages/argparse/index.html参数解析: https ://cran.r-project.org/web/packages/argparse/index.html

I solved the issue (thanks to @IRTFM) by placing the if-then-install.packages code in a separate Rscript (including the CRAN mirror):我通过将if-then-install.packages代码放在单独的 Rscript(包括 CRAN 镜像)中解决了这个问题(感谢@IRTFM):

if (!require("ape")) install.packages("ape", repos='http://cran.us.r-project.org')

which I then called using a separate Python subprocess in my Python routine然后我在我的 Python 例程中使用单独的 Python 子进程调用它

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

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