Hi awesome community, I am trying to build a shiny App that predicts theft (Electricity theft), but I am finding it difficult to create a dashboard according to my mind specification. I have created the dashboard but how do I:
- Set the predicted data to the dashboard such that whenever data is uploaded and the prediction button is clicked, the output should be shown in the dashboard. The output here is # OF PREDICTED THEFT CASES, # OF NO THEFT CASES and TOTAL NUMBER OF CUSTOMERS (based on the uploaded data)
- The dashboard should default to zero when no data is uploaded.
Here's my server code
library(caret)
#> Loading required package: lattice
#> Loading required package: ggplot2
library(shiny)
library(LiblineaR)
library(readr)
library(ggplot2)
library(tidyverse)
library(tidymodels)
model <- load("treemodel.rda")
source("C:/Users/PHED KEYACCTS/Documents/Theft Project/NEW UI/TheftApp/TheftApp/UI.R/Server/app.R")
server <- shinyServer(function(input, output) {
#####################********************#################################
options(shiny.maxRequestSize = 800*1024^2)
# The # of "views"
output$Total <- renderValueBox({
valueBox(
value = format(totalAccounts, big.mark=","),
subtitle = "Total # of Accounts",
color = "aqua"
)
})
# The # of "reads"
output$Bypass <- renderValueBox({
valueBox(
value = format(Bypass, big.mark=","),
subtitle = "Total # of Bypass"
)
})
# The # of "fans"
output$NoBypass <- renderValueBox({
valueBox(
value = format(NoBypass, big.mark=","),
subtitle = "Total # of No Bypass"
)
})
#############################********################************################
predictions<-reactive({
inFile <- input$file1
if (is.null(inFile)){
return(NULL)
}else{
withProgress(message = 'Predictions in progress. Please wait ...', {
input_data = readr::read_csv(input$file1$datapath, col_names = TRUE)
colnames(input_data) = c("Status","Availability", "Average.Consumption", "Estimated.Connected.Load",
"group_cons","MeterBypass","FeederBypass","TariffBypass")
input_data$Status = as.factor(input_data$Status )
levels(input_data$Status) <- c("Bypassed","NoBypass")
prediction = predict(tree.mod,input_data)
totalAccounts = sum(prediction)
Bypass = sum(prediction == 1)
NoBypass = sum(prediction == 0)
input_data_with_prediction = cbind(input_data,prediction )
input_data_with_prediction
})
}
})
#############################********################************################
#Downlaod MenuItem
output$sample_predictions = renderTable({ # the last 6 rows to show
pred = predictions()
head(pred)
})
# output$plot_predictions = renderPlot({ # the last 6 rows to show
#pred = predictions()
#cols <- c("Bypass" = "red","NoBypass" = "blue")
#ggplot(pred, aes(x =status,y= value)+ geom_point(size = 4, shape = 19, alpha = 0.6) +
#scale_colour_manual(values = cols,labels = c("Bypass", "NoBypass"),name="Test Result"))
#})
# Downloadable csv of predictions ----
output$downloadData <- downloadHandler(
filename = function() {
paste("input_data_with_predictions", ".csv", sep = "")
},
content = function(file) {
write.csv(predictions(), file, row.names = FALSE)
})
})
#shinyApp(ui = ui, server = server)
...