Python conda - 如何安装“R-essentials”中不可用的 R 包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34705917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:24:23  来源:igfitidea点击:

conda - How to install R packages that are not available in "R-essentials"?

pythonranacondaconda

提问by Frank

I use an out-of-the-box Anaconda installation to work with Python. Now I have read that it is possible to also "include" the R world within this installation and to use the IR kernel within the Jupyter/Ipython notebook.

我使用开箱即用的 Anaconda 安装来处理 Python。现在我已经读到,还可以在此安装中“包含”R 世界并在Jupyter/Ipython notebook 中使用 IR 内核。

I found the command to install a number of famous R packages: conda install -c r r-essentials

我找到了安装一些著名的 R 包的命令:conda install -cr r-essentials

My beginner's question:

我的初学者问题:

How do I install R packages that are not included in the R-essentialpackage? For example R packages that are available on CRAN. "pip" works only for PyPI Python packages, doesn't it?

如何安装未包含在R-essential包中的R包?例如,CRAN 上可用的 R 包。“pip”仅适用于 PyPI Python 包,不是吗?

采纳答案by Frank

Now I have found the documentation:

现在我找到了文档:

This is the documentation that explains how to generate R packages that are only available in the CRAN repository: https://www.continuum.io/content/conda-data-science

这是解释如何生成仅在 CRAN 存储库中可用的 R 包的文档:https://www.continuum.io/content/conda-data-science

Go to the section "Building a conda R package".

转到“构建 conda R 包”部分。

(Hint: As long as the R package is available under anaconda.org use this resource. See here: https://www.continuum.io/blog/developer/jupyter-and-conda-r)

(提示:只要 R 包在 anaconda.org 下可用,请使用此资源。请参见此处:https: //www.continuum.io/blog/developer/jupyter-and-conda-r

alistaire's answer is another possibility to add R packages:

alistaire的答案是添加 R 包的另一种可能性:

If you install packages from inside of R via the regular install.packages(from CRAN mirrors), or devtools::install_github(from GitHub), they work fine. @alistaire

如果您通过常规install.packages(来自 CRAN 镜像)或devtools::install_github(来自 GitHub)从R 内部安装软件包,它们可以正常工作。@alistaire

How to do this:Open your (independent) R installation, then run the following command:

如何执行此操作:打开(独立的)R 安装,然后运行以下命令:

install.packages("png", "/home/user/anaconda3/lib/R/library")

to add new package to the correct R library used by Jupyter, otherwise the package will be installed in /home/user/R/i686-pc-linux-gnu-library/3.2/png/libs mentioned in .libPaths().

将新包添加到 Jupyter 使用的正确 R 库中,否则该包将安装在.libPaths() 中提到的 /home/user/R/i686-pc-linux-gnu-library/3.2/png/libs 中。

回答by Yaw

To install other R Packages on Jupyter beyond R-essentials

在 Jupyter 上安装 R-essentials 之外的其他 R 包

install.packages('readr', repos='http://cran.us.r-project.org')

One issue is that the specific repository is the US.R-Project(as below). I tried others and it did not work.

一个问题是特定的存储库是US.R-Project(如下)。我尝试了其他人,但没有奏效。

N.B. Replace readrwith any desired package name to install.

NB 替换readr为任何想要安装的包名。

回答by volodymyr

I had a problem when trying to install package from github using install_github("user/package")in conda with r-essentials. Errors were multiple and not descriptive.

尝试install_github("user/package")在带有 r-essentials 的conda 中使用从 github 安装软件包时遇到问题。错误是多方面的,而且没有描述性。

Was able to resolve a problem using these steps:

能够使用以下步骤解决问题:

  • download and unzip the package locally
  • activate correct conda environment (if required)
  • run R from command line
  • library(devtools)
  • install('/path/to/unzipped-package')
  • Command failed due to missing dependancies, but now I know what's missing!
  • run install.packages('missing-package', repos='http://cran.us.r-project.org')for all dependancies
  • run install('/path/to/unzipped-package')again. Now it should work!
  • 在本地下载并解压包
  • 激活正确的 conda 环境(如果需要)
  • 从命令行运行 R
  • library(devtools)
  • install('/path/to/unzipped-package')
  • 由于缺少依赖项,命令失败,但现在我知道缺少什么了!
  • 运行install.packages('missing-package', repos='http://cran.us.r-project.org')所有依赖项
  • install('/path/to/unzipped-package')再次运行。现在它应该可以工作了!

回答by Deninhos

I found an easy workaround. I suppose that you have an RStudio IDE for you R. It is weird to use RStudio for that, but I tried straight from R in my terminal and it didn't work. So, in RStudio console, just do the usual adding the path to your anaconda directory (in OSX,'/Users/yourusernamehere/anaconda/lib/R/library')

我找到了一个简单的解决方法。我想你有一个 RStudio IDE 供你使用 R。为此使用 RStudio 很奇怪,但我直接在我的终端中从 R 尝试,但它没有工作。因此,在 RStudio 控制台中,只需将路径添加到 anaconda 目录(在 OSX 中,'/Users/yourusernamehere/anaconda/lib/R/library')

So, for example,

所以,例如,

install.packages('package','/Users/yourusernamehere/anaconda/lib/R/library')

I feel ashamed to post such a non-fancy answer, but that is the only one that worked for me.

我为发布这样一个非花哨的答案感到羞耻,但这是唯一对我有用的答案。

回答by fab_rat

I attempted to install R packages ggplot2, tidyverse, etc using standard command line CRAN repositories and ran into multiple issues and problems.

我尝试使用标准命令行 CRAN 存储库安装 R 包 ggplot2、tidyverse 等,但遇到了多个问题。

Everything from unusable "init.tcl" files to a string of fifteen character errors. I was able to use the Conda install and WWW.Anaconda.org site for details. This installed R packages within the Anaconda directory architecture that were executable in R in terminal, RStudio and R environment in Jupyter Notebooks of the Anaconda Navigator.

从不可用的“init.tcl”文件到包含 15 个字符的错误字符串。我可以使用 Conda 安装和 WWW.Anaconda.org 站点了解详细信息。这在 Anaconda 目录架构中安装了 R 包,这些包可以在终端、RStudio 和 Anaconda Navigator 的 Jupyter Notebooks 中的 R 环境中执行。

for example: conda install -c r r-tidyverseVery easy and worked the first time.
[On Kubuntu 17.04 with Anaconda Navigator 1.6.2; R version 3.3.2 (2016-10-31)]

例如:conda install -c r r-tidyverse非常容易并且第一次工作。
[在 Kubuntu 17.04 上使用 Anaconda Navigator 1.6.2;R 版本 3.3.2 (2016-10-31)]

回答by rojour

Some one suggested a not so elegant way around it, but what's elegant as long as it works.

有人提出了一种不太优雅的方法来解决它,但只要它有效,什么是优雅的。

install.packages('package','/Users/yourusernamehere/anaconda/lib/R/library')

install.packages('package','/Users/yourusernamehere/anaconda/lib/R/library')

I spent almost an entire morning looking for an answer to this problem. I was able to install the libraries on RStudio but not on Jupyter Notebook (they have different versions of R) The above solution "almost" worked, it's just that I found the Jupyter Notebook was trying to install in a different directory, and it will report what directory. So I only changed that and it worked as a charm... thanks to Dninhos

我几乎花了一整个上午来寻找这个问题的答案。我能够在 RStudio 上安装库,但不能在 Jupyter Notebook 上安装(它们有不同版本的 R)上述解决方案“几乎”有效,只是我发现 Jupyter Notebook 试图安装在不同的目录中,它会报告什么目录。所以我只改变了它,它起到了一种魅力......感谢 Dninhos

回答by Pranav Pandya

Install rpy2with conda and add following line in your Jupyter notebook.

使用conda安装rpy2并在您的 Jupyter 笔记本中添加以下行。

%load_ext rpy2.ipython

In next chunks, you can simply run any r code by specifying %R

在接下来的块中,您可以通过指定 %R 来简单地运行任何 r 代码

Below is my favorite method to install and/or load r package

下面是我最喜欢的安装和/或加载 r 包的方法

%R if (!require("pacman")) install.packages("pacman")
%R pacman::p_load(dplyr, data.table, package3, package4)

p_load argument will install + loadthe package if it's not in your libelse it will simply load it.

p_load 参数将安装+加载包,如果它不在你的库中,否则它只会加载它。

回答by Ziggy Eunicien

Here's a conda-centric answer. It builds on Frank's answer and the continuum website: https://www.continuum.io/content/conda-data-sciencewith a bit more detail.

这是一个以 conda 为中心的答案。它建立在 Frank 的回答和连续体网站上:https: //www.continuum.io/content/conda-data-science有更多细节。

Some packages not available in r-essentials are still available on conda channels, in that case, it's simple:

一些在 r-essentials 中不可用的包在 conda 频道上仍然可用,在这种情况下,很简单:

conda config --add channels r
conda install r-readxl

If you need to build a package and install using conda:

如果您需要构建一个包并使用 conda 安装:

conda skeleton cran r-xgboost
conda build r-xgboost
conda install --use-local r-xgboost

that last line is absent in the continuum website because they assume it gets published to anaconda repository first. Without it, nothing will be put in the envs/ directory and the package won't be accessible to commandline R or Jupyter.

连续体网站中没有最后一行,因为他们假设它首先发布到 anaconda 存储库。没有它, envs/ 目录中将没有任何内容,并且命令行 R 或 Jupyter 将无法访问该包。

On a mac, I found it important to install the Clang compiler for package builds:

在 Mac 上,我发现为包构建安装 Clang 编译器很重要:

conda install clangxx_oxs-64

回答by A. Beal

Adding it here so other beginners already working with Jupyter notebooks with Python and interested in using it with R: additional packages available for Anaconda can be installed via terminal using the same command used to instal the essential packages.

在这里添加它,以便其他已经使用 Python 使用 Jupyter 笔记本并有兴趣将其与 R 一起使用的初学者:可以使用用于安装基本软件包的相同命令通过终端安装适用于 Anaconda 的其他软件包。

Install r-essentials

安装必需品

conda install -c r r-essentials

Install microbenchmark (infrastructure to accurately measure and compare the execution time of R expressions)

安装 microbenchmark(用于准确测量和比较 R 表达式执行时间的基础设施)

conda install -c r r-microbenchmark

回答by Alf Eaton

To install a CRAN package from the command line:

从命令行安装 CRAN 包:

R --slave -e "install.packages('missing-package', repos='http://cran.us.r-project.org')"