Install & Use R & RStudio

Install & Use R & RStudio

2017-05-08. Category & Tags: R, Install, Ubuntu, Linux, Rstudio

R IN WINDOWS #

Download here and install.

R IN UBUNTU 18.04 #

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9 && \
sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/' && \
sudo apt update && \
sudo apt install -y r-base r-base-dev libcurl4-openssl-dev libssl-dev build-essential && \
sudo -i R # install packages as root, so all users can use.

Commonly used packages:

install.packages(c('devtools', 'digest', 'repr', 'IRdisplay', 'crayon', 'pbdZMQ', 'ggplot2', 'IRkernel', 'ggpubr'))

DigitalOcean

Optional: sudo chmod 777 /usr/local/lib/R/site-library so anyone can install packages.

R IN UBUNTU 16.04 #

Option 1:

sudo apt-get install -y r-base r-base-dev libcurl4-openssl-dev libssl-dev build-essential

Option 2 (to get a newer R version):
with PPA. ref digitalocean

sudo apt install apt-transport-https && \
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9 && \
sudo add-apt-repository 'deb [arch=amd64,i386] https://cran.rstudio.com/bin/linux/ubuntu xenial/' && \
sudo apt-get update && \
sudo apt-get install -y r-base r-base-dev libcurl4-openssl-dev libssl-dev build-essential && \
sudo -i R # install packages as root, so all users can use.

RSTUDIO IN WINDOWS #

Install R first, then install R-Studio.

ref: r-bloggers

INSTALL PACKAGES 安裝各种包 #

Use type = "win.binary" or type="binary" to avoid compilation and dependency error in windows.
E.g., install the “bibliometrix” r package. install.packages( c("bibliometrix"), type="binary")
If “binary” not specified, select “no” when promoted:
image

install a specific version / an old version #

ref

win.binary: #

# get the list of binary packages
wget https://cran.r-project.org/bin/windows/contrib/4.0/
# find the zip fileanme
grep -i pkg_name index.html
# download
wget https://cran.r-project.org/bin/windows/contrib/4.0/pkg_name_1.2.3.zip
# install
/path/to/R.exe CMD INSTALL pkg_name_1.2.3.zip # or in R: install.packages(packageurl_or_/path/to.zip, repos=NULL, type="win.binary")

source: #

Similar to above binary.

wget https://cran.r-project.org/src/contrib/
grep ...
wget ...tar.gz
/path/to/R.exe CMD INSTALL pkg_name_1.2.3.zip # or in R: install.packages(packageurl_or_.tar.gz, repos=NULL, type="source")

source repo #

require(devtools)
install_version("ggplot2", version = "0.9.1", repos = "http://cran.us.r-project.org")

DEFAULT MIRROR #

To choose a mirror/package server:
Option 1: temporaily

chooseCRANmirror(graphics=FALSE, ind=1) # 1 = cloud

Option 2:

echo 'options(repos=structure(c(CRAN="https://cloud.r-project.org")))' >> ~/.Rprofile

Option 3 [nix]:

vim /etc/R/Rprofile.site

uncomment and modify “local()”:

local({r <- getOption("repos")
      r["CRAN"] <- "https://cloud.r-project.org"
      options(repos=r)})

ref

CHECK #

install.packages(c('ggplot2'))
sessionInfo(package = NULL)
options(repr.plot.width=4, repr.plot.height=3)
t = c(1, 3, 6, 4, 9)
plot(t, col="blue")

FAQ #

Problem: package is build for <another_version>. Reason: the package (binary) is built by a newer R version and cannot run on an old R version. Solution: install a new R version, or downgrade the packege.