Skip to content

Instantly share code, notes, and snippets.

@koaning
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koaning/1b7b52e03ba5d81d5966 to your computer and use it in GitHub Desktop.
Save koaning/1b7b52e03ba5d81d5966 to your computer and use it in GitHub Desktop.
ShinieR Interaction with <<-
library(shiny)
library(ggplot2)
library(reshape2)
library(randomForest)
df = ChickWeight
n = nrow(df)
perfdf = data.frame(ntree=as.numeric(), mse_train=as.numeric(), mse_test=as.numeric())
count = 0
shinyServer(function(input, output) {
output$runs <- renderText({
train <- df[sample(n),][1:478,]
test <- df[sample(n),][479:n,]
rf = randomForest(weight ~ Time + Diet, data=train, ntree = input$ntree)
train$mse <- (predict(rf, train) - train$weight)^2
test$mse <- (predict(rf, test) - test$weight)^2
perfdf <<- rbind(perfdf, data.frame(ntree=input$ntree, mse_train=mean(train$mse), mse_test=mean(test$mse)))
count <<- count + 1
paste("random forest count :", count)
})
output$perf <- renderPlot({
input$ntree
perfdf <- melt(perfdf, id.vars = c("ntree"),
variable.name = "variable", value.name = "value"
)
p = ggplot() + geom_point(data=perfdf, aes(ntree, value, colour=variable))
# p = p + facet_grid( variable ~ . , scales = "free_y")
p = p + stat_smooth(data=perfdf, aes(ntree, value, colour=variable), se = FALSE)
# geom_smooth(data=perfdf, aes(ntree, value, colour=variable))
p + ggtitle("train set performance")
}, height=400)
})
library(shiny)
shinyUI(fluidPage(
# Application title
titlePanel("Shiny App"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("ntree",
"give the number of trees:",
min = 1,
max = 200,
value = 10, step=1)
),
# Show a plot of the generated distribution
mainPanel(
textOutput("runs"),
plotOutput("perf")
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment