forked from dataprofessor/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc6ff4c
commit 03bf870
Showing
1 changed file
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
############################################ | ||
# Data Professor # | ||
# http://youtube.com/dataprofessor # | ||
# http://github.com/dataprofessor # | ||
# http://facebook.com/dataprofessor # | ||
# https://www.instagram.com/data.professor # | ||
############################################ | ||
|
||
# Import libraries | ||
library(shiny) | ||
library(data.table) | ||
library(randomForest) | ||
|
||
# Read in the RF model | ||
model <- readRDS("model.rds") | ||
|
||
|
||
#################################### | ||
# User interface # | ||
#################################### | ||
|
||
ui <- pageWithSidebar( | ||
|
||
# Page header | ||
headerPanel('Iris Predictor'), | ||
|
||
# Input values | ||
sidebarPanel( | ||
HTML("<h3>Input parameters</h4>"), | ||
numericInput("Sepal.Length", label = "Sepal Length", value = 5.0), | ||
numericInput("Sepal.Width", label = "Sepal Width", value = 3.6), | ||
numericInput("Petal.Length", label = "Petal Length", value = 1.4), | ||
numericInput("Petal.Width", label = "Petal Width", value = 0.2), | ||
|
||
actionButton("submitbutton", "Submit", class = "btn btn-primary") | ||
), | ||
|
||
mainPanel( | ||
tags$label(h3('Status/Output')), # Status/Output Text Box | ||
verbatimTextOutput('contents'), | ||
tableOutput('tabledata') # Prediction results table | ||
|
||
) | ||
) | ||
|
||
#################################### | ||
# Server # | ||
#################################### | ||
|
||
server<- function(input, output, session) { | ||
|
||
# Input Data | ||
datasetInput <- reactive({ | ||
|
||
df <- data.frame( | ||
Name = c("Sepal Length", | ||
"Sepal Width", | ||
"Petal Length", | ||
"Petal Width"), | ||
Value = as.character(c(input$Sepal.Length, | ||
input$Sepal.Width, | ||
input$Petal.Length, | ||
input$Petal.Width)), | ||
stringsAsFactors = FALSE) | ||
|
||
Species <- 0 | ||
df <- rbind(df, Species) | ||
input <- transpose(df) | ||
write.table(input,"input.csv", sep=",", quote = FALSE, row.names = FALSE, col.names = FALSE) | ||
|
||
test <- read.csv(paste("input", ".csv", sep=""), header = TRUE) | ||
|
||
Output <- data.frame(Prediction=predict(model,test), round(predict(model,test,type="prob"), 3)) | ||
print(Output) | ||
|
||
}) | ||
|
||
# Status/Output Text Box | ||
output$contents <- renderPrint({ | ||
if (input$submitbutton>0) { | ||
isolate("Calculation complete.") | ||
} else { | ||
return("Server is ready for calculation.") | ||
} | ||
}) | ||
|
||
# Prediction results table | ||
output$tabledata <- renderTable({ | ||
if (input$submitbutton>0) { | ||
isolate(datasetInput()) | ||
} | ||
}) | ||
|
||
} | ||
|
||
#################################### | ||
# Create the shiny app # | ||
#################################### | ||
shinyApp(ui = ui, server = server) |