In today’s fast-moving world, we often need to use complex tools like machine learning to make sense of data and solve problems. However, these tools can be pretty tricky to use, especially if you’re not a tech expert. That’s where Shiny comes in! Shiny is an open packages from RStudio, which provides a web application framework to create interactive web application called Shiny apps. Shiny is like a magic wand for turning these powerful but confusing tools into easy-to-use web apps. While Shiny is often used to create interactive dashboard visualizations, in this article, we’ll explore a slightly different but equally exciting aspect of Shiny: deploying machine learning models. We’ll see how Shiny makes it super simple to deploy your machine learning creations, even if you’re not a tech whiz. So, let’s dive into the world of Shiny and discover how it can bring the power of machine learning to your fingertips in our fast-paced digital age.
In broad strokes, here are the steps we’ll be exploring in this article:
In this article, we will explore the employee turnover prediction dashboard, which you can access at https://algoritmadatascience.shinyapps.io/Dashboard-Employee-Turnover/, and it will provide an overview like this:
Let’s get started!
Before we dive into the technical stuff, let’s get familiar with Shiny and its main parts. A basic Shiny app is like building with three important R scripts:
global.R: Think of this as the
backstage manager. It gets everything ready for your app’s performance.
Here, you gather the tools you’ll need (like libraries), bring in the
props (your data), and make sure everything is set up just
right.
ui.R: This is where you design the
look of your app. Imagine it as the stage where your audience sees
everything. You decide how it should appear, what buttons and sliders
the audience can use, and how it all fits together.
server.R: Now, this is where the
real action happens. Think of it as the engine of your app. In the
server script, you make things happen based on what the audience (your
app’s users) does. You listen to their requests, do some magic behind
the scenes (like processing data or running models), and then show them
the results.
These three scripts work together, like a team putting on a play. global.R sets up the backstage, ui.R designs the stage, and server.R directs the performance. Together, they create a web app that can do cool stuff, like showing data or even deploying machine learning models. If you’re feeling uncertain about where to begin your journey into learning Shiny, a valuable resource is the book “Mastering Shiny” by O’Reilly Media, which provides comprehensive insights. You can access it online through this link: Mastering Shiny.
First, of course, we need to have a machine learning model that we
will deploy in Shiny. To make the process more efficient and secure, it
is advisable to store the machine learning model in RDS
format. This ensures that when it is deployed in Shiny, it does not
consume excessive memory or time. Here is an example of how to save a
random forest model in RDS format using the saveRDS()
function and store it with the name randomforest.RDS.
set.seed(417)
control <- trainControl(method = "repeatedcv", number = 10, repeats = 10)
# build model
model <- train(x = data_train[,-27],y = data_train[,27],method = "rf", trControl = control)
# save model
saveRDS(model, "randomforest.RDS")
After successfully running your prediction dashboard on your local computer, the final step is to deploy it to Shinyapps on the web, allowing easy access for many people. You can find detailed steps on how to deploy Shinyapps to the web at this official shiny link.
Don’t panic if you encounter any failures during deployment, as this
is a common occurrence. You can troubleshoot the errors by running the
code rsconnect::showLogs() in the RStudio Console and
resolving the identified issues. Typically, these errors are related to
missing package dependencies. You can also refer to the Posit
documentation for troubleshooting deployment errors.
Here is the core process when we want to deploy a machine learning model in a Shiny app. In fact, you can explore many more possibilities for deploying machine learning models according to the design you desire because Shiny is highly flexible. You can view the following Shiny gallery to explore Shiny designs more extensively. There, you’ll even find example code to help you get started. Once you have a grasp of how Shiny works, creating useful and visually appealing web applications using Shiny will become very straightforward. Unlock the limitless potential of Shiny and let your creativity run wild in deploying machine learning models that not only work efficiently but also look stunning. Learning will never stop, so keep exploring the exciting world of data science!