Ubuntu 14.04 安装R 环境

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

时间: 2024-09-29 23:30:54

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

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  sudo apt-get update 安装Nginx sudo apt-get install nginx Nginx安装完默认以经启动 启动Nginx:service nginx start 关闭Nginx:service nginx stop 重启Nginx:service nginx rest

Ubuntu 14.04 安装 jdk8u20 并配置环境变量 安装Android Studio

   Ubuntu 14.04 安装jdk并环境变量 jdk安装的前提:你的电脑里面没有安装openjdk,如果安装了,请先卸载 1.官网下载jdk:jdk-8u20-linux-x64.tar.gz 2.双击 jdk-8u20-linux-x64.tar.gz  提取文件到/home/ljk/JavaDevelop 目录下(你想要安装的那个目录,注意,在ubuuntu下,这个文件你一解压,就相当于已经安装了),提取完成后多出一个jdk1.8.0_20文件夹 3.CTRL+ALT+T打开终端,然

ubuntu 14.04 安装 vmware 10 X64 后无法启动解决方法

ubuntu 14.04 安装成功后,平时的工作什么的都够用了, 最近需要做一个测试,测试环境还必须是windows的.所以就准备整个虚拟机来,kvm 跟 vmware 考虑再三,选择了 vmware .vmware 10的安装方法,就不做说明了.度娘或谷哥还是有蛮多方法.我这里所写的是,vmware 安装后,启动时报错. 报错图片如下: 解决方法: 当然方法有很多种,我也试过几种方法,有使用patch 的,但是我用这种方法的时候,出现错误了,继续找方法的时候,有网友有告诉过这样一种方法,就是手

ubuntu 14.04安装压缩包版的mysql

既有环境: 1)下载MySQL Community Server 5.6.17 压缩版(mysql-5.6.17-linux-glibc2.5-x86_64.tar.gz) 2)安装目录: /opt/library/mysql-5.6.17-linux-glibc2.5-x86_64 3)软链接为/opt/library/mysql,指向同目录下的mysql-5.6.17-linux-glibc2.5-x86_64 1. 创建mysql专有的用户和组 ? 1 2 $ sudo groupadd

Ubuntu 14.04 安装 JDK 8,ubuntu14.04

Ubuntu 14.04 安装 JDK 8,ubuntu14.04 第一步,下载Linux版JDK 可以通过访问Oracle官网下载,或者直接通过命令行下载. [email protected]:~$ wget -c http://download.oracle.com/otn-pub/java/jdk/8u11-b12/jdk-8u11-linux-i586.tar.gz 第二步,解压安装 [email protected]:~$ mkdir -p /usr/lib/jvm [email pr

ubuntu 14.04安装pxe  

环境,是ubuntu14.04系统 参考文件https://www.maketecheasier.com/configure-pxe-server-ubuntu/ 步骤 1 环境准备 2 安装配置dhcp  tftp nfs 3 PXE安装和配置 4 添加pxe镜像并且导出到nfs服务器上 5 安装时候碰到的问题总结   1 环境准备   1安装ubuntu系统  2 配置固定ip地址 #interfaces(5) file used by ifup(8) and ifdown(8) autol

在Ubuntu 14.04安装和使用Docker

在Ubuntu 14.04安装和使用Docker 作者:chszs,版权全部,未经允许.不得转载.博主主页:http://blog.csdn.net/chszs Docker是一个开源软件,它能够把一个Linux应用和它所依赖的一切(比方配置文件)都封装到一个容器.然而.Docker与虚拟机不同,它使用了沙箱机制.Docker容器不执行操作系统,它共享主机上的操作系统. 下面我将在Ubuntu 14.04安装和使用Docker. Docker使得在同一个server上能够执行很多其他的应用程序-

ubuntu 14.04 安装笔记

ubuntu 14.04 安装笔记1.UltraISO将镜像写入U盘,从U盘启动安装.安装只用了不到5分钟2.添加软件源    根据网络设置主要的软件源,我这里用163的    apt-add-repository ppa:geany-dev/ppa    # geany,轻量级IDE    apt-add-repository ppa:rabbitvcs/ppa    # rabbitvcs,右键SVN菜单    apt-add-repository ppa:relan/exfat    #

[操作系统][Ubuntu 14.04] 安装Flash 安装QQ2013

[操作系统][Ubuntu 14.04] 安装Flash 安装QQ2013_郑少群个人网 一.安装Flash 打开Firefox浏览器弹出的Flash安装提醒早都烦死了,那么Ubuntu14.04怎么安装Flash呢? 1.32位系统命令行安装: 第一步 更新库: ? sudo apt-get update ? ? 第二步 安装Flash用下面的代码: sudo apt-get install flashplugin-installer ? 这样就将Flash和依赖装好了. 2.32位系统可视化