-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.R
160 lines (129 loc) · 3.9 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
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
Sys.setenv(TORCH_INSTALL=1)
Sys.setenv(TORCH_HOME=path.expand("libtorch/"))
library(torch)
library(shiny)
library(tidyverse)
library(tidymodels)
# I also needed to install the packages ranger, kknn, brulee
# an interactive ok for pytorch
tidymodels_prefer()
split_data <- readRDS("data/split_data.rds")
# App for selecting machine learning algorithm
ui <- fluidPage(
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "custom.css")
),
titlePanel("Machine learning course"),
br(),
sidebarLayout(
sidebarPanel(width = 6,
verticalLayout(
fluidRow(
column(
width = 6,
selectInput(
inputId = "model_selector",
label = NULL,
choices = list(
"Decision tree" = "decisiontree",
"Random forest" = "randomforest",
"Nearest neighbour" = "nneighbour",
"Neural network" = "neuralnet"
)
)
),
column(
width = 6,
actionButton(inputId = "create_model", label = "Run model")
)
),
br(),
verbatimTextOutput(outputId = "model_info1"),
verbatimTextOutput(outputId = "model_info2")
)#,
#actionButton(inputId = "browser", label = "browser")
),
mainPanel(width = 6,
DT::dataTableOutput("test_original_data"),
br(),
DT::dataTableOutput("test_original_correct"),
br(),
DT::dataTableOutput("test_new_data"),
br(),
DT::dataTableOutput("test_new_correct")
)
)
)
server <- function(input, output) {
observeEvent(input$browser, browser())
# Create model ----
model <- reactive({
switch(input$model_selector,
decisiontree = get_decision_tree(),
randomforest = get_random_forest(),
nneighbour = get_nneighbour(),
neuralnet = get_neuralnet()
)
}) %>%
bindEvent(input$create_model)
# Train the model ----
model_fit <- reactive({
fit(model(), Development ~ ., data=training(split_data))
})
# Predictions ----
training_predictions <- reactive({
model_fit() %>%
predict(new_data=training(split_data)) %>%
bind_cols(training(split_data))
})
test_predictions <- reactive({
model_fit() %>%
predict(new_data=testing(split_data)) %>%
bind_cols(testing(split_data))
})
# Test the model ----
## Training counts ----
training_counts <- reactive({
training_predictions() %>%
group_by(.pred_class, Development) %>%
count() %>%
ungroup()
})
## summary of training counts ----
training_summary <- reactive({
summarise_metrics(training_counts(), training_predictions())
})
## Test counts ----
test_counts <- reactive({
test_predictions() %>%
group_by(.pred_class, Development) %>%
count() %>%
ungroup()
})
## Summary of test counts ----
test_summary <- reactive({
summarise_metrics(test_counts(), test_predictions())
})
# Output tables ----
output$test_original_data <- DT::renderDataTable(
counts_table(training_counts(), title = "Original training data")
)
output$test_original_correct <- DT::renderDataTable({
summary_metrics_table(training_summary(), title = "Summary of training data")
})
output$test_new_data <- DT::renderDataTable(
counts_table(test_counts(), title = "New test data")
)
output$test_new_correct <- DT::renderDataTable(
summary_metrics_table(test_summary(), title = "Summary of test data")
)
# Output text ----
output$model_info1 <- renderPrint({
model() %>% translate()
})
output$model_info2 <- renderPrint({
model_fit()
})
}
# Run the application
shinyApp(ui = ui, server = server)