Web Apps in R: Building Data-Driven Web Application in R | Shiny Tutorial Ep 3
Key Takeaways
This video tutorial demonstrates building a simple data-driven web application in R using the Shiny web framework and the random forest algorithm for prediction. The application utilizes various tools such as Shiny, R, RStudio, GitHub, and randomForest to create a web app that makes predictions based on input parameters like outlook, temperature, humidity, and windy.
Full Transcript
welcome back to the data professor YouTube channel if you're new here my name is shannon antacid ahmad and i'm an associate professor of bioinformatics on this youtube channel we cover about data science concepts and practical tutorials so if you're into this kind of content please consider subscribing okay so this is the third episode of the web apps in our series so today we're going to build a play golf web application so probably you're wondering what is a play golf application so the play golf web application that we're going to build today it's going to be based on the weather data set provided by the weeka data mining software so let's have a look here so the data set is a relatively small data set where it has a total of five variables so four variables are the outlook temperature humidity and the win and the class label would be to play or not play golf which is a function of the weather and the condition right like whether it is sunny whether the temperature is high whether the humidity is high or low or medium and whether there is win or not true or false and then the final decision is to either play or not play COFF so before we dive into the code let's have a look at how does the web application looks like okay so the web app looks like this it's a very simple application so the name of the web app is play golf and so here there is the input parameters comprising of the four variables that I have mentioned the first one is the outlook and so the user can select one of three outlook whether it is sunny weather there is overcast of cloud and also whether there is rain or not and the second variable is the temperature and so this is a slider input so the user can slide the input value and then the humidity is also a slider input and then windy is either a yes or a No which is a drop-down menu and then when you're ready to make a prediction you just click on the submit button okay and here you see a prediction is being made and the prediction says yes and then we also see the underlying probability in that the know has a 27 percent probability and the yes has a 73 percent probability and so you could play around with this right if for example if it is sunny and the humidity is very high and the temperature is very high and there is when should you play golf no I mean really if the temperature is high it's very humid and it's very windy and sunny right probably not okay well if it's what if the humidity is low temperature is quite cool and it's sunny and it's not so windy would you play golf right yes right so there's a 87 probability for yes and a 13 percent probability for no okay so this is the web app we're going to build today so let's go ahead and stop the web map for a moment okay so what you want to do now is go to the data professor github and then you want to click on the code directory and followed by clicking on the shiny directory and finally clicking on the 0:03 play golf directory and then click on the app dot r file okay so what you want to do is right click on the raw link and then save the file so I'm gonna save it into the weather folder save it okay it's right here click on it make sure that the app works okay it works prediction has been made okay cool so it's just clear up this by pressing on the ctrl + L button ok so let's have a look under the hood what does the app dot our file looks like so the first couple of lines which has import libraries used by this app dot our file so this comprises of the shiny packaged shiny themes packaged the data table library the r curl library and the random forest library ok so next line of code would be to create a data object called weather by reading the CSV which is load it from the data professor github in the data folder and the file is called weather dot weeka dot CSV ok let's have a look at the data set what does it look like I'll click on the line weather and then I hit on the ctrl + Enter button and then let's type in weather ok so this is what me see let's go with head and then weather ok and so we see that there are five columns outlook temperature humidity windy and play right and let's have a look at the data type of the data set we see that the outlook has three factor levels play has two factor levels so these are categorical label no yes and the outlook is overcast rainy and sunny windy is a true-and-false humidity and temperature are integers and so a random forest model will be created by using the four variables comprising of outlook temperature humidity windy as the input variable and the play variable here will be used as the output variable or the variable that we want to predict ok and in the the data equals to weather which is the weather data object here and we're gonna use number of three to be five hundred and because there are four input variables we're just gonna use em try a four okay so let's try building a model and the model has been built and let's apply the model for prediction shall we I mean just to test that the code is working properly so let's try applying the model on the input file that I have previously mentioned about and so we're gonna run this line and putting the data into the variable called test and then we're gonna assign the factor you can see if we don't do it and it'll provide an error so before we run this line of code let's just try to make a prediction model and we should be able to see a error coming up okay so we got this error error in predict random forest type of predictor in new data do not match so what we notice is that if we type in the STR and we type in weather and then we notice that the outlook has factors with three level but if it Str and then the test variable notice that the outlet has a factor of only one level and this is because the input data has only one line of data which is essentially one row of data and and so the outlook is only sunny so the prediction being made has only one role and but in reality it should have three levels of the feather so we're gonna define that by telling the code that there are three possible factors there are overcast rainy and sunny so let's run that line of code and then run the prediction again okay and it works and then print outputs and here you go we got the prediction which is exactly what is going to be displayed on the web application let me show you right we make the prediction and it's shown here so this table you see here it's shown right here so the model works and let's go to the other lines of code so the next one would be the user interface right as I have mentioned in previous video the user interface represents the first component of the shiney web app and this is followed by the second component which is the server and then this is followed by the third component which is the fusion of the user interface and the server component using the shiny app function okay so let's talk about the UI so this UI object makes use of the fluid page function and we're gonna use the theme equals to the shiny theme United and so the United theme will give the buttons a red color so if we change it to cerulean then we're going to have the cerulean color theme which is a bit blue okay so please refer to the first video of the web apps in our in order to see the selection of web templates that you can choose from okay so just run the app again and I'm going to put the app just about right right here so the header panel is play golf and so this is right here play golf so if you want to change the name feel free to do so and then the next one is the sidebar panel so the sidebar panel except the input parameters it's located to the left and there will be a total of four input and so the first one is select input which is a drop-down menu and if you click on it you get three selection sunny overcast rainy and when you hover on the drop-down menu on sunny it will secretly under the hood select the sunny object and if you select on the overcast also it will under the hood be equivalent to the overcast object and if you select on the rainy it will be equivalent to the rainy object and the default is to select rainy right here we'll select rainy ask the default what if you change it to sunny and let's change the value to be a high value let's say like 85 and humidity to be 95 and it's windy that's true reload the app again right so high temperature high humidity windy sunny don't play golf okay so here you can change the default value to your liking okay so we have mentioned about the three data objects for here sunny overcast and rainy so keep that in mind we're gonna make use of that in the server function and note that when we will refer to it later on in the server function it will be referred to as input dollar sign outlook input dollar sign temperature input dollar sign humidity and input dollar sign windy here why don't we just scroll down and have look here input dollar sign outlook input dollar sign temperature input dollar sign humidity input dollar sign windy okay and then the smooth back notice the spelling here using the small letter not the capital one so the capital one here are the label so it's exactly what we're gonna see in the web application outlook with the : it's right here on the label okay actually you don't have to type in label if you don't want to we could just you know delete it and it will give the same results it's just implied okay but if you want to add the label argument you could feel free to do so right but if you do it for London well might as well do it for all so that would actually make the coat looks a bit more easier to read right let me see here that okay this is the object name Outlook and the label is Outlook with a capital o reload the app here you go right it works as usual so here this outlook here it's an outlook object and this temperature here is the temperature object and this action button here is the submit button so this submit button is added in order to overcome the reactive function so we're just at the familiar submit button so that users can initiate the prediction process when they feel ready to do so instead of having the web app being reactive and making the prediction spontaneously upon sliding up and down of the input values because when it's reactive if you move it by one notch and you let go of the mouse it'll make a prediction but for this one with the submit button no prediction will be made until you actually click on the submit button so actually this might be a good thing on the server side because the server will work a bit less if the prediction being made is made only once versus if it is in the reactive mode if you slide the input value and you just change your mind later on then prediction will be made at each point of the changing of the input value of the slider here or even the drop-down menu right but we do it once with the use of the submit button okay and that's it for this left sidebar panel and then the main panel here will display the result from the output generated by the server function so we're going to talk about that in just a moment so why don't we note that the output being generated by the server component will be called contents and table data okay okay so let's stop on to the server function which is the second component so in this data set input variable it will comprise of the first component is it will create a data frame which will accept four input values from the web ocation compressing of the outlook temperature humidity windy which is right here outlook temperature humidity windy and then it will combine it with the play variable which is the fifth column of the original data set and then it will transpose the data set it will rotate it it will transpose it and then write a input dot CSV file it will read the input dot CSV file back in into the test variable and then it will apply the factor function in order to tell that the outlook has three levels and finally a prediction will be made using the model generated earlier by means of the random forest and apply the prediction model to predict the input values from the user and once the prediction is made it will be sent from here into the output dollar sign table data as the function data set input right here and then it's going to render the table as we will see in the web application so the table that is being rendered will be right here which comprises of three columns the prediction the no and the yes probability okay and this status output text box it's just essentially this box right here so if we load the app for the first time it will just say server is ready for calculation and if we click on the submit button the texts will change to calculation complete and it will be followed by the prediction results table okay so note that there are two output being generated right here output dollar sign contents output dollar sign table data so these two output will be sent to the UI component right here table data and contents table data will be displayed at the table using the table output function and the status of the prediction whether it is ready for prediction or prediction has been made would be displayed by the verbatim text output function okay and this is just a label of the status output text box shown right here that we're going to have the h3 font size I mean if you want 62h to make it a bit bigger then you will notice that the we'll become bigger Hey I'm just gonna make it back to h3 okay so that's all right and then the the last component shiny app function which is fuse the UI and the server together okay and you have all of this in 121 lines of code and so nothing fancy here just a simple web application that you can create using the shiny language thank you for watching please like subscribe and share and I'll see you in the next one but in the meantime please check out these videos
Original Description
In this R Shiny tutorial video, I will guide you through the process of building a simple data-driven web application in R using the Shiny web framework. This ML-based web app that we are building will be data-driven in the sense that it makes use of the random forest algorithm for predicting whether to play or not play golf as a function of the input weather parameters (weather outlook, temperature, humidity and windy).
🌟 Buy me a coffee: https://www.buymeacoffee.com/dataprofessor
📎CODE: https://github.com/dataprofessor/code/tree/master/shiny/003-play-golf
⭕ TIMESTAMP
0:00 Introduction
0:20 What web app we are building today?
0:44 Weather dataset from WEKA
1:19 Overview of the web app that we are building
3:06 Go to Data Professor GitHub
3:34 Save the app.R from code/shiny/003-play-golf
3:56 Walkthrough of the app.R code
4:04 Importing libraries
4:22 Load the Weather dataset
4:36 Let's look at the Weather dataset
5:18 Building the Random Forest model
5:52 Apply trained model to make predictions
6:20 Prediction does not work!
6:48 Ordinal variable (outlook) is a Factor w/ 3 levels
7:04 Let's solve the error by defining the Factor
7:16 Prediction works!
7:39 3 components of a Shiny web application
8:03 1. User interface (UI)
8:41 headerPanel()
8:49 sidebarPanel()
9:00 selectInput() and sliderInput()
10:05 Keep in mind the input variable names
10:13 input$outlook, input$temperature, input$humidity, input$windy
10:46 label argument of input
11:36 actionButton()
11:49 Advantages of Submit actionButton
12:44 mainPanel()
13:05 2. server function
13:09 datasetInput()
13:15 Create a data frame using 4 input parameters from the web app
13:39 Writing the input data to input CSV file
13:47 Apply Factor function to manually assign 3 levels (to avoid potential error)
13:52 Apply the trained Random Forest model to make a prediction on test data
14:08 Transfer prediction results to be displayed in the mainPanel() of UI
14:49 2 outputs are generated from the server
⭕ Playlist
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Data Professor · Data Professor · 30 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
▶
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
How a Biologist became a Data Scientist
Data Professor
WEKA Tutorial #1.1 - How to Build a Data Mining Model from Scratch
Data Professor
WEKA Tutorial #1.2 - How to Build a Data Mining Model from Scratch
Data Professor
WEKA Tutorial #1.3 - How to Build a Data Mining Model from Scratch
Data Professor
Computational Drug Discovery: Machine Learning for Making Sense of Big Data in Drug Discovery
Data Professor
Quotes #1 on Big Data and Data Science
Data Professor
Quotes #2 on Big Data and Data Science
Data Professor
Quotes #3 on Big Data and Data Science
Data Professor
Quotes #4 on Big Data and Data Science
Data Professor
Quotes #5 on Big Data and Data Science
Data Professor
Data Science 101: Starting a Data Science / Data Mining Project
Data Professor
Data Science 101: CRISP-DM - Data Mining / Data Science in 6 Steps
Data Professor
R Programming 101: How to Define Variables
Data Professor
R Programming 101: Read and Write CSV files
Data Professor
Data Science 101: Basic Command-Line for Data Science
Data Professor
Strategies for Learning Data Science in 2020 (Data Science 101)
Data Professor
Building your Data Science Portfolio with GitHub (Data Science 101)
Data Professor
R Programming 101: Setting up R programming environment (R, RStudio and RStudio.cloud)
Data Professor
Exploratory Data Analysis in R: Towards Data Understanding
Data Professor
Exploratory Data Analysis in R: Quick Dive into Data Visualization
Data Professor
Machine Learning in R: Building a Classification Model
Data Professor
Machine Learning in R: Repurpose Machine Learning Code for New Data
Data Professor
Data Science 101: Deploying your Machine Learning Model
Data Professor
Machine Learning in R: Deploy Machine Learning Model using RDS
Data Professor
Data Pre-processing in R: Handling Missing Data
Data Professor
Machine Learning in R: Speed up Model Building with Parallel Computing
Data Professor
Data Science 101: Overview of Machine Learning Model Building Process
Data Professor
Web Apps in R: Building your First Web Application in R | Shiny Tutorial Ep 1
Data Professor
Web Apps in R: Build Interactive Histogram Web Application in R | Shiny Tutorial Ep 2
Data Professor
Web Apps in R: Building Data-Driven Web Application in R | Shiny Tutorial Ep 3
Data Professor
Web Apps in R: Building the Machine Learning Web Application in R | Shiny Tutorial Ep 4
Data Professor
Web Apps in R: Build BMI Calculator web application in R for health monitoring | Shiny Tutorial Ep 5
Data Professor
Machine Learning in R: Building a Linear Regression Model
Data Professor
What programming language to learn for Data Science? R versus Python
Data Professor
How to Become a Data Scientist (Learning Path and Skill Sets Needed)
Data Professor
Using Python in R
Data Professor
Interpretable Machine Learning Models
Data Professor
Making Scatter Plots in R [Data Visualisation in R series]
Data Professor
Machine Learning in Python: Building a Classification Model
Data Professor
Compare Machine Learning Classifiers in Python
Data Professor
Hyperparameter Tuning of Machine Learning Model in Python
Data Professor
Practical Introduction to Google Colab for Data Science
Data Professor
File Handling in Google Colab for Data Science
Data Professor
Pandas for Data Science: Create and Combine DataFrames / Rename Columns
Data Professor
Machine Learning in Python: Building a Linear Regression Model
Data Professor
Machine Learning in Python: Principal Component Analysis (PCA) for Handling High-Dimensional Data
Data Professor
How to Plot an ROC Curve in Python | Machine Learning in Python
Data Professor
Installing conda on Google Colab for Data Science
Data Professor
Use native R on Google Colab for Data Science
Data Professor
How to Save and Download files from Google Colab
Data Professor
Easy Web Scraping in Python using Pandas for Data Science
Data Professor
Data Science for Computational Drug Discovery using Python (Part 1)
Data Professor
Pandas Profiling for Data Science (Quick and Easy Exploratory Data Analysis)
Data Professor
Exploratory Data Analysis in Python using pandas
Data Professor
Quick tour of PyCaret (a low-code machine learning library in Python)
Data Professor
How to Upload Files to Google Colab
Data Professor
How to Install and Use Pandas Profiling on Google Colab
Data Professor
How to Adjust the Style of Pandas DataFrame
Data Professor
How to use Bamboolib for Data Wrangling in Data Science
Data Professor
How to use Pandas Profiling on Kaggle
Data Professor
More on: ML Pipelines
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Bloom Filters, Explained Properly
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Medium · Python
Chapters (35)
Introduction
0:20
What web app we are building today?
0:44
Weather dataset from WEKA
1:19
Overview of the web app that we are building
3:06
Go to Data Professor GitHub
3:34
Save the app.R from code/shiny/003-play-golf
3:56
Walkthrough of the app.R code
4:04
Importing libraries
4:22
Load the Weather dataset
4:36
Let's look at the Weather dataset
5:18
Building the Random Forest model
5:52
Apply trained model to make predictions
6:20
Prediction does not work!
6:48
Ordinal variable (outlook) is a Factor w/ 3 levels
7:04
Let's solve the error by defining the Factor
7:16
Prediction works!
7:39
3 components of a Shiny web application
8:03
1. User interface (UI)
8:41
headerPanel()
8:49
sidebarPanel()
9:00
selectInput() and sliderInput()
10:05
Keep in mind the input variable names
10:13
input$outlook, input$temperature, input$humidity, input$windy
10:46
label argument of input
11:36
actionButton()
11:49
Advantages of Submit actionButton
12:44
mainPanel()
13:05
2. server function
13:09
datasetInput()
13:15
Create a data frame using 4 input parameters from the web app
13:39
Writing the input data to input CSV file
13:47
Apply Factor function to manually assign 3 levels (to avoid potential error)
13:52
Apply the trained Random Forest model to make a prediction on test data
14:08
Transfer prediction results to be displayed in the mainPanel() of UI
14:49
2 outputs are generated from the server
🎓
Tutor Explanation
DeepCamp AI