Download Web Application Development with R Using Shiny - Second Edition - Sample Chapter PDF

TitleWeb Application Development with R Using Shiny - Second Edition - Sample Chapter
File Size7.0 MB
Total Pages27
Document Text Contents
Page 1


C o m m u n i t y E x p e r i e n c e D i s t i l l e d

Integrate the power of R with the simplicity of Shiny to deliver
cutting-edge analytics over the Web

Web Application Development
with R Using Shiny
Second Edition

hris B


Web Application Development
with R Using Shiny
Second Edition

R is a highly fl exible and powerful tool for analyzing
and visualizing data. Most of the applications built
using various libraries with R are desktop-based. But
what if you want to go on the Web? Here comes Shiny
to your rescue!

Shiny allows you to create interactive web applications
using the excellent analytical and graphical capabilities
of R. This book will guide you through basic data
management and analysis with R through your fi rst Shiny
application, and then show you how to integrate Shiny
applications with your own web pages. Finally, you will
learn how to fi nely control the inputs and outputs of your
application, along with using other packages to build
state-of-the-art applications, including dashboards.

Who this book is written for
This book is for anybody who wants to produce interactive
data summaries over the web, whether you want to
share them with a few colleagues or the whole world.
No previous experience with R, Shiny, HTML, or CSS is
required to begin using this book, although you should
possess some previous experience with programming in
a different language.

$ 39.99 US
£ 25.99 UK

Prices do not include
local sales tax or VAT
where applicable

Chris Beeley

What you will learn from this book

 Build interactive applications using Shiny's
built-in widgets

 Use the built-in layout functions in Shiny to
produce user-friendly applications

 Integrate Shiny applications with web pages
and customize them using HTML and CSS

 Harness the power of JavaScript and jQuery
to customize your applications

 Engage your users and build better analytics
using interactive plots

 Debug your applications using Shiny's
built-in functions

 Deliver simple and powerful analytics across
your organization using Shiny dashboards

 Share your applications with colleagues or
over the Internet using cloud services or your
own server

eb A

pplication D

ent w
ith R

sing S

hiny S
econd E



community experience dist i l led

Visit for books, eBooks,
code, downloads, and PacktLib.

Free Sam

Page 2

In this package, you will find:
 The author biography

 A preview chapter from the book, Chapter 1 'Getting Started with R and


 More information on Web Application Development with R using Shiny -

Second Edition

Page 13


Getting Started with R and Shiny!

[ 8 ]

Note the use of the comma with nothing before it to indicate that all rows are
required. In general, dataframes can be accessed using dataObject[x,y] with x
being the number(s) or name(s) of the rows required and y being the number(s) or
name(s) of the columns required. For example, if the fi rst 10 rows were required
from the pageViews column, it could be achieved like this:

> analyticsData[1:10,"pageViews"]

[1] 836 676 940 689 647 899 934 718 776 570

Leaving the space before the comma blank returns all rows, and the space after the
comma blank returns all variables. For example, the following command returns the
fi rst three rows of all variables:

> analyticsData[1:3,]

The following screenshot shows the output of this command:

Dataframes are a special type of list. Lists can hold many different types of data
including lists. As with many data types in R, their elements can be named, which
can be useful to write code that is easy to understand. Let's make a list of the options
for dinner, with drink quantities expressed in milliliters.

In the following example, please note also the use of the c() function, which is used
to produce vectors and lists by giving their elements separated by commas. R will
pick an appropriate class for the return value, string for vectors that contain strings,
numeric for those that only contain numbers, logical for Boolean values, and so on:

> dinnerList <- list("Vegetables" =

c("Potatoes", "Cabbage", "Carrots"),

"Dessert" = c("Ice cream", "Apple pie"),

"Drinks" = c(250, 330, 500)


Note that code is indented throughout, although entering directly into the
console will not produce indentations; it is done for readability.

Page 14

Chapter 1

[ 9 ]

Indexing is similar to dataframes (which are, after all, just a special instance of a list).
They can be indexed by number, as shown in the following command:

> dinnerList[1:2]


[1] "Potatoes" "Cabbage" "Carrots"


[1] "Ice cream" "Apple pie"

This returns a list. Returning an object of the appropriate class is achieved
using [[]]:

> dinnerList[[3]]

[1] 250 330 500

In this case a numeric vector is returned. They can be indexed also by name:

> dinnerList["Drinks"]


[1] 250 330 500

Note that this, also, returns a list.

Matrices and arrays, which, unlike dataframes, only hold one type of data, also make
use of square brackets for indexing, with analyticsMatrix[, 3:6] returning all rows
of the third to sixth column, analyticsMatrix[1, 3] returning just the fi rst row of the
third column, and analyticsArray[1, 2, ] returning the fi rst row of the second column
across all of the elements within the third dimension.

Variable types
R is a dynamically typed language and you are not required to declare the type of
your variables. It is worth knowing, of course, about the different types of variable
that you might read or write using R. The different types of variable can be stored
in a variety of structures, such as vectors, matrices, and dataframes, although some
restrictions apply as detailed previously (for example, matrices must contain only
one variable type):

• Declaring a variable with at least one string in will produce a vector of
strings (in R, the character data type):
> c("First", "Third", 4, "Second")

[1] "First" "Third" "4" "Second"

Page 26

Chapter 1

Now there are a few different statistical distributions to pick from and a different
method of selecting the number of observations. By now, you should be looking
at the web page and imagining all the possibilities there are to produce your own
interactive data summaries and styling them just how you want, quickly and simply.
By the end of the next chapter, you'll have made your own application with the
default UI, and by the end of the book, you'll have complete control over the styling
and be pondering where else you can go.

There are lots of other examples included with the Shiny library; just type
runExample() at the console to be provided with a list.

To see some really powerful and well-featured Shiny applications, take a look at the
showcase at

In this chapter, we installed R and explored the different options for GUIs and IDEs,
and looked at some examples of the power of R. We saw how R makes it easy to
manage and reformat data and produce beautiful plots with a few lines of code. You
also learned a little about the coding conventions and data structures of R. We saw
how to format a dataset and produce an interactive plot in a document quickly and
easily. Finally, we installed Shiny, ran the examples included in the package, and got
introduced to a couple of basic concepts within Shiny.

In the next chapter, we will go on to build our own Shiny application using the
default UI.

Page 27

Where to buy this book
You can buy Web Application Development with R using Shiny - Second Edition from

the Packt Publishing website.

Alternatively, you can buy the book from Amazon,, Computer Manuals and most internet

book retailers.

Click here for ordering and shipping details.

Stay Connected:

Get more information Web Application Development with R using Shiny - Second Edition

Similer Documents