-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.R
108 lines (94 loc) · 2.62 KB
/
app.R
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
library(shiny)
# Configure the log object
log <- log4r::logger()
api_url <- "http://127.0.0.1:8000/predict"
ui <- fluidPage(
titlePanel("Penguin Mass Predictor"),
# Model input values
sidebarLayout(
sidebarPanel(
sliderInput(
"bill_length",
"Bill Length (mm)",
min = 30,
max = 60,
value = 45,
step = 0.1
),
selectInput(
"sex",
"Sex",
c("Male", "Female")
),
selectInput(
"species",
"Species",
c("Adelie", "Chinstrap", "Gentoo")
),
# Get model predictions
actionButton(
"predict",
"Predict"
)
),
mainPanel(
h2("Penguin Parameters"),
verbatimTextOutput("vals"),
h2("Predicted Penguin Mass (g)"),
textOutput("pred")
)
)
)
server <- function(input, output) {
# Log app start
log4r::info(log, "App Started")
# Input params
vals <- reactive(
list(
bill_length_mm = input$bill_length,
species_Chinstrap = input$species == "Chinstrap",
species_Gentoo = input$species == "Gentoo",
sex_male = input$sex == "Male"
)
)
# Fetch prediction from API
pred <- eventReactive(
input$predict,
{
input_json_array <- jsonlite::toJSON(
list( # <- creates an array of objects
isolate(vals())
),
auto_unbox=TRUE,
pretty = FALSE
)
## log4r::info breaks when called from within eventReactive code block...
# log4r::info(paste0("Send prediction inputs (", input_json_array, ") to api (", api_url, ")"))
## Warning: Error in $: $ operator is invalid for atomic vectors
## 145: log4r::info
## ... have to use message() instead:
message(paste0("Send prediction inputs (", input_json_array, ") to api (", api_url, ")"))
tryCatch({
res <- httr2::request(api_url) |>
# httr2::req_body_json(val()) |> # <- json auto serialization was not working...
httr2::req_body_raw(input_json_array) |> # <- ... so we manualy serialized to json string
httr2::req_perform()
message("Return response")
# Log error returned from server
if (httr2::resp_is_error(res)) {
log4r::error(log, paste("HTTP Error"))
}
}, error = function(err) {
# Log error if no response from server
log4r::error(log, paste(err))
})
httr2::resp_body_json(res)
},
ignoreInit = TRUE
)
# Render to UI
output$pred <- renderText(pred()$predict[[1]])
output$vals <- renderPrint(vals())
}
# Run the application
shinyApp(ui = ui, server = server)