Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Web Application Development with R Using Shiny Second Edition
Web Application Development with R Using Shiny Second Edition

Web Application Development with R Using Shiny Second Edition: Integrate the power of R with the simplicity of Shiny to deliver cutting-edge analytics over the Web , Second Edition

Arrow left icon
Profile Icon Chris Beeley
Arrow right icon
Free Trial
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.5 (10 Ratings)
Paperback Jan 2016 194 pages 2nd Edition
eBook
NZ$14.99 NZ$51.99
Paperback
NZ$64.99
Subscription
Free Trial
Arrow left icon
Profile Icon Chris Beeley
Arrow right icon
Free Trial
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.5 (10 Ratings)
Paperback Jan 2016 194 pages 2nd Edition
eBook
NZ$14.99 NZ$51.99
Paperback
NZ$64.99
Subscription
Free Trial
eBook
NZ$14.99 NZ$51.99
Paperback
NZ$64.99
Subscription
Free Trial

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Web Application Development with R Using Shiny Second Edition

Chapter 1. Getting Started with R and Shiny!

R is free and open source as well as being the pre-eminent tool for statisticians and data scientists. It has more than 6000 user-contributed packages, which help users with tasks as diverse as chemistry, biology, physics, finance, psychology, and medical science, as well as drawing extremely powerful and flexible statistical graphics.

In recent years, R has become more and more popular, and there are an increasing number of packages for R, which make cleaning, analyzing, and presenting data on the web easy for everybody. The Shiny package, in particular, makes it incredibly easy to deliver interactive data summaries and queries to end users through any modern web browser. You're reading this book because you want to use these powerful and flexible tools for your own content.

This book will show you how, right from starting with R, to build your own interfaces with Shiny and integrate them with your own websites. In this chapter, we're going to cover the following:

  • Download and install R and choose a code editing environment/IDE
  • Look at the power of R and learn about how RStudio and contributed packages can make writing code, managing projects, and working with data easier
  • Install Shiny and run the examples
  • Take a look at some awesome Shiny applications and some of the elements of the Shiny application we will build over the course of this book

R is a big subject, and this is a whistle-stop tour; so if you get a little lost along the way, don't worry. This chapter is really all about showing you what's out there and encouraging you to delve deeper into the bits that interest you and showing you places you can go for help if you want to learn more on a particular subject.

Installing R

R is available for Windows, Mac OS X, and Linux at cran.r-project.org. Source code is also available at the same address. It is also included in many Linux package management systems; Linux users are advised to check before downloading from the web. Details on installing from source or binary for Windows, Mac OS X, and Linux are all available at cran.r-project.org/doc/manuals/R-admin.html.

The R console

Windows and Mac OS X users can run the R application to launch the R console. Linux and Mac OS X users can also run the R console straight from the terminal by typing R.

In either case, the R console itself will look something like this:

The R console

R will respond to your commands right from the terminal. Let's have a go:

> 2 + 2
[1] 4

The [1] tells you that R returned one result, in this case, 4. The following command shows how to print Hello world:

> print("Hello world!")
[1] "Hello world!"

The following command shows the multiples of pi:

> 1:10 * pi
[1]  3.141593  6.283185  9.424778 12.566371 15.707963 18.849556
[7] 21.991149 25.132741 28.274334 31.415927

This example illustrates vector-based programming in R. 1:10 generates the numbers 1:10 as a vector, and each is then multiplied by pi, which returns another vector, the elements each being pi times larger than the original. Operating on vectors is an important part of writing simple and efficient R code. As you can see, R again numbers the values it returns at the console with the seventh value being 21.99.

One of the big strengths of using R is the graphics capability, which is excellent even in a vanilla installation of R (these graphics are referred to as base graphics because they ship with R). When adding packages such as ggplot2 and some of the JavaScript-based packages, R becomes a graphical tour de force, whether producing statistical, mathematical, or topographical figures, or indeed many other types of graphical output. To get a flavor of the power of base graphics, simply type the following at the console:

> demo(graphics)

You can also type the following command:

> demo(persp)

There is more on ggplot2 and base graphics later in the chapter and a brief introduction to JavaScript and D3-based packages for R in Chapter 6, Advanced Applications II–Using JavaScript Libraries in Shiny Applications.

Enjoy! There are many more examples of R graphics at gallery.r-enthusiasts.com/.

Code editors and IDEs

The Windows and OSX versions of R both come with built-in code editors, which allow code to be edited, saved, and sent to the R console. It's hard to recommend that you use this because it is rather primitive. Most users would be best served by RStudio (rstudio.com/), which includes project management and version control (including support for Git, which is covered in Chapter 7, Sharing Your Creations), viewing of data and graphics, code completion, package management, and many other features. The following is an illustrative screenshot of an RStudio session:

Code editors and IDEs

As can be seen, in the top-left corner, there is the code editing pane (with syntax highlighting). The clockwise move from there will take you to the environment pane (in which the different objects loaded into the session can be seen); the viewing pane containing various options such as Files, Plots, Build, Help, and the console (again, with syntax highlighting). In the middle, there is one of the most useful features of RStudio—the ability to view dataframes. This function also comes with sorting and filtering by column.

However, if you already use an IDE for other types of code, it is quite likely that R can be well integrated into it. Examples of IDEs with good R integration include the following:

  • Emacs with the Emacs Speaks Statistics plugin
  • Vim with the Vim-R plugin
  • Eclipse with the StatET plugin

Learning R

There are almost as many uses for R as there are people using it. It is not possible to cover your specific needs within this book. However, it is likely that you wish to use R to process, query, and visualize data such as sales figures, satisfaction surveys, concurrent users, sporting results, or whatever types of data your organization processes. Later chapters will concentrate on Google Analytics data downloaded from the API, but for now, let's just take a look at the basics.

Getting help

There are many books and online materials that cover all aspects of R. The name R can make it difficult to come up with useful web search hits (substituting CRAN for R can sometimes help); nonetheless, searching for R tutorial brings back useful results. Some useful resources include the following:

At the R console, ?functionname (for example, ?help) brings up help materials and use of ??help will bring up a list of potentially relevant functions from installed packages.

Subscribing to and asking questions on the R-help mailing list at r-project.org/mail.html allows you to communicate with some of the leading figures in the R community as well as many other talented enthusiasts. Do read the posting guide and research your question before you ask any questions because it's a busy and sometimes unforgiving list.

There are two Stack Exchange communities, which can provide further help at stats.stackexchange.com/ (for questions about statistics and visualization with R) and stackoverflow.com/ (for questions about programming with R).

There are many ways to learn R and related subjects online; RStudio has a very useful list on their website available at goo.gl/8tX7FP.

Loading data

The simplest way of loading data into R is probably using a comma-separated value (.csv) spreadsheet file, which can be downloaded from many data sources and loaded and saved in all spreadsheet software (such as Excel or LibreOffice). The read.table() command imports data of this type by specifying the separator as a comma, or there is a function specifically for .csv files, read.csv(), as shown in the following command:

> analyticsData <- read.table("~/example.csv", sep = ",")

Otherwise, you can use the following command:

> analyticsData <- read.csv("~/example.csv")

Note that unlike in other languages, R uses <- for assignment as well as =. Assignment can be made the other way using ->. The result of this is that y can be told to hold the value of 4 like this, y <- 4, or like this, 4 -> y. There are some other more advanced things that can be done with assignment in R, but don't worry about them now. Just write code using the assignment operator in the preceding example and you'll be just like the natives that you will come across on forums and blog posts.

Either of the preceding code examples will assign the contents of the Analytics.csv file to a dataframe named analyticsData, with the first row of the spreadsheet providing the variable names. A dataframe is a special type of object in R, which is designed to be useful for the storage and analysis of data.

Data types and structures

There are many data types and structures of data within R. The following topics summarize some of the main types and structures that you will use when building Shiny applications.

Dataframes, lists, arrays, and matrices

Dataframes have several important features, which make them useful for data analysis:

  • Rectangular data structures with the typical use being cases (for example, days in one month) down the rows and variables (page views, unique visitors, or referrers) along the columns.
  • A mix of data types is supported. A typical dataframe might include variables containing dates, numbers (integers or floats), and text.
  • With subsetting and variable extraction, R provides a lot of built-in functionality to select rows and variables within a dataframe.
  • Many functions include a data argument, which makes it very simple to pass dataframes into functions and process only the variables and cases that are relevant, which makes for cleaner and simpler code.

We can inspect the first few rows of the dataframe using the head(analyticsData) command. The following screenshot shows the output of this command:

Dataframes, lists, arrays, and matrices

As you can see, there are four variables within the dataframe—one contains dates, two contain integer variables, and one contains a numeric variable. There is more about variable types in R shown in the following paragraphs.

Variables can be extracted from dataframes very simply using the $ operator as follows:

> analyticsData$pageViews
 [1] 836 676 940 689 647 899 934 718 776 570 651 816
[13] 731 604 627 946 634 990 994 599 657 642 894 983
[25] 646 540 756 989 965 821

Also, variables can be extracted from dataframes using [], as shown in the following command:

> analyticsData[, "pageViews"]

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 first 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 first three rows of all variables:

> analyticsData[1:3,]

The following screenshot shows the output of this command:

Dataframes, lists, arrays, and matrices

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

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

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]
$Vegetables
[1] "Potatoes" "Cabbage"  "Carrots"

$Dessert
[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"]
$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 first row of the third column, and analyticsArray[1, 2, ] returning the first 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"
    

    You will notice that the numeral 4 is converted to a string, "4". This is as a result of coercion, in which elements of a data structure are converted to other data types in order to fit within the types allowed within the data structure. Coercion occurs automatically, as in this case, or with an explicit call to the as() function, for example, as.numeric(), or as.Date().

  • Declaring a variable with just numbers will produce a numeric vector:
    > c(15, 10, 20, 11, 0.4, -4)
    [1] 15.0 10.0 20.0 11.0  0.4 -4.0
    
  • R includes, of course, also a logical data type:
    > c(TRUE, FALSE, TRUE, TRUE, FALSE)
    [1]  TRUE FALSE  TRUE  TRUE FALSE
    
  • A data type exists for dates, often a source of problems for beginners:
    > as.Date(c("2013/10/24", "2012/12/05", "2011/09/02"))
    [1] "2013-10-24" "2012-12-05" "2011-09-02"
    
  • The use of the factor data type tells R all of the possible values of a categorical variable, such as gender or species:
    > factor(c("Male", "Female", "Female", "Male", "Male"),
      levels = c("Female", "Male")
    [1] Male   Female Female Male   Male
    Levels: Female Male
    

Functions

As you grow in confidence with R you will wish to begin writing your own functions. This is achieved very simply and in a manner quite reminiscent of many other languages. You will no doubt wish to read more about writing functions in R in a fuller treatment, but just to give you an idea, here is a function called the sumMultiply function which adds together x and y and multiplies by z:

sumMultiply <- function(x, y, z){
  final = (x+y) * z
  return(final)
}

This function can now be called using sumMultiply(2, 3, 6), which will return 2 plus 3 times 6, which gives 30.

Objects

There are many special object types within R which are designed to make it easier to analyze data. Functions in R can be polymorphic, that is to say they can respond to different data types in different ways in order to produce the output that the user desires. For example, the plot() function in R responds to a wide variety of data types and objects, including single dimension vectors (each value of y plotted sequentially) and two-dimensional matrices (producing a scatterplot), as well as specialized statistical objects such as regression models and time series data. In the latter case, plots specialized for these purposes are produced.

As with the rest of this introduction, don't worry if you haven't written functions before, or don't understand object concepts and aren't sure what this all means. You can produce great applications without understanding all these things, but as you do more and more with R you will start to want to learn more detail about how R works and how experts produce R code. This introduction is designed to give you a jumping off point to learn more about how to get the best out of R (and Shiny).

Base graphics and ggplot2

There are lots of user-contributed graphics packages in R that can produce some wonderful graphics. You may wish to take a look for yourself at the CRAN task view cran.r-project.org/web/views/Graphics.html. We will have a very quick look at two approaches: base graphics, so-called because it is the default graphical environment within a vanilla installation of R, and ggplot2, a highly popular user-contributed package produced by Hadley Wickham, which is a little trickier to master than base graphics, but can very rapidly produce a wide range of graphical data summaries. We will cover two graphs familiar to all, the bar chart and the line chart.

Bar chart

Useful when comparing quantities across categories, bar charts are very simple within base graphics, particularly when combined with the table() command. We will use the mpg dataset which comes with the ggplot2 package; it summarizes different characteristics of a range of cars. First, let's install the ggplot2 package. You can do this straight from the console:

> install.packages("ggplot2")

Alternatively, you can use the built in package functions in IDEs like RStudio or RKWard. We'll need to load the package at the beginning of each session in which we wish to use this dataset or the ggplot2 package itself. From the console type the following command:

> library(ggplot2)

We will use the table() command to count the number of each type of car featured in the dataset:

> table(mpg$class)

This returns a table object (another special object type within R) that contains the following columns shown in the screenshot:

Bar chart

Producing a bar chart of this object is achieved simply like this:

> barplot(table(mpg$class), main = "Base graphics")

The barplot function takes a vector of frequencies. Where they are named, as here (the table()command returning named frequencies in table form), names are automatically included on the x axis. The defaults for this graph are rather plain; explore ?barplot and ?par to learn more about fine-tuning your graphics.

We've already loaded the ggplot2 package in order to use the mpg dataset, but if you have shut down R in between these two examples you will need to reload it by using the following command:

> library(ggplot2)

The same graph is produced in ggplot2 as follows:

> ggplot(data = mpg, aes(x = class)) + geom_bar() + ggtitle("ggplot2")

This ggplot call shows the three fundamental elements of ggplot calls—the use of a dataframe (data = mpg), the setup of aesthetics (aes(x = class)), which determines how variables are mapped onto axes, colors, and other visual features, and the use of + geom_xxx(). A ggplot call sets up the data and aesthetics, but does not plot anything. Functions such as geom_bar() (there are many others, see ??geom) tell ggplot what type of graph to plot, as well as taking optional arguments, for example, geom_bar() optionally takes a position argument which defines whether the bars should be stacked, offset, or stretched to a common height to show proportions instead of frequencies.

These elements are the key to the power and flexibility that ggplot2 offers. Once the data structure is defined, ways of visualizing that data structure can be added and taken away easily, not only in terms of the type of graphic (bar, line, or scatter) but also the scales and co-ordinate system (log10; polar coordinates) and statistical transformations (smoothing data, summarizing over spatial co-ordinates). The appearance of plots can be easily changed with pre-set and user-defined themes, and multiple plots can be added in layers (that is, adding to one plot) or facets (that is, drawing multiple plots with one function call).

Line chart

Line charts are most often used to indicate change, particularly over time. This time we will use the longley dataset, featuring economic variables between 1947 and 1962:

> plot(x = 1947 : 1962, y = longley$GNP, type = "l",
  xlab = "Year", main = "Base graphics")

The x axis is given very simply by 1947 : 1962, which enumerates all the numbers between 1947 and 1962, and the type = "l" argument specifies the plotting of lines, as opposed to points or both.

The ggplot call looks a lot like the bar chart except with an x and y dimension in the aesthetics this time. The command looks as follows:

> ggplot(longley, aes(x = 1947 : 1962, y = GNP)) + geom_line() +
  xlab("Year") + ggtitle("ggplot2")

Base graphics and ggplot versions of the bar chart are shown in the following screenshot for the purposes of comparison:

Line chart

Advanced tasks with dplyr and ggvis

Just to get a taste of how easy it can be to do some really cool things with R and to start to build the foundation of the Shiny application that we are going to build through the course of this book, let's build a few graphics using some Google Analytics data and present them in an interactive document. We are going to use two contributed packages, dplyr and ggvis. The dplyr package provides very powerful functions for selecting, filtering, combining, and summarizing datasets. As you will see throughout this book, dplyr allows you to very rapidly process data to your exact specifications. The ggvis package provides very simple functions to make your visualizations interactive.

We're going to run through some of the code very quickly indeed, so you can get a feeling for some of the tasks and structures involved, but we'll return to this application later in the book where everything will be explained in detail. Just relax and enjoy the ride for now. If you want to browse or run all the code, it is available at chrisbeeley.net/website/index.html.

The Google Analytics code is not included because it requires a login for the Google Analytics API; instead, you can download the actual data from the previously mentioned link. Getting your own account for Google Analytics and downloading data from the API is covered in Chapter 5, Advanced Applications I – Dashboards. I am indebted to examples at goo.gl/rPFpF9 and at goo.gl/eL4Lrl for helpful examples of showing data on maps within R.

Preparing the data

In order to prepare the data for plotting, we will make use of dplyr. As with all packages that are included on the CRAN repository of packages (cran.r-project.org/web/packages/), it can be installed using the package management functions in RStudio or other GUIs, or by typing install.packages("dplyr") at the console. It's worth noting that there are even more packages available elsewhere (for example, on GitHub), which can be compiled from the source.

The first job is to prepare the data that will demonstrate some of the power of the dplyr package using the following code:

groupByDate =
filter(gadf, networkDomain %in% topThree$networkDomain) %>%
group_by(YearMonth, networkDomain) %>%
summarise(meanSession = mean(sessionDuration, na.rm = TRUE),
  users = sum(users),
  newUsers = sum(newUsers),
  sessions = sum(sessions))

This single block of code, all executed in one line, produces a dataframe suitable for plotting and uses chaining to enhance the simplicity of the code. Three separate data operations, filter(), group_by(), and summarise(), are all used, with the results from each being sent to the next instruction using the %>% operator. The three instructions carry out the following tasks:

  • filter(): This is similar to subset(). This operation keeps only rows that meet certain requirements, in this case, data for which networkDomain (the originating ISP of the page view) is in the top three most common ISPs. This has already been calculated and stored within topThree$networkDomain (this step is omitted here for brevity).
  • group_by(): This allows operations to be carried out on subsets of data points, in this case, data points subsetted by the year and month and by the originating ISP.
  • summarise(): This carries out summary functions such as sum or mean on several data points.

So, to summarize, the preceding code filters the data to select only the ISPs with the most users overall, groups it by the year or month and the ISP, and finds the sum or mean of several of the metrics within it (sessionDuration, users, and so on).

A simple interactive line plot

We already saw how easy it is to draw line plots in ggplot2. Let's add some Shiny magic to a line plot now. This can be achieved very easily indeed in RStudio by just navigating to File | New | R Markdown | New Shiny document and installing the dependencies when prompted. This will create a new R Markdown document with interactive Shiny elements. R Markdown is an extension of Markdown (daringfireball.net/projects/markdown/), which is itself a markup language, such as HTML or LaTeX, which is designed to be easy to use and read. R Markdown allows R code chunks to be run within a Markdown document, which renders the contents dynamic. There is more information about Markdown and R Markdown in Chapter 2, Building Your First Application. This section gives a very rapid introduction to the type of results possible using Shiny-enabled R Markdown documents.

For more details on how to run interactive documents outside RStudio, refer to goo.gl/NGubdo. Once the document is set up, the code is as follows:

# add interactive UI element
inputPanel(
  checkboxInput("smooth", label = "Add smoother?", value = FALSE)
)

# draw the plot
renderPlot({
  thePlot = ggplot(groupByDate, aes(x = Date, y = meanSession,
  group = networkDomain, colour = networkDomain)) +
  geom_line() + ylim(0, max(groupByDate$meanSession))
  if(input$smooth){
    thePlot = thePlot + geom_smooth()
  }
  print(thePlot)
})

That's it! You'll have an interactive graphic once you run the document (click on Run document in RStudio or use the run() command from the rmarkdown package), as shown in the following screenshot:

A simple interactive line plot

As you can see, Shiny allows us to turn on or off a smoothing line courtesy of geom_smooth() from the ggplot2 package.

Producing an interactive map (click to examine the value associated with each country) using the ggvis package is as simple as the following:

getUsers = function(x){
  if(is.null(x)) return(NULL)
    theCountry = head(filter(map.df, id == x$id), 1)$CNTRY_NAME
  return(filter(groupByCountry, country == theCountry)$users)
}

map.df %>%
group_by(group, id) %>%
ggvis(~long, ~lat) %>%
layer_paths(fill = ~ users) %>%
scale_numeric("fill", trans = "log", label = "log(users)") %>%
add_tooltip(getUsers, "click")  %>%
hide_axis("x") %>% hide_axis("y")

The final result looks like the following screenshot:

A simple interactive line plot

As you can see, the number of users is shown for the USA. This has been achieved simply by clicking on this country. Don't worry if you can't follow all of this code; this section is just designed to show you how quick and easy it is to produce effective and interactive visualizations.

Installing Shiny and running the examples

Shiny can be installed using standard package management functions as described previously (using the GUI or running install.packages("shiny") at the console).

Let's run some of the examples:

> library(shiny)
> runExample("01_hello")

Your web browser should launch and display the following screenshot (note that I clicked on the show below button on the app to better fit the graphic on the page):

Installing Shiny and running the examples

The graph shows the frequency of a set of random numbers drawn from a statistical distribution known as the normal distribution, and the slider allows users to select the size of the draw, from 0 to 1000. You will note that when you move the slider, the graph updates automatically. This is a fundamental feature of Shiny, which makes use of a reactive programming paradigm.

This is a type of programming that uses reactive expressions, which keep track of the values on which they are based that can change (known as reactive values) and update themselves whenever any of their reactive values change. So, in this example, the function that generates the random data and draws the graph is a reactive expression, and the number of random draws that it makes is a reactive value on which the expression depends. So, whenever the number of draws changes, the function re-executes.

Note

You can find more information about this example as well as a comprehensive tutorial for Shiny at shiny.rstudio.com/tutorial/.

Also, note the layout and style of the web page. Shiny is based by default on the bootstrap theme (getbootstrap.com/). However, you are not limited by the styling at all and can build the whole UI using a mix of HTML, CSS, and Shiny code.

Let's look at an interface made with bare-bones HTML and Shiny. Note that in this and all subsequent examples, we're going to assume that you run library(shiny) at the beginning of each session. You don't have to run it before each example except at the beginning of each R session. So, if you have closed R and come back, then run it at the console. If you can't remember, run it again to be sure, as follows:

> runExample("08_html")

And here it is, in all its customizable glory:

Installing Shiny and running the examples

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 shiny.rstudio.com/gallery/.

Summary

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.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Use Shiny's built-in functions to produce engaging user interfaces, and integrate them into your own web pages
  • Implement powerful user-contributed packages to access graphics from the web, make your own dashboards, use interactive maps, and more
  • Extend Shiny using JavaScript and jQuery with minimal coding using this handy, step-by-step guide

Description

R is a highly flexible 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 first Shiny application, and then show you how to integrate Shiny applications with your own web pages. Finally, you will learn how to finely control the inputs and outputs of your application, along with using other packages to build state-of-the-art applications, including dashboards.

Who is this book 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.

What you will learn

  • 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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jan 29, 2016
Length: 194 pages
Edition : 2nd
Language : English
ISBN-13 : 9781782174349
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jan 29, 2016
Length: 194 pages
Edition : 2nd
Language : English
ISBN-13 : 9781782174349
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just NZ$7 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just NZ$7 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total NZ$ 210.97
Mastering RStudio: Develop, Communicate, and Collaborate with R
NZ$80.99
Learning Shiny
NZ$64.99
Web Application Development with R Using Shiny Second Edition
NZ$64.99
Total NZ$ 210.97 Stars icon
Banner background image

Table of Contents

8 Chapters
1. Getting Started with R and Shiny! Chevron down icon Chevron up icon
2. Building Your First Application Chevron down icon Chevron up icon
3. Building Your Own Web Pages with Shiny Chevron down icon Chevron up icon
4. Taking Control of Reactivity, Inputs, and Outputs Chevron down icon Chevron up icon
5. Advanced Applications I – Dashboards Chevron down icon Chevron up icon
6. Advanced Applications II – Using JavaScript Libraries in Shiny Applications Chevron down icon Chevron up icon
7. Sharing Your Creations Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.5
(10 Ratings)
5 star 30%
4 star 30%
3 star 10%
2 star 20%
1 star 10%
Filter icon Filter
Top Reviews

Filter reviews by




Jamshed Bharucha Jun 23, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Very clear
Amazon Verified review Amazon
Oleg Okun Feb 16, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
It may look a bit unusual to consider R as a language of web application development. However, Chris Beeley wrote this book where he disperses any doubts through a number of convincing examples, gradually rising in complexity. Of course, the basic R alone could not be responsible for this exploit: this is where Shiny comes to help (and to shine). Shiny is a web application framework for R allowing to convert data analysis carried out by R packages into interactive web applications without even knowing HTML, CSS, or JavaScript. Thus, R+Shiny can be a viable alternative to the traditional ways to develop web applications.The book is useful for those who already knows R well, but never before tried Shiny and turning to another language is not an option. Numerous code examples guide a reader from creating the first minimal application to designing a complex interactive dashboard.
Amazon Verified review Amazon
Oscar Jun 11, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Excelente libro
Amazon Verified review Amazon
Pethuru Raj Mar 12, 2016
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Due to a series of humongous innovations and improvisations in the information and communication technology (ICT) domain, the amount of multi-structured data getting generated, captured, transformed, stocked, and subjected to a variety of investigations across industry verticals these days is growing exponentially. Considering the tremendous amount of data at hand, the mathematicians, statisticians, and data scientists need highly competent and cognitive tools and techniques to play around with all kinds of data to extract actionable insights to be shared with business executives, decision-makers, analysts, and researchers. Simply stating, the process of transitioning data into information and into knowledge is getting simplified and streamlined through a host of powerful languages, tools, and integrated platforms.The R language is becoming a popular tool for the data-driven world. Google and Facebook are using the R language heavily in their day-to-day operations. Undoubtedly R is also the tool of choice for data scientists at Microsoft, who apply machine learning to data from Bing, Azure, Office, and the Sales, Marketing and Finance departments. R is not only gaining its footprint in corporate corridors but also in academic institutions. Researchers are extensively using R language for their investigations. Data scientists need to gain the competency on data manipulation, data visualization, and machine learning. Implementing these in R is quite easy. The dplyr package in R makes data manipulation easy. Further on, ggplot2 is one of the best data visualization tools around. Similarly for machine learning, the contributions of R language are really immense. There are several books explaining the nitty-gritty of R towards the simplification of machine learning concepts.RStudio is an IDE that makes R easier to use and more productive. RStudio combines a set of productivity tools into a single environment. Shiny makes it incredibly easy to build interactive web applications with R. Shiny has automatic “reactive” binding between inputs and outputs and extensive pre-built widgets. Shiny allows creating interactive web applications using the excellent analytical and graphical capabilities of R.This book is full of practical examples and shows you how to write cutting-edge interactive content for the Web, right from a minimal example all the way to fully styled and extensible applications. This book includes an introduction to Shiny and R and takes you all the way to advanced functions in Shiny as well as using Shiny in conjunction with HTML, CSS, and JavaScript to produce attractive and highly interactive applications quickly and easily. It also includes a detailed look at other packages available for R, which can be used in conjunction with Shiny to produce dashboards, maps, advanced D3 graphics, among many things.
Amazon Verified review Amazon
Don't be afraid Nov 25, 2018
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
good and helpful
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.