For training purposes, I would like to make a Shiny application where you can check the column names and use them to teach an arbitrary forest algorithm.
My Shiny Application looks like this:
library(shiny)
library(DT)
library(titanic)
library(randomForest)
ui <- fluidPage(
DT::dataTableOutput("mytable"),
checkboxInput("checkbox" , label = "Pclass", value = FALSE),
checkboxInput("checkbox" , label = "Sex", value = FALSE),
checkboxInput("checkbox" , label = "Age", value = FALSE),
actionButton("runRF", "Predict"),
plotOutput("plotRF")
)
server <- function(input, output, session) {
output$mytable = DT::renderDataTable({
titanic_train
})
observeEvent(input$runRF, {
var = c("Pclass")
fit <- randomForest(as.factor(Survived) ~ var, data = titanic_train, importance = TRUE, ntree=2000)
prediction <- as.numeric(predict(fit, titanic_test))
titanic_test$predicted <- prediction
output$plotRF <- renderPlot({
hist(prediction)
})
})
}
shinyApp(ui, server)
Basically the code above works when I do something like:
fit <- randomForest(as.factor(Survived) ~ Age, data = titanic_train, importance = TRUE, ntree=2000)
Or
fit <- randomForest(as.factor(Survived) ~ Pclass + Age, data = titanic_train, importance = TRUE, ntree=2000)
However, I would like to make a workout dependent variable or fields that you are checking. Therefore, if you check Age + Pclass, it should be:
fit <- randomForest(as.factor(Survived) ~ Pclass + Age, data = titanic_train, importance = TRUE, ntree=2000)
If you noted age:
fit <- randomForest(as.factor(Survived) ~ Age, data = titanic_train, importance = TRUE, ntree=2000)
I assume that I need to make a list where I store the "checked values", for example:
var = c(checkElement1)
However, this gives me the following erorr:
Warning: Error in model.frame.default: variable lengths differ (found for 'var')
Stack trace (innermost first):
74: model.frame.default
73: model.frame
72: eval
71: eval
70: randomForest.formula
69: randomForest
68: observeEventHandler [#11]
4: <Anonymous>
3: do.call
2: print.shiny.appobj
1: <Promise>
Any thoughts where this is going wrong?