简体   繁体   English

在 Python 中调用 R 包的问题

[英]Problem with calling an R package in Python

For context: My goal is to create a graphic interface so that the user can run a program that I have been developing in R. The interface is done using the Tkinter module from python (version 3.3).对于上下文:我的目标是创建一个图形界面,以便用户可以运行我一直在 R 中开发的程序。该界面是使用 python(3.3 版)中的 Tkinter 模块完成的。 Right now I am trying to make it work in Windows.现在我正试图让它在 Windows 中工作。

I believe I have successfully called the R interpreter using python to run my R file (run.R).我相信我已经成功地使用 python 调用了 R 解释器来运行我的 R 文件(run.R)。 In this file I call the R package 'seqinr'.在这个文件中,我将 R 包称为“seqinr”。 However when I call the R interpreter from python I obtain this:但是,当我从 python 调用 R 解释器时,我得到了这个:

I run this:我运行这个:

os.system('C:/"Program Files"/R/R-3.6.1/bin/Rscript run.R')

and I obtain this:我得到了这个:

Error in library(seqinr) : there is no package called 'seqinr'
Calls: source -> withVisible -> eval -> eval -> library
Execution halted

However, when I run the 'C:/"Program Files"/R/R-3.6.1/bin/Rscript run.R' command in the Command Prompt, it works perfectly and I have no problems.但是,当我在命令提示符中运行 'C:/"Program Files"/R/R-3.6.1/bin/Rscript run.R' 命令时,它运行良好,我没有任何问题。 I also checked where my package is installed and it's in the default directory that R uses to install all the packages.我还检查了我的包的安装位置,它位于 R 用于安装所有包的默认目录中。 I also do not have any other R versions installed in my machine.我的机器上也没有安装任何其他 R 版本。

I have no clue what is happening now, so I would appreciate any help.我不知道现在发生了什么,所以我很感激任何帮助。

Thank you!谢谢!

Although probably not satisfactory, I'd recommend changing your "run.R" file to include the following lines:虽然可能不令人满意,但我建议更改您的“run.R”文件以包含以下几行:

if (!("seqinr" %in% installed.packages())) {
    install.packages('seqinr', repos = "https://cloud.r-project.org")
}
library(seqinr)

This will install the package if it isn't available to wherever R is looking for packages when being called from Python.如果在从 Python 调用时 R 正在寻找包的任何地方都无法使用该包,这将安装该包。 Additionally, you could include print(rownames(installed.packages())) in your "run.R" file to see what packages are currently available to your R installation at run time when calling from Python.此外,您可以在“run.R”文件中包含print(rownames(installed.packages())) ,以查看从 Python 调用时在运行时当前哪些包可用于 R 安装。 Inclusion of the line print(.libPaths()) will tell you where R is looking for installed packages at run time as well.包含行print(.libPaths())也会告诉您 R 在运行时在哪里寻找已安装的包。

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

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