In this article, by Hernán G. Resnizky, author of the book Learning Shiny, the main objective will be to learn how to install all the needed components to build an application in R with Shiny. Additionally, some general ideas about what R is will be covered in order to be able to dive deeper into programming using R.
The following topics will be covered:
(For more resources related to this topic, see here.)
As stated on the R-project main website:
"R is a language and environment for statistical computing and graphics."
R is a successor of S and is a GNU project. This means, briefly, that anyone can have access to its source codes and can modify or adapt it to their needs. Nowadays, it is gaining territory over classic commercial software, and it is, along with Python, the most used language for statistics and data science.
Regarding R's main characteristics, the following can be considered:
R can be installed in every operating system. It is highly recommended to download the program directly from http://cran.rstudio.com/ when working on Windows or Mac OS. On Ubuntu, R can be easily installed from the terminal as follows:
sudo apt-get update
sudo apt-get install r-base
sudo apt-get install r-base-dev
The installation of r-base-dev is highly recommended as it is a package that enables users to compile the R packages from source, that is, maintain the packages or install additional R packages directly from the R console using the install.packages() command.
To install R on other UNIX-based operating systems, visit the following links:
When working on Windows, R can be launched via its application. After the installation, it is available as any other program on Windows. When opening the program, a window like this will appear:
When working on Linux, you can access the R console directly by typing R on the command line:
In both the cases, R executes in runtime. This means that you can type in code, press Enter, and the result will be given immediately as follows:
> 2+2
[1] 4
The R application in any operating system does not provide an easy environment to develop code. For this reason, it is highly recommended (not only to write web applications in R with Shiny, but for any task you want to perform in R) to use an Integrated Development Environment (IDE).
As with other programming languages, there is a huge variety of IDEs available for R. IDEs are applications that make code development easier and clearer for the programmer. RStudio is one of the most important ones for R, and it is especially recommended to write web applications in R with Shiny because this contains features specially designed for R. Additionally, RStudio provides facilities to write C++, Latex, or HTML documents and also integrates them to the R code.
RStudio also provides version control, project management, and debugging features among many others.
RStudio for desktop computers can be downloaded from its official website at http://www.rstudio.com/products/rstudio/download/ where you can get versions of the software for Windows, MAC OS X, Ubuntu, Debian, and Fedora.
Before installing and running RStudio, it is important to have R installed. As it is an IDE and not the programming language, it will not work at all. The following screenshot shows RStudio's starting view:
At the first glance, the following four main windows are available:
Along with numerous features, RStudio also provides keyboard shortcuts. A few of them are listed as follows:
Description |
Windows/Linux |
OSX |
Complete the code. |
Tab |
Tab |
Run the selected piece of code. If no piece of code is selected, the active line is run. |
Ctrl + Enter |
⌘ + Enter |
Comment the selected block of code. |
Ctrl + Shift + C |
⌘ + / |
Create a section of code, which can be expanded or compressed by clicking on the arrow to the left. Additionally, it can be accessed by clicking on it in the bottom left menu. |
##### |
##### |
Find and replace. |
Ctrl + F |
⌘ + F |
The following screenshots show how a block of code can be collapsed by clicking on the arrow and how it can be accessed quickly by clicking on its name in the bottom-left part of the window:
Clicking on the circled arrow will collapse the Section 1 block, as follows:
The full list of shortcuts can be found at https://support.rstudio.com/hc/en-us/articles/200711853-Keyboard-Shortcuts.
For further information about other RStudio features, the full documentation is available at https://support.rstudio.com/hc/en-us/categories/200035113-Documentation.
Shiny is a package created by RStudio, which enables to easily interface R with a web browser. As stated in its official documentation, Shiny is a web application framework for R that makes it incredibly easy to build interactive web applications with R.
One of its main advantages is that there is no need to combine R code with HTML/JavaScript code as the framework already contains prebuilt features that cover the most commonly used functionalities in a web interactive application. There is a wide range of software that has web application functionalities, especially oriented to interactive data visualization. What are the advantages of using R/Shiny then, you ask? They are as follows:
As with any other package available in the CRAN repositories, the easiest way to install Shiny is by executing install.packages("shiny").
The following output should appear on the console:
Due to R's extensibility, many of its packages use elements (mostly functions) from other packages. For this reason, these packages are loaded or installed when the package that is dependent on them is loaded or installed. This is called dependency. Shiny (on its 0.10.2.1 version) depends on Rcpp, httpuv, mime, htmltools, and R6.
An R session is started only with the minimal packages loaded. So if functions from other packages are used, they need to be loaded before using them. The corresponding command for this is as follows:
library(shiny)
When installing a package, the package name must be quoted but when loading the package, it must be unquoted.
After these instructions, the reader should be able to install all the fundamental elements to create a web application with Shiny. Additionally, he or she must have acquired at least a general idea of what R and the R project is.
Further resources on this subject: