Introduction

R is a popular open source programming language that specializes in statistical computing and graphics. It is widely used by statisticians for developing statistical software and performing data analysis. One of R's strengths is that it is highly and easily extensible by allowing users to author and submit their own packages. The R community is known to be very active and is noted for continuously adding user-generated statistical packages for specific areas of study, which makes R applicable to many fields of study.

The "Comprehensive R Archive Network" (CRAN) is a collection of sites (called mirrors) which carry identical material, consisting of many R packages and the R distributions themselves. You can download R and many R packages from any of the CRAN mirrors, but we will use the RStudio mirror.

In this guide, we will learn how to set up R on a DigitalOcean Droplet running Ubuntu 14.04. If your Droplet is running a different operating system, most of the instructions will still apply, but you may need to modify some of the commands. Following this guide to completion should take about 10-15 minutes.

 

Prerequisites

For this tutorial, you will need:

  • An Ubuntu 14.04 Droplet with at least 1 GB of RAM. All the commands in this tutorial should be run as a non-root user. If root access is required for the command, it will be preceded by sudoInitial Server Setup with Ubuntu 14.04 explains how to add users and give them sudo access.
 

Step 1 — Setting Up APT

To install R, we're going to use the APT (Advanced Packaging Tool) tool. It uses a special file that lists the sources of where packages should be downloaded from. That file is /etc/apt/sources.list. In order to get the most recent version of R, we need to add the correct repository to the list of sources by adding a line to the sources file. The exact line you need to add will vary depending on the exact Ubuntu version. For Ubuntu 14.04, run the following command to add the correct repository to /etc/apt/sources.list.

  • sudo sh -c 'echo "deb http://cran.rstudio.com/bin/linux/ubuntu trusty/" >> /etc/apt/sources.list'

If you are running a different Ubuntu version, consult this document for the correct repository to add.

To authenticate packages downloaded using APT, we have to add a public key. The Ubuntu archives on CRAN are signed with a key with ID E084DAB9. Add this key to your system.

  • gpg --keyserver keyserver.ubuntu.com --recv-key E084DAB9

Next we need to add the key to apt.

  • gpg -a --export E084DAB9 | sudo apt-key add -
 

Step 2 — Installing R

Now that APT has been set up properly, we are ready to use it to install R.

First, we need to update the list of available packages since we updated the sources list.

  • sudo apt-get update

Now we can install R. We use the -y flag to automatically answer Yes when asked if we are sure we want to download the package.

  • sudo apt-get -y install r-base

At this point, you should have an installation of the latest R version on your Droplet. You can test this by running the R command.

  • R

You should see output similar to the following.

R version 3.2.1 (2015-06-18) -- "World-Famous Astronaut"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R. >

You are now inside the R interactive shell and can run arbitrary R commands.

Quit R, and return to your Droplet with the q() function:

  • q(save = "no")
 

Step 3 — Installing R Packages from CRAN

Now that R is installed on your Droplet, any user on the Droplet can use R. When R is installed, it automatically installs a number of default packages, but in order to do anything truly meaningful in R you will probably need to install extra packages. It is important to have at least 1 GB of RAM available in order to install many packages.

As mentioned previously, CRAN hosts not only R itself, but many R packages as well. To install new R packages that are hosted on CRAN, or to update existing ones, you use the install.packages()function in R. If you wanted to install package somepackage, you would open R and run the following R command.

# This is an example, do not run this
install.packages("somepackage")

However, any package installed by a specific user in R will only be available to that user by default. For example, if user sammy installs somepackage, then user jessie will not be able to use somepackage until they install it as well.

It is possible to install an R package in a way that makes it available to all users on the Droplet by installing it as root. As an example, let's install the shiny package, which is a very popular package used to create web applications from R code. One way to install the package as root would be to log in as root, run R, and run the install.packages() command. However, it's recommended not to log in as root, so instead we can just run the R command as root. We'll also specify the repos parameter so that the package is downloaded from the RStudio CRAN repository, the same one we used when downloading R itself.

  • sudo su - -c "R -e \"install.packages('shiny', repos = 'http://cran.rstudio.com/')\""

By installing a package this way rather than opening R and running the install.packages() command, the shiny package is made available to all users on the Droplet.

Let's verify that shiny was installed correctly by trying to load it. Start an R session.

  • R

In R, try loading the shiny package.

  • library(shiny)

Running the previous command should result in no errors. Now quit R.

  • q(save = "no")
 

Step 4 — Installing devtools Package

While many R packages are hosted on CRAN and can be installed using the built-in install.packages() function, there are many more packages that are hosted on GitHub but are not on CRAN. To install R packages from GitHub, we need to use the devtools R package, so let's install it.

The devtools R package requires three system packages to be installed on the Droplet, namely libcurl4-gnutls-devlibxml2-dev, and libssl-devc. Install these three packages:

  • sudo apt-get -y install libcurl4-gnutls-dev libxml2-dev libssl-dev

Now the devtools R package can be installed. Remember that we want to install it using the same method as described above, rather than install it within an R session, because devtools should be available to all users.

  • sudo su - -c "R -e \"install.packages('devtools', repos='http://cran.rstudio.com/')\""

The above command to install devtools could take several minutes to complete.

 

Step 5 — Installing R Packages from GitHub

Now that we have devtools installed, we can install any R package that is on GitHub using the install_github() function. Just like with CRAN packages, when installing GitHub packages you need to run the command from the system shell to make the package available to all users. Let's try installing the shinyjs GitHub package, which adds functionality to the shiny package. A GitHub package is defined by its author (daattali) and its name (shinyjs).

  • sudo su - -c "R -e \"devtools::install_github('daattali/shinyjs')\""

Let's verify that shinyjs was installed correctly by trying to load it. Start an R session.

  • R

In R, try loading the shinyjs package.

  • library(shinyjs)

Running the previous command could result in some messages, but no error messages. Now quit R.

  • q(save = "no")
 

Next Steps

You now have a working R installation on your Droplet.

To learn more about R, visit the official R website, or try learning R hands-on and interactively with theswirl package.

For more information on CRAN and what it offers, visit the official CRAN website.

For a better experience writing R code on your Droplet, you may want to install an RStudio Server using this tutorial.

If you want to host any of your Shiny code on your Droplet, you may want to install a Shiny Server using this tutorial.

 

Conclusion

In this guide, we went through the steps required to set up R on an Ubuntu 14.04 Droplet. We also learned the difference between installing R packages from GitHub vs CRAN and how to ensure that these packages are made available for all users on the Droplet.

ref: https://www.digitalocean.com/community/tutorials/how-to-set-up-r-on-ubuntu-14-04

Ubuntu 14.04 安装R 环境的更多相关文章

  1. ubuntu 14.04 安装torch及编译环境zbstudio

    ubuntu 14.04 安装torch及编译环境zbstudio torch zbstudio 本来是安装官网给的步骤安装torch的,可是碰到一系列的问题,后来参考网上的安装方法安装成功了 官网安 ...

  2. Ubuntu 14.04 安装LNMP(nginx/1.12.1+php7.1.9+mysql5.7.19)环境

    这篇教程中,我们将讨论怎样在Ubuntu 14.04搭建LNMP环境 1 安装Nginx 首先我们要更新apt源 sudo add-apt-repository ppa:nginx/stable  s ...

  3. ubuntu 14.04 安装svn server (subversionedge )

    ubuntu 14.04 安装subversionedge 请仔细阅读安装包自带的readme文件! 1.先去官网,找安装包: http://subversion.apache.org/ http:/ ...

  4. 在Ubuntu 14.04安装和使用Docker

    Docker是一个开源软件,它可以把一个Linux应用和它所依赖的一切(比如配置文件)都封装到一个容器.然而,Docker与虚拟机不同,它使用了沙箱机制,Docker容器不运行操作系统,它共享主机上的 ...

  5. [转]在Ubuntu 14.04安装和使用Docker

    在Ubuntu 14.04安装和使用Docker 作者:chszs,版权所有,未经同意,不得转载.博主主页:http://blog.csdn.net/chszs Docker是一个开源软件,它可以把一 ...

  6. Ubuntu 14.04 安装VMware 12

    /*********************************************************************** * Ubuntu 14.04 安装VMware 12 ...

  7. Ubuntu 14.04 关于 TensorFlow 环境的配置

    Ubuntu 14.04 关于 TensorFlow 环境的配置   本教程截图于 TensorFlow 官方文档中文版  https://github.com/jikexueyuanwiki/ten ...

  8. Ubuntu 14.04安装Chromium浏览器并添加Flash插件Pepper Flas

    转自Ubuntu 14.04安装Chromium浏览器并添加Flash插件Pepper Flash Player Chromium谷歌的开源浏览器将不再支持Netscape浏览器插件API,Adobe ...

  9. ubuntu 14.04 安装搜狗拼音输入法

    原文:ubuntu 14.04 安装搜狗拼音输入法 ubuntu桌面系统下终于有了好用的拼音法-搜狗拼音输入法,欲在ubuntu 14.04下安装搜狗拼音输入法相当的简单. 先到搜狗拼音官网下载对应的 ...

随机推荐

  1. 不停mysql服务添加从库的两种方式

    现在生产环境MySQL数据库是一主一从,由于业务量访问不断增大,故再增加一台从库.前提是不能影响线上业务使用,也就是说不能重启MySQL服务,为了避免出现其他情况,选择在网站访问量低峰期时间段操作. ...

  2. 对Android的恶意吐槽(勿看,有毒)

    CSDN博客:http://blog.csdn.net/niu_gao 我觉得android系统中有一个特恶心人的大败笔.就是这个大败笔造成了android系统的卡卡卡不停. 这个大败笔就是对acti ...

  3. X64 Deep Dive

    zhuan http://www.codemachine.com/article_x64deepdive.html X64 Deep Dive This tutorial discusses some ...

  4. 自动实时监控Windows2003服务器终端登录

    朋友一台Windows服务器被黑了,而且还被人恶意删除了一些数据备份,帮其做了下安全加固,考虑到服务器都是通过Windows 终端服务器来管理的,就想办法对其登录做个监控,找了个命令行下发邮件的小工具 ...

  5. python地址解析经纬度,城市

    1.地址列表 1.txt 上海市普陀区梅川路299-301号 浙江省杭州市拱墅区丰登路305-311号1层 江苏省南京市鼓楼区碧树园86号101室 浙江省宁波市江北区范江岸路38弄6号-10号1层商铺 ...

  6. Oracle PLSQL Demo - 07.LOOP循环,以EXIT WHEN退出[EXIT in LOOP]

    declare v_sal ; begin loop v_sal :; dbms_output.put_line(v_sal); ; end loop; end;

  7. 【转】python多版本并存,python3安装pip

    python3 与 pip3 安装与使用   1. yum -y install openssl* (pip依赖ssl环境) 2.编译安装python3 下载地址:https://www.python ...

  8. 常见电源品牌大揭密(转贴自pceva,作者royalk)

    常见电源品牌大揭密(转贴自pceva,作者royalk) 介绍电源品牌代工厂之前,必须介绍一下电源分类: 标准电源 标准电源就是电脑城装机用得最多的电源,性能正常,外观一般,原生接线,也没有什么风扇停 ...

  9. 人工智能时代,应立即学习python

    人工智能时代,应立即学习python 应用:web开发,自动化运维开发,自动化测试,数据分析,机器学习 1.python 快速易学习2.python 基于web开发(zhihu:tornad web框 ...

  10. SVN导入maven项目

    在项目中,曾今遇到过这种问题,用eclipse将项目从svn下载下来,maven去自动下载jar包怎么都报错,本来时间就很紧张, 还特么遇到这种坑爹的问题.不过,整了我一天,最后终于在同事的帮助下,搞 ...