Hi nirgrahamuk,
Again, thanks for the reply!
I think I wasn't clear enough in my explination of what I'm trying to do, so I found a example of a machine learning application (credit to Data Professor on Youtube), take a look at the code below.
I am trying to make it easier to update the ML algorithm AND user interface when new data becomes available. In the example below, we are trying to predict if a person would go golfing given the state of four different variables. Imagine that I have now gathered a 5th variable, which I would like to include in the prediction (and therefore the application). I want to be able to include this 5th variable without having to hardcode the variable into the application, but simply by uploading a new dataset which includes all the previous varaibles as well as the new one.
Is it possible to do this without having to hard code any variables?
# Import libraries
library(shiny)
library(shinythemes)
library(data.table)
library(RCurl)
library(randomForest)
# Read data
DATA <- read.csv(text = getURL("https://raw.githubusercontent.com/dataprofessor/data/master/weather-weka.csv"))
# Clean data
DATA <- as.data.frame(unclass(DATA), stringsAsFactors = TRUE)
# Build model
model <- randomForest(play ~ ., data = DATA, ntree = 500, mtry = 4, importance = TRUE)
####################################
# User interface #
####################################
ui <- fluidPage(theme = shinytheme("united"),
# Page header
headerPanel('Play Golf?'),
# Input values
sidebarPanel(
HTML("<h3>Input parameters</h3>"),
selectInput("outlook", label = "Outlook:",
choices = levels(DATA[,1]),
selected = levels(DATA[,1][1])),
sliderInput("temperature", "Temperature:",
min = 64, max = 86,
value = 70),
sliderInput("humidity", "Humidity:",
min = 65, max = 96,
value = 90),
selectInput("windy", label = "Windy:",
choices = list("Yes" = "TRUE", "No" = "FALSE"),
selected = "TRUE"),
actionButton("submitbutton", "Submit", class = "btn btn-primary")
),
mainPanel(
"Will a person play golf given these circumstances?",
verbatimTextOutput('contents'),
tableOutput('tabledata') # Prediction results table
)
)
####################################
# Server #
####################################
server <- function(input, output, session) {
# Input Data
datasetInput <- reactive({
# outlook,temperature,humidity,windy,play
df <- data.frame(
Name = c("outlook",
"temperature",
"humidity",
"windy"),
Value = as.character(c(input$outlook,
input$temperature,
input$humidity,
input$windy)),
stringsAsFactors = FALSE)
input <- transpose(rbind(df, "play"))
write.table(input,"input.csv", sep=",", quote = FALSE, row.names = FALSE, col.names = FALSE)
test <- read.csv(paste("input", ".csv", sep=""), header = TRUE)
test$outlook <- factor(test$outlook, levels = levels(DATA[,1]))
Output <- data.frame(Prediction=predict(model,test), round(predict(model,test,type="prob"), 3))
print(Output)
})
# Prediction results table
output$tabledata <- renderTable({
if (input$submitbutton>0) {
isolate(datasetInput())
}
})
}
####################################
# Create the shiny app #
####################################
shinyApp(ui = ui, server = server)