Installing R and RStudio Server in Ubuntu Linux

R is a language and environment for statistical computing and graphics, providing a wide variety of statistical and graphical techniques. The R environment is open source software under GPL. R has rich software packages and is widely used for statistical analysis.

RStudio Server is an R integrated development environment (IDE) that provides many useful features helping R developers to develop and debug R programs. RStudio Server provides a Web Server and users can connect to the Server using a web browser. This is especially useful to install an RStudio Server in a powerful computer in the Cloud like AWS and access it from any where using any device with a browser connected to the Internet. Note that RStudio also has a desktop version and the installation tutorial is covered in How To Install R and RStudio Desktop in Ubuntu Linux 20.04. RStudio Server is also open source software under AGPL v3. There are also professional versions with supporting services.

In this post, we will introduce how to install the R environment and the RStudio Server on Ubuntu Linux 20.04. For other Ubuntu version, the steps are similar. But make sure to download the RStudio Server version for that version of Ubuntu Linux.

Install R environment in Ubuntu Linux

The R environment software is already included in Ubuntu’s official software package repository. The package for R in Ubuntu is r-base. We can install the packages for the R environment using apt as follows.

$ sudo apt update
$ sudo apt install r-base

An example output is as follows for installing r-base.

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  bzip2-doc libblas-dev libblas3 libbz2-dev libjpeg-dev libjpeg-turbo8-dev libjpeg8-dev liblapack-dev liblapack3 liblzma-dev libncurses-dev
  libncurses5-dev libpcre16-3 libpcre2-dev libpcre2-posix2 libpcre3-dev libpcre32-3 libpcrecpp0v5 libpng-dev libpng-tools libreadline-dev r-base-core
  r-base-dev r-base-html r-cran-boot r-cran-class r-cran-cluster r-cran-codetools r-cran-foreign r-cran-kernsmooth r-cran-lattice r-cran-mass
  r-cran-matrix r-cran-mgcv r-cran-nlme r-cran-nnet r-cran-rpart r-cran-spatial r-cran-survival r-doc-html r-recommended
Suggested packages:
  liblapack-doc liblzma-doc ncurses-doc readline-doc elpa-ess r-doc-info | r-doc-pdf r-mathlib texlive-base texlive-latex-base texlive-plain-generic
  texlive-fonts-recommended texlive-fonts-extra texlive-extra-utils texlive-latex-recommended texlive-latex-extra texinfo
The following NEW packages will be installed:
  bzip2-doc libblas-dev libblas3 libbz2-dev libjpeg-dev libjpeg-turbo8-dev libjpeg8-dev liblapack-dev liblapack3 liblzma-dev libncurses-dev
  libncurses5-dev libpcre16-3 libpcre2-dev libpcre2-posix2 libpcre3-dev libpcre32-3 libpcrecpp0v5 libpng-dev libpng-tools libreadline-dev r-base
  r-base-core r-base-dev r-base-html r-cran-boot r-cran-class r-cran-cluster r-cran-codetools r-cran-foreign r-cran-kernsmooth r-cran-lattice r-cran-mass
  r-cran-matrix r-cran-mgcv r-cran-nlme r-cran-nnet r-cran-rpart r-cran-spatial r-cran-survival r-doc-html r-recommended
0 upgraded, 42 newly installed, 0 to remove and 1 not upgraded.
Need to get 55.5 MB of archives.
After this operation, 117 MB of additional disk space will be used.
Do you want to continue? [Y/n] 
Get:1 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 bzip2-doc all 1.0.8-2 [501 kB]
Get:2 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 libblas3 amd64 3.9.0-1build1 [142 kB]
Get:3 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 libblas-dev amd64 3.9.0-1build1 [147 kB]
Get:4 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 libbz2-dev amd64 1.0.8-2 [30.2 kB]
Get:5 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 libjpeg8-dev amd64 8c-2ubuntu8 [1,552 B]
Get:6 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 libjpeg-dev amd64 8c-2ubuntu8 [1,546 B]
Get:7 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 liblapack3 amd64 3.9.0-1build1 [2,154 kB]
Get:8 http://security.ubuntu.com/ubuntu focal-security/main amd64 libjpeg-turbo8-dev amd64 2.0.3-0ubuntu1.20.04.1 [238 kB]
Get:9 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 liblapack-dev amd64 3.9.0-1build1 [4,242 kB]
Get:10 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 libncurses-dev amd64 6.2-0ubuntu2 [339 kB]
Get:11 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 libncurses5-dev amd64 6.2-0ubuntu2 [976 B]
Get:12 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 libpcre16-3 amd64 2:8.39-12build1 [150 kB]
Get:13 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 libpcre2-posix2 amd64 10.34-7 [5,992 B]
Get:14 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 libpcre2-dev amd64 10.34-7 [670 kB]
Get:15 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 libpcre32-3 amd64 2:8.39-12build1 [140 kB]
Get:16 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 libpcrecpp0v5 amd64 2:8.39-12build1 [15.5 kB]
Get:17 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 libpcre3-dev amd64 2:8.39-12build1 [540 kB]
Get:18 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 libpng-dev amd64 1.6.37-2 [175 kB]
Get:19 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 libpng-tools amd64 1.6.37-2 [26.1 kB]
Get:20 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 libreadline-dev amd64 8.0-4 [141 kB]
Get:21 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-base-core amd64 3.6.3-2 [24.5 MB]
Get:22 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-cran-boot all 1.3-24-2 [615 kB]
Get:23 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-cran-cluster amd64 2.1.0-2 [525 kB]
Get:24 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-cran-foreign amd64 0.8.75-1 [231 kB]
Get:25 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-cran-mass amd64 7.3-51.5-2 [1,114 kB]
Get:26 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-cran-kernsmooth amd64 2.23-16-1+1 [90.0 kB]
Get:27 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-cran-lattice amd64 0.20-40-1 [1,125 kB]
Get:28 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-cran-nlme amd64 3.1.144-1 [2,220 kB]
Get:29 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-cran-matrix amd64 1.2-18-1 [3,448 kB]
Get:30 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-cran-mgcv amd64 1.8-31-1 [2,725 kB]
Get:31 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-cran-survival amd64 3.1-8-1 [7,388 kB]
Get:32 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-cran-rpart amd64 4.1-15-2 [679 kB]
Get:33 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-cran-class amd64 7.3-15-1 [86.2 kB]
Get:34 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-cran-nnet amd64 7.3-13-1 [111 kB]
Get:35 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-cran-spatial amd64 7.3-11-2build2 [127 kB]
Get:36 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-cran-codetools all 0.2-16-1 [89.3 kB]
Get:37 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-recommended all 3.6.3-2 [2,816 B]
Get:38 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-base all 3.6.3-2 [9,308 B]
Get:39 http://hk.archive.ubuntu.com/ubuntu focal/main amd64 liblzma-dev amd64 5.2.4-1 [145 kB]
Get:40 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-base-dev all 3.6.3-2 [4,524 B]
Get:41 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-doc-html all 3.6.3-2 [537 kB]
Get:42 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 r-base-html all 3.6.3-2 [55.8 kB]
Fetched 55.5 MB in 6s (9,519 kB/s)      
Extracting templates from packages: 100%
Selecting previously unselected package bzip2-doc.
(Reading database ... 262574 files and directories currently installed.)
Preparing to unpack .../00-bzip2-doc_1.0.8-2_all.deb ...
Unpacking bzip2-doc (1.0.8-2) ...
Selecting previously unselected package libblas3:amd64.
Preparing to unpack .../01-libblas3_3.9.0-1build1_amd64.deb ...
Unpacking libblas3:amd64 (3.9.0-1build1) ...
Selecting previously unselected package libblas-dev:amd64.
Preparing to unpack .../02-libblas-dev_3.9.0-1build1_amd64.deb ...
Unpacking libblas-dev:amd64 (3.9.0-1build1) ...
Selecting previously unselected package libbz2-dev:amd64.
Preparing to unpack .../03-libbz2-dev_1.0.8-2_amd64.deb ...
Unpacking libbz2-dev:amd64 (1.0.8-2) ...
Selecting previously unselected package libjpeg-turbo8-dev:amd64.
Preparing to unpack .../04-libjpeg-turbo8-dev_2.0.3-0ubuntu1.20.04.1_amd64.deb ...
Unpacking libjpeg-turbo8-dev:amd64 (2.0.3-0ubuntu1.20.04.1) ...
Selecting previously unselected package libjpeg8-dev:amd64.
Preparing to unpack .../05-libjpeg8-dev_8c-2ubuntu8_amd64.deb ...
Unpacking libjpeg8-dev:amd64 (8c-2ubuntu8) ...
Selecting previously unselected package libjpeg-dev:amd64.
Preparing to unpack .../06-libjpeg-dev_8c-2ubuntu8_amd64.deb ...
Unpacking libjpeg-dev:amd64 (8c-2ubuntu8) ...
Selecting previously unselected package liblapack3:amd64.
Preparing to unpack .../07-liblapack3_3.9.0-1build1_amd64.deb ...
Unpacking liblapack3:amd64 (3.9.0-1build1) ...
Selecting previously unselected package liblapack-dev:amd64.
Preparing to unpack .../08-liblapack-dev_3.9.0-1build1_amd64.deb ...
Unpacking liblapack-dev:amd64 (3.9.0-1build1) ...
Selecting previously unselected package libncurses-dev:amd64.
Preparing to unpack .../09-libncurses-dev_6.2-0ubuntu2_amd64.deb ...
Unpacking libncurses-dev:amd64 (6.2-0ubuntu2) ...
Selecting previously unselected package libncurses5-dev:amd64.
Preparing to unpack .../10-libncurses5-dev_6.2-0ubuntu2_amd64.deb ...
Unpacking libncurses5-dev:amd64 (6.2-0ubuntu2) ...
Selecting previously unselected package libpcre16-3:amd64.
Preparing to unpack .../11-libpcre16-3_2%3a8.39-12build1_amd64.deb ...
Unpacking libpcre16-3:amd64 (2:8.39-12build1) ...
Selecting previously unselected package libpcre2-posix2:amd64.
Preparing to unpack .../12-libpcre2-posix2_10.34-7_amd64.deb ...
Unpacking libpcre2-posix2:amd64 (10.34-7) ...
Selecting previously unselected package libpcre2-dev:amd64.
Preparing to unpack .../13-libpcre2-dev_10.34-7_amd64.deb ...
Unpacking libpcre2-dev:amd64 (10.34-7) ...
Selecting previously unselected package libpcre32-3:amd64.
Preparing to unpack .../14-libpcre32-3_2%3a8.39-12build1_amd64.deb ...
Unpacking libpcre32-3:amd64 (2:8.39-12build1) ...
Selecting previously unselected package libpcrecpp0v5:amd64.
Preparing to unpack .../15-libpcrecpp0v5_2%3a8.39-12build1_amd64.deb ...
Unpacking libpcrecpp0v5:amd64 (2:8.39-12build1) ...
Selecting previously unselected package libpcre3-dev:amd64.
Preparing to unpack .../16-libpcre3-dev_2%3a8.39-12build1_amd64.deb ...
Unpacking libpcre3-dev:amd64 (2:8.39-12build1) ...
Selecting previously unselected package libpng-dev:amd64.
Preparing to unpack .../17-libpng-dev_1.6.37-2_amd64.deb ...
Unpacking libpng-dev:amd64 (1.6.37-2) ...
Selecting previously unselected package libpng-tools.
Preparing to unpack .../18-libpng-tools_1.6.37-2_amd64.deb ...
Unpacking libpng-tools (1.6.37-2) ...
Selecting previously unselected package libreadline-dev:amd64.
Preparing to unpack .../19-libreadline-dev_8.0-4_amd64.deb ...
Unpacking libreadline-dev:amd64 (8.0-4) ...
Selecting previously unselected package r-base-core.
Preparing to unpack .../20-r-base-core_3.6.3-2_amd64.deb ...
Unpacking r-base-core (3.6.3-2) ...
Selecting previously unselected package r-cran-boot.
Preparing to unpack .../21-r-cran-boot_1.3-24-2_all.deb ...
Unpacking r-cran-boot (1.3-24-2) ...
Selecting previously unselected package r-cran-cluster.
Preparing to unpack .../22-r-cran-cluster_2.1.0-2_amd64.deb ...
Unpacking r-cran-cluster (2.1.0-2) ...
Selecting previously unselected package r-cran-foreign.
Preparing to unpack .../23-r-cran-foreign_0.8.75-1_amd64.deb ...
Unpacking r-cran-foreign (0.8.75-1) ...
Selecting previously unselected package r-cran-mass.
Preparing to unpack .../24-r-cran-mass_7.3-51.5-2_amd64.deb ...
Unpacking r-cran-mass (7.3-51.5-2) ...
Selecting previously unselected package r-cran-kernsmooth.
Preparing to unpack .../25-r-cran-kernsmooth_2.23-16-1+1_amd64.deb ...
Unpacking r-cran-kernsmooth (2.23-16-1+1) ...
Selecting previously unselected package r-cran-lattice.
Preparing to unpack .../26-r-cran-lattice_0.20-40-1_amd64.deb ...
Unpacking r-cran-lattice (0.20-40-1) ...
Selecting previously unselected package r-cran-nlme.
Preparing to unpack .../27-r-cran-nlme_3.1.144-1_amd64.deb ...
Unpacking r-cran-nlme (3.1.144-1) ...
Selecting previously unselected package r-cran-matrix.
Preparing to unpack .../28-r-cran-matrix_1.2-18-1_amd64.deb ...
Unpacking r-cran-matrix (1.2-18-1) ...
Selecting previously unselected package r-cran-mgcv.
Preparing to unpack .../29-r-cran-mgcv_1.8-31-1_amd64.deb ...
Unpacking r-cran-mgcv (1.8-31-1) ...
Selecting previously unselected package r-cran-survival.
Preparing to unpack .../30-r-cran-survival_3.1-8-1_amd64.deb ...
Unpacking r-cran-survival (3.1-8-1) ...
Selecting previously unselected package r-cran-rpart.
Preparing to unpack .../31-r-cran-rpart_4.1-15-2_amd64.deb ...
Unpacking r-cran-rpart (4.1-15-2) ...
Selecting previously unselected package r-cran-class.
Preparing to unpack .../32-r-cran-class_7.3-15-1_amd64.deb ...
Unpacking r-cran-class (7.3-15-1) ...
Selecting previously unselected package r-cran-nnet.
Preparing to unpack .../33-r-cran-nnet_7.3-13-1_amd64.deb ...
Unpacking r-cran-nnet (7.3-13-1) ...
Selecting previously unselected package r-cran-spatial.
Preparing to unpack .../34-r-cran-spatial_7.3-11-2build2_amd64.deb ...
Unpacking r-cran-spatial (7.3-11-2build2) ...
Selecting previously unselected package r-cran-codetools.
Preparing to unpack .../35-r-cran-codetools_0.2-16-1_all.deb ...
Unpacking r-cran-codetools (0.2-16-1) ...
Selecting previously unselected package r-recommended.
Preparing to unpack .../36-r-recommended_3.6.3-2_all.deb ...
Unpacking r-recommended (3.6.3-2) ...
Selecting previously unselected package r-base.
Preparing to unpack .../37-r-base_3.6.3-2_all.deb ...
Unpacking r-base (3.6.3-2) ...
Selecting previously unselected package liblzma-dev:amd64.
Preparing to unpack .../38-liblzma-dev_5.2.4-1_amd64.deb ...
Unpacking liblzma-dev:amd64 (5.2.4-1) ...
Selecting previously unselected package r-base-dev.
Preparing to unpack .../39-r-base-dev_3.6.3-2_all.deb ...
Unpacking r-base-dev (3.6.3-2) ...
Selecting previously unselected package r-doc-html.
Preparing to unpack .../40-r-doc-html_3.6.3-2_all.deb ...
Unpacking r-doc-html (3.6.3-2) ...
Selecting previously unselected package r-base-html.
Preparing to unpack .../41-r-base-html_3.6.3-2_all.deb ...
Unpacking r-base-html (3.6.3-2) ...
Setting up libpcrecpp0v5:amd64 (2:8.39-12build1) ...
Setting up bzip2-doc (1.0.8-2) ...
Setting up libjpeg-turbo8-dev:amd64 (2.0.3-0ubuntu1.20.04.1) ...
Setting up libncurses-dev:amd64 (6.2-0ubuntu2) ...
Setting up r-doc-html (3.6.3-2) ...
Setting up libpcre16-3:amd64 (2:8.39-12build1) ...
Setting up libpng-tools (1.6.37-2) ...
Setting up libpng-dev:amd64 (1.6.37-2) ...
Setting up libreadline-dev:amd64 (8.0-4) ...
Setting up libblas3:amd64 (3.9.0-1build1) ...
update-alternatives: using /usr/lib/x86_64-linux-gnu/blas/libblas.so.3 to provide /usr/lib/x86_64-linux-gnu/libblas.so.3 (libblas.so.3-x86_64-linux-gnu) in
 auto mode
Setting up libpcre32-3:amd64 (2:8.39-12build1) ...
Setting up libpcre2-posix2:amd64 (10.34-7) ...
Setting up liblzma-dev:amd64 (5.2.4-1) ...
Setting up libncurses5-dev:amd64 (6.2-0ubuntu2) ...
Setting up libjpeg8-dev:amd64 (8c-2ubuntu8) ...
Setting up libblas-dev:amd64 (3.9.0-1build1) ...
update-alternatives: using /usr/lib/x86_64-linux-gnu/blas/libblas.so to provide /usr/lib/x86_64-linux-gnu/libblas.so (libblas.so-x86_64-linux-gnu) in auto 
mode
Setting up libbz2-dev:amd64 (1.0.8-2) ...
Setting up liblapack3:amd64 (3.9.0-1build1) ...
update-alternatives: using /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3 to provide /usr/lib/x86_64-linux-gnu/liblapack.so.3 (liblapack.so.3-x86_64-linux
-gnu) in auto mode
Setting up libpcre2-dev:amd64 (10.34-7) ...
Setting up libpcre3-dev:amd64 (2:8.39-12build1) ...
Setting up libjpeg-dev:amd64 (8c-2ubuntu8) ...
Setting up r-base-core (3.6.3-2) ...

Creating config file /etc/R/Renviron with new version
Setting up liblapack-dev:amd64 (3.9.0-1build1) ...
update-alternatives: using /usr/lib/x86_64-linux-gnu/lapack/liblapack.so to provide /usr/lib/x86_64-linux-gnu/liblapack.so (liblapack.so-x86_64-linux-gnu) 
in auto mode
Setting up r-cran-lattice (0.20-40-1) ...
Setting up r-cran-nlme (3.1.144-1) ...
Setting up r-cran-cluster (2.1.0-2) ...
Setting up r-cran-nnet (7.3-13-1) ...
Setting up r-cran-codetools (0.2-16-1) ...
Setting up r-cran-boot (1.3-24-2) ...
Setting up r-base-dev (3.6.3-2) ...
Setting up r-cran-spatial (7.3-11-2build2) ...
Setting up r-cran-mass (7.3-51.5-2) ...
Setting up r-cran-foreign (0.8.75-1) ...
Setting up r-base-html (3.6.3-2) ...
Setting up r-cran-matrix (1.2-18-1) ...
Setting up r-cran-kernsmooth (2.23-16-1+1) ...
Setting up r-cran-mgcv (1.8-31-1) ...
Setting up r-cran-class (7.3-15-1) ...
Setting up r-cran-survival (3.1-8-1) ...
Setting up r-cran-rpart (4.1-15-2) ...
Setting up r-recommended (3.6.3-2) ...
Setting up r-base (3.6.3-2) ...
Processing triggers for desktop-file-utils (0.24-1ubuntu3) ...
Processing triggers for mime-support (3.64ubuntu1) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for gnome-menus (3.36.0-1ubuntu1) ...
Processing triggers for libc-bin (2.31-0ubuntu9) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for install-info (6.7.0.dfsg.2-5) ...

If no errors is reported, the R software is installed.

Verify the R environment installed

We can start the R REPL (Read-Eval-Print Loop) to verify the R installation by running R:

$ R

It should print output similar to

R version 3.6.3 (2020-02-29) -- "Holding the Windsock"
Copyright (C) 2020 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.

> 

Run the NLM demo by inputting the following command into the REPL

> demo(nlm)

R will print output as follows and start a Window to show the plot.

> demo(nlm)

    demo(nlm)
    ---- ~~~

Type  <Return>   to start : 

> #  Copyright (C) 1997-2009, 2017 The R Core Team
> 
> ### Helical Valley Function
> ### Page 362 Dennis + Schnabel
> 
> require(stats); require(graphics); require(utils)

> theta <- function(x1,x2) (atan(x2/x1) + (if(x1 <= 0) pi else 0))/ (2*pi)

> ## but this is easier :
> theta <- function(x1,x2) atan2(x2, x1)/(2*pi)

> f <- function(x) {
+     f1 <- 10*(x[3] - 10*theta(x[1],x[2]))
+     f2 <- 10*(sqrt(x[1]^2+x[2]^2)-1)
+     f3 <- x[3]
+     return(f1^2 + f2^2 + f3^2)
+ }

> ## explore surface {at x3 = 0}
> x <- seq(-1, 2, length.out=50)

> y <- seq(-1, 1, length.out=50)

> z <- apply(as.matrix(expand.grid(x, y)), 1, function(x) f(c(x, 0)))

> contour(x, y, matrix(log10(z), 50, 50))
Hit <Return> to see next plot: 

> str(nlm.f <- nlm(f, c(-1,0,0), hessian = TRUE))
List of 6
 $ minimum   : num 1.24e-14
 $ estimate  : num [1:3] 1.00 3.07e-09 -6.06e-09
 $ gradient  : num [1:3] -3.76e-07 3.49e-06 -2.20e-06
 $ hessian   : num [1:3, 1:3] 2.00e+02 -4.07e-02 9.77e-07 -4.07e-02 5.07e+02 ...
 $ code      : int 2
 $ iterations: int 27

> points(rbind(nlm.f$estim[1:2]), col = "red", pch = 20)

> stopifnot(all.equal(nlm.f$estimate, c(1, 0, 0)))

> ### the Rosenbrock banana valley function
> 
> fR <- function(x)
+ {
+     x1 <- x[1]; x2 <- x[2]
+     100*(x2 - x1*x1)^2 + (1-x1)^2
+ }

> ## explore surface
> fx <- function(x)
+ {   ## `vectorized' version of fR()
+     x1 <- x[,1]; x2 <- x[,2]
+     100*(x2 - x1*x1)^2 + (1-x1)^2
+ }

> x <- seq(-2, 2, length.out=100)

> y <- seq(-0.5, 1.5, length.out=100)

> z <- fx(expand.grid(x, y))

> op <- par(mfrow = c(2,1), mar = 0.1 + c(3,3,0,0))

> contour(x, y, matrix(log10(z), length(x)))
Hit <Return> to see next plot: 

You can hit the Enter key to see more plots and try more demo functions.

Install RStudio Server on Ubuntu

After the R environment is ready, we can move to to install the RStudio Server.

First, download the RStudio Server software package by

$ wget https://download2.rstudio.org/server/bionic/amd64/rstudio-server-1.3.1093-amd64.deb

Then, install the deb package for RStudio Server by

$ sudo apt install ./rstudio-server-1.3.1093-amd64.deb 

It should print output as follows.

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Note, selecting 'rstudio-server' instead of './rstudio-server-1.3.1093-amd64.deb'
The following NEW packages will be installed:
  rstudio-server
0 upgraded, 1 newly installed, 0 to remove and 1 not upgraded.
Need to get 0 B/66.7 MB of archives.
After this operation, 623 MB of additional disk space will be used.
Get:1 /home/ruser/Downloads/rstudio-server-1.3.1093-amd64.deb rstudio-server amd64 1.3.1093 [66.7 MB]
Selecting previously unselected package rstudio-server.
(Reading database ... 265935 files and directories currently installed.)
Preparing to unpack .../rstudio-server-1.3.1093-amd64.deb ...
Unpacking rstudio-server (1.3.1093) ...
Setting up rstudio-server (1.3.1093) ...
useradd: user 'rstudio-server' already exists
Created symlink /etc/systemd/system/multi-user.target.wants/rstudio-server.service → /lib/systemd/system/rstudio-server.service.
● rstudio-server.service - RStudio Server
     Loaded: loaded (/lib/systemd/system/rstudio-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2020-10-02 21:48:31 HKT; 1s ago
    Process: 15040 ExecStart=/usr/lib/rstudio-server/bin/rserver (code=exited, status=0/SUCCESS)
   Main PID: 15053 (rserver)
      Tasks: 3 (limit: 9330)
     Memory: 2.6M
     CGroup: /system.slice/rstudio-server.service
             └─15053 /usr/lib/rstudio-server/bin/rserver

Oct 02 21:48:31 ubuntu-linux systemd[1]: Starting RStudio Server...
Oct 02 21:48:31 ubuntu-linux systemd[1]: Started RStudio Server.

Verify the installation of RStudio Server

After the software package is installed, the rstudio-server service is automatically started. By default, it provides the service on TCP port 8787. Remember to open this port in the firewall from the Cloud Service and the Ubuntu Linux (if you use any firewall software enabled). We can then connect to RStudio Server by using a browser to visit:

http://<server domain or IP>:8787

(or, http://localhost:8787 if the Ubuntu server is the local PC).

The login web page will show. Enter the Ubuntu Linux user’s username and password. Then we can access the RStudio IDE from the browser. Similarly, run the demo(nlm) command and the plot will show.

If you can successfully show the plot, congratulations and the R environment and RStudio IDE is ready for you to start the journey with R!

Leave a Reply

Your email address will not be published. Required fields are marked *