如何正确使用Anaconda配置Python,R环境

这篇文章摘于我写在github上的一篇文章,希望大家多提意见,谢谢。

How to set up your own anaconda in a scientific way?

python2, python3, R in conda

Introduction

  • what is anaconda?

    • According to its own website, Anaconda is the leading open data science platform powered by Python. The open source version of Anaconda is a high performance distribution of Python and R and includes over 100 of the most popular Python, R and Scala packages for data science.
    • Or you can simply regard it as a manager of the packages of your programming languages.
  • why we want to use anaconda?

    • Convinient to manage packages
    • Jupyter notebook
  • object

    • Set up anaconda for a python and r user (linux)
    • I use py2 more often so I set it my default environment

Get started!

  1. Click this link and download corresponding version of anaconda.

  2. Type the following line in the terminal and the installation will be set up automatically

     ```bash Anaconda2-4.3.1-Linux-x86_64.sh``` . 
    

Do include the "bash" command even if you are not using the bash shell.

  1. Add the following line to the .bashrc or .zshrc(if you are a zsh user).

     ```export PATH="/home/zijun/anaconda2/bin:$PATH"```
    
  2. Create python3 environment in your conda.

     ```conda create -n py3 python=3.5 anaconda```
    
  3. Create R environment in your conda.

     ```conda create -n r -c r r-essentials```
    
  4. Install tck for R (enable install.packages() in R)

     ```source activate r
        conda install -c intel tcl=8.6.4```
    
  5. Note1: if you want to remove environment you install:

     ```conda remove --name py3 --all```
    
  6. Note2: switch different environment

     ```source activate r
        source deactivate r```
    

Some issues you may encounter during the process

Q: tcl not found

install.packages('highfrequency')
--- Please select a CRAN mirror for use in this session ---
Error: .onLoad failed in loadNamespace() for 'tcltk', details:
call: fun(libname, pkgname)
error: Can't find a usable init.tcl in the following directories:
/opt/anaconda1anaconda2anaconda3/lib/tcl8.5 ./lib/tcl8.5 ./lib/tcl8.5 ./library ./library ./tcl8.5.18/library ./tcl8.5.18/library

A: Solution:

  • Just do the step 5 above to solve this problem.

  • Or add the following lines to the .bashrc or .zshrc

     ``` export TCL_LIBRARY="/home/zijun/anaconda2/lib/tcl8.5"
         export TK_LIBRARY="/home/zijun/anaconda2/lib/tk8.5" ```
    
  • Note1: tck is a package which supports install.packages() function and it is installed in the global environment if you use sudo apt-get install tcl8.5-dev tk8.5-dev and conda has no access to it.

  • Note2: packages will be installed in /home/zijun/anaconda/envs/r/lib/R/library using install.packages()

Q: How to install packages in R

A: Since we set up the tcl, we can easily use install.packages() or remove.packages() to manage the R packages. However in this way, conda will lose control over r packages. Also you may find some suggestions using conda-build like this:

  • switch back to the root environment
  • type conda install conda-build in the terminal
  • Use conda skeleton cran and conda build to install packages (you may mind the dependency issue)

However this will not give you the control either. Moreover, there might be more issues about shitty sytax in R. So my suggestion is simply using install.packages() in R to install your packages.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容