Reader small image

You're reading from  Data Wrangling with R

Product typeBook
Published inFeb 2023
PublisherPackt
ISBN-139781803235400
Edition1st Edition
Concepts
Right arrow
Author (1)
Gustavo R Santos
Gustavo R Santos
author image
Gustavo R Santos

Gustavo R Santos has worked in the Technology Industry for 13 years, improving processes, and analyzing datasets and creating dashboards. Since 2020, he has been working as a Data Scientist in the retail industry, wrangling, analyzing, visualizing and modeling data with the most modern tools like R, Python and Databricks. Gustavo also gives lectures from time to time at an online school about Data Science concepts. He has a background in Marketing, is certified as Data Scientist by the Data Science Academy Brazil and pursues his specialist MBA in Data Science at the University of São Paulo
Read more about Gustavo R Santos

Right arrow

Build an Application with Shiny in R

We have finally reached the last chapter. We have learned so much during this book, and we have been able to consolidate our new knowledge with the machine learning project we created in the last chapter. Now, it is time to put that model in production, making it available for the final user.

Putting a model into production is nothing more than taking it from the environment where it was created and trained, usually an internal environment in a company or even a data scientist’s local machine, and making it available in an application, serving the purpose it was created for.

In this project, putting the model into production entails creating a web app with the Shiny library, embedding the model into it, and making it available to receive textual input from users. It will predict the probability of that text being spam or not.

This is our plan for this final chapter:

  • Learning the basics of Shiny
  • Creating an application...

Technical requirements

All the code can be found in the book’s GitHub repository: https://github.com/PacktPublishing/Data-Wrangling-with-R/tree/main/Part4/Chapter14.

The libraries we need are as follows:

library(shiny)
library(shinythemes)
library(plotly)
library(tidyverse)
library(randomForest)

Learning the basics of Shiny

Shiny for R was launched in 2012 by Winston Chang as a package to help R developers to create interactive UIs for R scripts. This way, the developers can not only create models, but also share them with other people via an application. Currently, Shiny is on version 1.7.0 and is more robust than ever, expanding its horizons to other programming languages.

As a side note, let’s define an application, since we will use this term a lot in this chapter. An application, or just app, is software created via programming language to solve a problem. It performs a task, such as text editing, web browsing, or translation. A web application is just an app on the internet, such as the many websites where we can do things and interact, such as document creation, photo editing, or design creation.

Among the options available on the market, Shiny stands out for being a complete solution for the developer, offering many gadgets that can be added to applications...

Creating an application

We created a classification model that is able to estimate the probability of any text being classified as spam or not spam, based on the most common spam words and characters from the Spambase dataset. However, if we never add that model to a tool where a person can input text, the likelihood is that the model will become useless. So, the solution is to deploy it, embedding the classifier in a web application. Let’s define our project next.

The project

The project for this last chapter is described in the following bullet points:

  • Problem: Create an interactive application able to deploy a machine learning model to the web.
  • Description: The tool will be able to receive textual input, transform the data to a data frame that will feed the machine learning random forest classifier. The model predicts the probability that a text message is spam or not.
  • Tools: Shiny library and RStudio.

Coding

Now that our project is clear,...

Deploying the application on the web

Working with Shiny and RStudio makes it really easy to deploy an app. Once it is ready for deployment, all we need to do is click the blue Publish Document… button in the top right-hand corner of the source code window in RStudio, right next to the Run App button, as shown in Figure 14.5.

Figure 14.5 – Publish Application option for Shiny apps in RStudio

A pop-up window will appear, asking you to connect RStudio to an account. Hit the Next button. See Figure 14.6.

Figure 14.6 – Publishing options

The next step is to select where you want to publish it. ShinyApps.io has a free version for students. You can choose it, as shown in Figure 14.7.

Figure 14.7 – Options for publishing an application

To connect to the account, you must register on the website and get a token to connect to ShinyApps. Follow the instructions and paste the result in the input...

Summary

We have reached the end of this book, and we closed it by learning about a fantastic tool: the Shiny library for R. Shiny is a tool for building interactive applications, enabling R developers to deploy and show their work online.

In this chapter, we learned the basics of the Shiny library, how to get started, and the most common functions, and we then turned our efforts to build an application that wrapped the random forest model trained in the last chapter, enabling it to serve the purpose that it was built for: helping our digital marketing client to have fewer messages going to the spam box.

The project was based on patterns from an open dataset from the UCI repository; therefore, it will not be applicable to any specific emails or any messages, but more specifically to the patterns learned from that dataset, to solve the problem from our hypothetical client, which was our goal.

We ended the chapter by deploying the app online using the ShinyApps.io free version...

Exercises

  1. What is an application?
  2. What is Shiny?
  3. What are the three main components of Shiny?
  4. What does it mean to deploy an app?

Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Data Wrangling with R
Published in: Feb 2023Publisher: PacktISBN-13: 9781803235400
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Gustavo R Santos

Gustavo R Santos has worked in the Technology Industry for 13 years, improving processes, and analyzing datasets and creating dashboards. Since 2020, he has been working as a Data Scientist in the retail industry, wrangling, analyzing, visualizing and modeling data with the most modern tools like R, Python and Databricks. Gustavo also gives lectures from time to time at an online school about Data Science concepts. He has a background in Marketing, is certified as Data Scientist by the Data Science Academy Brazil and pursues his specialist MBA in Data Science at the University of São Paulo
Read more about Gustavo R Santos