-
Notifications
You must be signed in to change notification settings - Fork 0
/
Iris_Shiny.R
103 lines (86 loc) · 2.41 KB
/
Iris_Shiny.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
# Deploying to Production
# Load shiny
library(shiny)
# Load decision tree package
library(tree)
# Set working directory
setwd("/Users/randallhall/Desktop")
# Load training data
load("Train.RData")
# Load tree model
load("Tree.RData")
# Load color brewer library
library(RColorBrewer)
# Create a color palette
palette <- brewer.pal(3, "Set2")
# Create user interface code
ui <- fluidPage(
titlePanel("Iris Species Predictor"),
sidebarLayout(
sidebarPanel(
sliderInput(
inputId = "petal.length",
label = "Petal Length (cm)",
min = 1,
max = 7,
value = 4),
sliderInput(
inputId = "petal.width",
label = "Petal Width (cm)",
min = 0.0,
max = 2.5,
step = 0.5,
value = 1.5)),
mainPanel(
textOutput(
outputId = "text"),
plotOutput(
outputId = "plot"))))
# Create server code
server <- function(input, output) {
output$text = renderText({
# Create predictors
predictors <- data.frame(
Petal.Length = input$petal.length,
Petal.Width = input$petal.width,
Sepal.Length = 0,
Sepal.Width = 0)
# Make prediction
prediction = predict(
object = model,
newdata = predictors,
type = "class")
# Create prediction text
paste(
"The predicted species is ",
as.character(prediction))
})
output$plot = renderPlot({
# Create a scatterplot colored by species
plot(
x = iris$Petal.Length,
y = iris$Petal.Width,
pch = 19,
col = palette[as.numeric(iris$Species)],
main = "Iris Petal Length vs. Width",
xlab = "Petal Length (cm)",
ylab = "Petal Width (cm)")
# Plot the decision boundaries
partition.tree(
model,
label = "Species",
add = TRUE)
# Draw predictor on plot
points(
x = input$petal.length,
y = input$petal.width,
col = "red",
pch = 4,
cex = 2,
lwd = 2)
})
}
# Create a shiny app
shinyApp(
ui = ui,
server = server)