Introduction
R is a popular open source programming language This 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's highly and easily extensible by allowing users to author and submit their own packages. The R community are known to be very active and are noted for continuously adding user-generated statistical packages for S pecific 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 mate Rial, 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'll use the RStudio mirror.
In this guide, we'll learn how to set up R on a digitalocean Droplet running Ubuntu 14.04. If your Droplet is running a different operating system, and most of the instructions would still apply, but you may need to mo Dify some of the commands. Following this guide to completion should take about 10-15 minutes.
Prerequisites
For the tutorial, you'll need:
- An Ubuntu 14.04 Droplet with at least 1 GB of RAM. All the commands-tutorial should be run as a non-root user. If root access is required for the command, it'll be preceded by
sudo
. Initial Server Setup with Ubuntu 14.04 explains what 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 is downloaded from. The 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 add ing a line to the sources file. The exact line is need to add would 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 is running a different Ubuntu version, consult this document for the correct repository to add.
To authenticate packages downloaded using APT, we had to add a public key. The Ubuntu archives on CRAN is 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 the APT has been set up properly, we is ready for use with it to install R.
First, we need to update the list of available packages since we updated the sources list.
Now we can install R. We use the -y
flag to automatically answer Yes when asked if we is sure we want to download the package.
- sudo apt-get-y install R-base
At this point, you should has an installation of the latest R version on your Droplet. You can test this by running the R
command.
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 ComputingPlatform: 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 localeR 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 is now inside the R interactive shell and can run arbitrary R commands.
Quit R, and return to your Droplet with the q()
function:
Step 3-installing R Packages from CRAN
Now the R is installed on your Droplet, and any user on the Droplet can use R. When R is installed, it automatically installs a number of the default packages, but the order to do anything truly meaningful In the R you'll probably need to install extra packages. It is important to the 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 is hosted on CRAN, or to update existing ones, you use the install.packages()
function in R. If you wanted to the install package somepackage, you would open R and run the following R command.
# This is an example, do not run thisinstall.packages("somepackage")
However, any package installed by a specific the user in R is only being available to the user by default. For example, if user Sammy installs somepackage, then user Jessie would not be able to use somepackage until they i Nstall it as well.
It is possible-to-install a R package in a-a-makes it available to all users on the Droplet by installing it as RO Ot. As an example, let's install the package shiny
, which are a very popular package used to create Web applications from R cod E. One-to-install the package as root would is 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 repos
the parameter so and the package are 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 to rather than opening R install.packages()
and running the command, the package is shiny
made available To all users on the Droplet.
Let's verify that is shiny
installed correctly by trying to load it. Start an R session.
In R, try loading the package shiny
.
Running the previous command should result in no errors. Now quit R.
Step 4-installing
devtools
Package
While many R packages is hosted on CRAN and can be installed using the built-in install.packages()
function, there is many more packa Ges that is hosted on GitHub but is not on CRAN. To install R-packages from GitHub, we need to use devtools
the R package, so let's install it.
The devtools
R package requires three system packages to being installed on the Droplet, namely libcurl4-gnutls-dev
, libxml2-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 is 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, we have devtools
installed, we can install any R package, which is on GitHub using the install_github()
function. Just like with CRAN packages if installing GITHUB packages you need to run the command from the system shell to make th E package available to all users. Let's try installing shinyjs
the GitHub package, which adds functionality to the package shiny
. A GitHub package was defined by it author ( daattali
) and its name ( shinyjs
).
- sudo su-c "r-e \" Devtools::install_github (' daattali/shinyjs ') \ ""
Let's verify that is shinyjs
installed correctly by trying to load it. Start an R session.
In R, try loading the package shinyjs
.
Running the previous command could result in some messages, but no error messages. Now quit R.
Next Steps
You are now having a working R installation on your Droplet.
To learn more on R, visit the official R website, or try learning R hands-on and interactively with the package swirl
.
For more information on CRAN and what it offers, visit the official CRAN website.
For a better experience writing R code on your Droplet, the want to install the RStudio Server using this tutorial.
If you want to host any of the your Shiny code on your Droplet, you could 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 what to ensure that these packages is 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 Installation R environment