0
I am trying to map prediction results in the Shiny app. In this context, the users should be provided with different input masks. If a user enters e.g. Adults = 2, children = 1, previous_cancellations = 0 etc., then the Shiny App should output in percent how likely it is that a cancellation will happen under the given conditions.
Unfortunately, I get the following error:
Object of type 'closure' is not indexable.
Can someone please help me to solve both errors?
This is the code:
library(data.table)
library(RCurl)
library(randomForest)
library(flexdashboard)
hotel <- structure(
list(
hotel = c(
"Resort Hotel", "Resort Hotel", "Resort Hotel",
"Resort Hotel", "Resort Hotel", "Resort Hotel", "City Hotel",
"Resort Hotel", "Resort Hotel", "Resort Hotel", "Resort Hotel",
"Resort Hotel", "Resort Hotel", "Resort Hotel", "Resort Hotel",
"Resort Hotel", "Resort Hotel", "Resort Hotel", "Resort Hotel",
"Resort Hotel", "City Hotel", "Resort Hotel", "Resort Hotel",
"Resort Hotel", "Resort Hotel", "Resort Hotel", "Resort Hotel",
"Resort Hotel", "Resort Hotel", "Resort Hotel", "Resort Hotel",
"Resort Hotel", "Resort Hotel", "Resort Hotel", "Resort Hotel",
"Resort Hotel", "Resort Hotel", "Resort Hotel", "Resort Hotel",
"Resort Hotel", "Resort Hotel", "Resort Hotel", "Resort Hotel",
"Resort Hotel", "Resort Hotel", "Resort Hotel", "Resort Hotel",
"City Hotel", "Resort Hotel", "Resort Hotel"),
is_canceled = c(0,
0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0,
0, 0, 1, 0, 0, 0, 0),
adults = c(2, 2, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1,
2, 2, 2, 3, 3, 2, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
children = c(0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
0, 0, 2, 0, 0, 0, 0),
babies = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 3, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0,
1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
meal = c("BB",
"BB", "BB", "BB", "BB", "BB", "BB", "FB", "BB", "HB", "BB", "HB",
"BB", "HB", "BB", "BB", "BB", "BB", "BB", "BB", "BB", "BB", "BB",
"BB", "HB", "BB", "BB", "BB", "BB", "BB", "BB", "BB", "BB", "BB",
"BB", "BB", "BB", "BB", "BB", "HB", "BB", "BB", "BB", "HB", "BB",
"BB", "BB", "BB", "HB", "HB"),
is_repeated_guest = c(
0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0
),
previous_cancellations = c(
0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0
),
previous_bookings_not_canceled = c(
0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1
),
reserved_room_type = c(
"C", "C", "A", "A", "B", "A", "C",
"C", "A", "D", "E", "D", "D", "G", "E", "D", "E", "A", "A", "G",
"F", "A", "A", "D", "D", "D", "D", "E", "A", "D", "A", "D", "E",
"A", "D", "D", "A", "D", "D", "E", "G", "D", "F", "E", "A", "G",
"A", "B", "A", "E"
),
deposit_type = c(
"No Deposit", "No Deposit",
"No Deposit", "No Deposit", "No Deposit", "No Deposit", "No Deposit",
"No Deposit", "No Deposit", "No Deposit", "No Deposit", "No Deposit",
"No Deposit", "No Deposit", "Test", "No Deposit", "No Deposit",
"No Deposit", "No Deposit", "No Deposit", "No Deposit", "No Deposit",
"No Deposit", "No Deposit", "No Deposit", "No Deposit", "No Deposit",
"Test", "No Deposit", "No Deposit", "No Deposit", "No Deposit",
"No Deposit", "No Deposit", "No Deposit", "No Deposit", "No Deposit",
"No Deposit", "No Deposit", "No Deposit", "No Deposit", "No Deposit",
"No Deposit", "No Deposit", "No Deposit", "No Deposit", "No Deposit",
"No Deposit", "No Deposit", "No Deposit"
),
customer_type = c(
"Transient",
"Transient", "Transient", "Transient", "Transient", "Transient",
"Transient", "Transient", "Transient", "Transient", "Transient",
"Transient", "Transient", "Transient", "Transient", "Transient",
"Contract", "Transient", "Transient", "Transient", "Transient",
"Transient", "Transient", "Transient", "Contract", "Transient",
"Contract", "Transient", "Transient", "Transient", "Transient",
"Transient", "Transient", "Transient", "Transient", "Transient",
"Transient", "Transient", "Transient", "Transient", "Transient",
"Contract", "Transient", "Transient", "Transient", "Transient",
"Transient", "Transient-Party", "Contract", "Transient"
),
adr = c(
0,
0, 75, 75, 98, 98, 107, 103, 82, 105, 123, 145, 97, 154, 94,
97, 97, 88, 107, 153, 97, 84, 84, 99, 94, 63, 79, 107, 94, 87,
62, 63, 108, 65, 108, 108, 98, 108, 108, 137, 117, 79, 123, 137,
110, 153, 58, 82, 82, 119
),
total_of_special_requests = c(
0,
0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 3, 1, 0, 3, 0, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 2, 0, 1, 2, 0, 2, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0,
1, 2, 0, 1, 2, 0, 1
)
),
row.names = c(NA, -50L),
class = c("tbl_df",
"tbl", "data.frame")
)
hotel$is_canceled <- as.factor(hotel$is_canceled)
hotel$is_repeated_guest <- as.factor(hotel$is_repeated_guest)
hotel$meal <- as.factor(hotel$meal)
hotel$reserved_room_type <- as.factor(hotel$reserved_room_type)
hotel$deposit_type <- as.factor(hotel$deposit_type)
hotel$customer_type <- as.factor(hotel$customer_type)
hotel$adr <- as.integer(hotel$adr)
hotel$hotel <- as.factor(hotel$hotel)
library(dplyr)
model_rf <- randomForest::randomForest(is_canceled ~., data = hotel, ntree = 100, mtry = 5, importance = TRUE)
ui <- fluidPage(headerPanel('HotelCancellationPrediction'),
sidebarPanel(
selectInput('hotel', label = 'Hotel',
c('City Hotel','Resort Hotel')),
sliderInput('adults', label = 'Erwachsene',
min=1, max=3, value=1),
sliderInput('children', label = 'Kinder',
min=0, max =2, value = 0),
sliderInput('babies', label = 'Kinder',
min = 0, max=3, value = 0),
selectInput('meal', label = 'Mahlzeit',
c('HB','BB')),
sliderInput('is_repeated_guest', label='Erneuter Gast',
min = 0, max = 1, value = 0),
sliderInput('previous_cancellations', label='Vorherige Stornierungen',
min = 0, max = 1, value = 0),
sliderInput('previous_bookings_not_canceled', label = 'vorherige Bookings not cancelled',
min = 0, max = 1, value = 0),
selectInput('reserved_room_type', label = 'Raumtyp',
c('A','B','C','D','E','F','G')),
selectInput('deposit_type', label='Bezahltyp',
c('No Deposit','Test')),
selectInput('customer_type', label = 'Kundentyp',
c('Transient','Transient-Party','Contract')),
sliderInput('adr', label = 'Kosten',
min=0, max=200, value=0),
sliderInput('total_of_special_requests', label='Sonderwuensche',
min=0, max=2, value=0),
submitButton('Submit')
),
mainPanel(
tags$label(h3('Status/Output')),
verbatimTextOutput('contents'),
tableOutput('tabledata'),
gaugeOutput('Scale')
)
)
server <- function(input, output, session){
datasetInput <- reactive({
df <- data.frame(
Name = c('hotel',
'adults',
'children',
'babies',
'meal',
'is_repeated_guest',
'previous_cancellations',
'previous_bookings_not_canceled',
'reserved_room_type',
'deposit_type',
'customer_type',
'adr',
'total_of_special_requests'),
value = as.character(c(input$hotel,
input$adults,
input$children,
input$babies,
input$meal,
input$is_repeated_guest,
input$previous_cancellations,
input$previous_bookings_not_canceled,
input$reserved_room_type,
input$deposit_type,
input$customer_type,
input$adr,
input$total_of_special_requests)),
stringsAsFactors=FALSE)
is_canceled <- 'is_canceled'
df <- rbind(df, is_canceled)
input <- transpose(df)
write.table(input, 'input.csv', sep =',', quote=FALSE, row.names=FALSE, col.names = FALSE)
write.table(input,"input4.csv", sep = ',', quote = FALSE, row.names = FALSE, col.names=FALSE)
test <- read.csv(paste("input", ".csv", sep=""), header = TRUE)
test$hotel <- factor(test$hotel, levels=c("City Hotel","Resort Hotel"))
test$meal <- factor(test$meal, levels = c('HB','BB'))
test$reserved_room_type <- factor(test$reserved_room_type, levels = c('A','B','C','D','E','F','G'))
test$deposit_type <- factor(test$deposit_type, levels = c('No Deposit','Test'))
test$customer_type <- factor(test$customer_type, levels = c('Transient','Transient-Party','Contract'))
Output <- data.frame(Prediction = predict(model_rf, test), round(predict(model_rf, test, type='prob'),3))
print(Output)
})
my_plot <- reactive({
gauge(datasetInput()$Yes, min=0, max=100, symbol='%', label='Success', gaugeSectors(
success = c(80, 100), warning = c(40, 79), danger = c(0,39)))
})
output$Scale <- renderGauge({
my_plot
})
output$contents <- renderPrint({
"forecast Results"
})
output$tabledata <- renderTable({
datasetInput()
})
}
shinyApp(ui=ui, server=server)```