This repository was archived by the owner on Dec 12, 2023. It is now read-only.
generated from course-files/BBT4206-Lab15of15-ConsumePlumberAPIOutput
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLab 15 - plumberAPI.R
More file actions
290 lines (245 loc) · 11 KB
/
Lab 15 - plumberAPI.R
File metadata and controls
290 lines (245 loc) · 11 KB
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
30
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# *****************************************************************************
# Lab 11: Plumber API ----
#
# Course Code: BBT4206
# Course Name: Business Intelligence II
# Semester Duration: 21st August 2023 to 28th November 2023
#
# Lecturer: Allan Omondi
# Contact: aomondi [at] strathmore.edu
#
# Note: The lecture contains both theory and practice. This file forms part of
# the practice. It has required lab work submissions that are graded for
# coursework marks.
#
# License: GNU GPL-3.0-or-later
# See LICENSE file for licensing information.
# *****************************************************************************
# **[OPTIONAL] Initialization: Install and use renv ----
# The R Environment ("renv") package helps you create reproducible environments
# for your R projects. This is helpful when working in teams because it makes
# your R projects more isolated, portable and reproducible.
# Further reading:
# Summary: https://rstudio.github.io/renv/
# More detailed article: https://rstudio.github.io/renv/articles/renv.html
# "renv" It can be installed as follows:
# if (!is.element("renv", installed.packages()[, 1])) {
# install.packages("renv", dependencies = TRUE,
# repos = "https://cloud.r-project.org") # nolint
# }
# require("renv") # nolint
# Once installed, you can then use renv::init() to initialize renv in a new
# project.
# The prompt received after executing renv::init() is as shown below:
# This project already has a lockfile. What would you like to do?
# 1: Restore the project from the lockfile.
# 2: Discard the lockfile and re-initialize the project.
# 3: Activate the project without snapshotting or installing any packages.
# 4: Abort project initialization.
# Select option 1 to restore the project from the lockfile
# renv::init() # nolint
# This will set up a project library, containing all the packages you are
# currently using. The packages (and all the metadata needed to reinstall
# them) are recorded into a lockfile, renv.lock, and a .Rprofile ensures that
# the library is used every time you open the project.
# Consider a library as the location where packages are stored.
# Execute the following command to list all the libraries available in your
# computer:
.libPaths()
# One of the libraries should be a folder inside the project if you are using
# renv
# Then execute the following command to see which packages are available in
# each library:
lapply(.libPaths(), list.files)
# This can also be configured using the RStudio GUI when you click the project
# file, e.g., "BBT4206-R.Rproj" in the case of this project. Then
# navigate to the "Environments" tab and select "Use renv with this project".
# As you continue to work on your project, you can install and upgrade
# packages, using either:
# install.packages() and update.packages or
# renv::install() and renv::update()
# You can also clean up a project by removing unused packages using the
# following command: renv::clean()
# After you have confirmed that your code works as expected, use
# renv::snapshot(), AT THE END, to record the packages and their
# sources in the lockfile.
# Later, if you need to share your code with someone else or run your code on
# a new machine, your collaborator (or you) can call renv::restore() to
# reinstall the specific package versions recorded in the lockfile.
# [OPTIONAL]
# Execute the following code to reinstall the specific package versions
# recorded in the lockfile (restart R after executing the command):
# renv::restore() # nolint
# [OPTIONAL]
# If you get several errors setting up renv and you prefer not to use it, then
# you can deactivate it using the following command (restart R after executing
# the command):
# renv::deactivate() # nolint
# If renv::restore() did not install the "languageserver" package (required to
# use R for VS Code), then it can be installed manually as follows (restart R
# after executing the command):
if (require("languageserver")) {
require("languageserver")
} else {
install.packages("languageserver", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}
# Introduction ----
# We can create an API to access the model from outside R using a package
# called Plumber.
# STEP 1. Install and Load the Required Packages ----
## plumber ----
if (require("plumber")) {
require("plumber")
} else {
install.packages("plumber", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}
## caret ----
if (require("caret")) {
require("caret")
} else {
install.packages("caret", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}
# Create a REST API using Plumber ----
# REST API stands for Representational State Transfer Application Programming
# Interface. It is an architectural style and a set of guidelines for building
# web services that provide interoperability between different systems on the
# internet. RESTful APIs are widely used for creating and consuming web
# services.
## Principles of REST API ----
### 1. Stateless ----
# The server does not store any client state between requests. Each request from
# the client contains all the necessary information for the server to understand
# and process the request.
### 2. Client-Server Architecture ----
# The client and server are separate entities that communicate over the
# Internet. The client sends requests to the server, and the server processes
# those requests and sends back responses.
### 3. Uniform Interface ----
# REST APIs use a uniform and consistent set of interfaces and protocols. The
# most common interfaces are based on the HTTP protocol, such as GET (retrieve
# a resource), POST (create a new resource), PUT (update a resource), DELETE
# (remove a resource), etc.
### 4. Resource-Oriented ----
# REST APIs are based on the concept of resources, which are identified by
# unique URIs (Uniform Resource Identifiers). Clients interact with these
# resources by sending requests to their corresponding URIs.
### 5. Representation of Resources ----
# Resources in a REST API can be represented in various formats, such as JSON
# (JavaScript Object Notation), XML (eXtensible Markup Language), YAML (YAML
# Ain't Markup Language) or plain text. The server sends the representation of
# a resource in the response to the client.
# REST APIs are widely used for building web services that can be consumed by
# various client applications, such as web browsers, mobile apps, or other
# servers. They provide a scalable and flexible approach to designing APIs that
# can evolve over time. Developers can use RESTful principles to create APIs
# that are easy to understand, use, and integrate into different systems.
# When working with a REST API, clients typically send HTTP requests to
# specific endpoints (URLs) provided by the server, and the server responds
# with the requested data or performs the requested actions. The communication
# between client and server is based on the HTTP protocol, making REST APIs
# widely supported and accessible across different platforms and programming
# languages.
# In summary, a REST API is a set of rules and conventions for building web
# services that follow the principles of REST. It provides a standardized and
# scalable way for systems to communicate and exchange data over the internet.
# This requires the "plumber" package that was installed and loaded earlier in
# STEP 1. The commenting below makes R recognize the code as the definition of
# an API, i.e., #* comments.
loaded_Satellite_model_lda <- readRDS("./models/saved_satellite_model_lda.rds")
#* @apiTitle Satellite Prediction Model API
#* @apiDescription Used to predict whether a patient has Satellite or not.
#* @param arg_x.1 Pixel band 1
#* @param arg_x.2 Pixel band 2
#* @param arg_x.3 Pixel band 3
#* @param arg_x.4 Pixel band 4
#* @param arg_x.5 Pixel band 5
#* @param arg_x.6 Pixel band 6
#* @param arg_x.7 Pixel band 7
#* @param arg_x.8 Pixel band 8
#* @param arg_x.9 Pixel band 9
#* @param arg_x.10 Pixel band 10
#* @param arg_x.11 Pixel band 11
#* @param arg_x.12 Pixel band 12
#* @param arg_x.13 Pixel band 13
#* @param arg_x.14 Pixel band 14
#* @param arg_x.15 Pixel band 15
#* @param arg_x.16 Pixel band 16
#* @param arg_x.17 Pixel band 17
#* @param arg_x.18 Pixel band 18
#* @param arg_x.19 Pixel band 19
#* @param arg_x.20 Pixel band 20
#* @param arg_x.21 Pixel band 21
#* @param arg_x.22 Pixel band 22
#* @param arg_x.23 Pixel band 23
#* @param arg_x.24 Pixel band 24
#* @param arg_x.25 Pixel band 25
#* @param arg_x.26 Pixel band 26
#* @param arg_x.27 Pixel band 27
#* @param arg_x.28 Pixel band 28
#* @param arg_x.29 Pixel band 29
#* @param arg_x.30 Pixel band 30
#* @param arg_x.31 Pixel band 31
#* @param arg_x.32 Pixel band 32
#* @param arg_x.33 Pixel band 33
#* @param arg_x.34 Pixel band 34
#* @param arg_x.35 Pixel band 35
#* @param arg_x.36 Pixel band 36
#* @get /Satellite
predict_Satellite <-
function(arg_x.1, arg_x.2, arg_x.3 , arg_x.4, arg_x.5, arg_x.6 , arg_x.7 , arg_x.8 , arg_x.9, arg_x.10 ,
arg_x.11 , arg_x.12, arg_x.13, arg_x.14, arg_x.15, arg_x.16, arg_x.17, arg_x.18, arg_x.19, arg_x.20,
arg_x.21 , arg_x.22 , arg_x.23 , arg_x.24, arg_x.25, arg_x.26, arg_x.27,arg_x.28,arg_x.29,arg_x.30,
arg_x.31,arg_x.32,arg_x.33,arg_x.34,arg_x.35,arg_x.36) {
# Create a data frame using the arguments
# Create a data frame using the arguments
to_be_predicted <-
data.frame(
x.1 = as.numeric(arg_x.1),
x.2 = as.numeric(arg_x.2),
x.3 = as.numeric(arg_x.3),
x.4 = as.numeric(arg_x.4),
x.5 = as.numeric(arg_x.5),
x.6 = as.numeric(arg_x.6),
x.7 = as.numeric(arg_x.7),
x.8 = as.numeric(arg_x.8),
x.9 = as.numeric(arg_x.9),
x.10 = as.numeric(arg_x.10),
x.11 = as.numeric(arg_x.11),
x.12 = as.numeric(arg_x.12),
x.13 = as.numeric(arg_x.13),
x.14 = as.numeric(arg_x.14),
x.15 = as.numeric(arg_x.15),
x.16 = as.numeric(arg_x.16),
x.17 = as.numeric(arg_x.17),
x.18 = as.numeric(arg_x.18),
x.19 = as.numeric(arg_x.19),
x.20 = as.numeric(arg_x.20),
x.21 = as.numeric(arg_x.21),
x.22 = as.numeric(arg_x.22),
x.23 = as.numeric(arg_x.23),
x.24 = as.numeric(arg_x.24),
x.25 = as.numeric(arg_x.25),
x.26 = as.numeric(arg_x.26),
x.27 = as.numeric(arg_x.27),
x.28 = as.numeric(arg_x.28),
x.29 = as.numeric(arg_x.29),
x.30 = as.numeric(arg_x.30),
x.31 = as.numeric(arg_x.31),
x.32 = as.numeric(arg_x.32),
x.33 = as.numeric(arg_x.33),
x.34 = as.numeric(arg_x.34),
x.35 = as.numeric(arg_x.35),
x.36 = as.numeric(arg_x.36)
)
# Make a prediction based on the data frame
predict(loaded_Satellite_model_lda, to_be_predicted)
}
# [OPTIONAL] **Deinitialization: Create a snapshot of the R environment ----
# Lastly, as a follow-up to the initialization step, record the packages
# installed and their sources in the lockfile so that other team-members can
# use renv::restore() to re-install the same package version in their local
# machine during their initialization step.
# renv::snapshot() # nolint