Skip to content

Instantly share code, notes, and snippets.

@hrbrmstr
Last active August 28, 2021 01:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hrbrmstr/8499598 to your computer and use it in GitHub Desktop.
Save hrbrmstr/8499598 to your computer and use it in GitHub Desktop.
Shiny app code for SolvoMediocris : http://shiny.dds.ec/solvomediocris/
library(shiny)
library(mc2d)
library(ggplot2)
library(scales)
shinyServer(function(input,output){
values <- reactiveValues()
values$N <- 50000
values$run <- "no"
observe({
if (input$runmodel != 0) {
isolate({
values$N <- input$N
TEFestimate <- data.frame(L = input$tefl, ML = input$tefml, H = input$tefh, CONF = input$tefconf)
TSestimate <- data.frame(L = input$tcapl, ML = input$tcapml, H = input$tcaph, CONF = input$tcapconf)
RSestimate <- data.frame(L = input$csl, ML = input$csml, H = input$csh, CONF = input$csconf)
LMestimate <- data.frame(L = input$lml, ML = input$lmml, H = input$lmh, CONF = 1)
LMsample <- function(x){
return(sum(rpert(x, LMestimate$L, LMestimate$ML, LMestimate$H, shape = LMestimate$CONF) ))
}
TEFsamples <- rpert(values$N, TEFestimate$L, TEFestimate$ML, TEFestimate$H, shape = TEFestimate$CONF)
TSsamples <- rpert(values$N, TSestimate$L, TSestimate$ML, TSestimate$H, shape = TSestimate$CONF)
RSsamples <- rpert(values$N, RSestimate$L, RSestimate$ML, RSestimate$H, shape = RSestimate$CONF)
VULNsamples <- TSsamples > RSsamples
LEF <- TEFsamples[VULNsamples]
values$ALEsamples <- sapply(LEF, LMsample)
values$VAR <- quantile(values$ALEsamples, probs=(0.95))
})
}
})
output$detail <- renderPrint({
if (input$runmodel != 0) {
print(summary(values$ALEsamples));
}
})
output$detail2 <- renderPrint({
if (input$runmodel != 0) {
print(paste0("Losses at 95th percentile are $", format(values$VAR, nsmall = 2, big.mark = ",")));
}
})
output$plot <- renderPlot({
if (input$runmodel != 0) {
ALEsamples <- values$ALEsamples
gg <- ggplot(data.frame(ALEsamples), aes(x = ALEsamples))
gg <- gg + geom_histogram(binwidth = diff(range(ALEsamples)/50), aes(y = ..density..), color = "black", fill = "white")
gg <- gg + geom_density(fill = "steelblue", alpha = 1/3)
gg <- gg + scale_x_continuous(labels = comma)
gg <- gg + theme_bw()
print(gg)
}
})
})
shinyUI(pageWithSidebar(
headerPanel("SolvoMediocris"),
sidebarPanel(
tags$head(
tags$style(type="text/css", "input { font-size:10px; width:40px; display:inline-block; }"),
tags$style(type="text/css", "#lml, #lmml, #lmh, #lmconf { font-size:11px; width:100px; display:inline-block; }"),
tags$style(type="text/css", "#N { font-size:14px; width:200px; display:inline-block; }"),
tags$style(type="text/css", "label[for=N] { font-size:14px; display:inline-block; }"),
tags$style(type="text/css", "label { font-size:10px; display:inline-block; }")
),
h4("Threat Event Frequency"),
numericInput("tefl", "Min:", 10, min = 0, max = 100),
numericInput("tefml", "ML:", 20, min = 0, max = 100),
numericInput("tefh", "Max:", 100, min = 0, max = 100),
numericInput("tefconf", "Conf:", 1, min = 1, max = 5),
h4("Threat Capability"),
numericInput("tcapl", "Min:", 20, min = 0, max = 100),
numericInput("tcapml", "ML:", 30, min = 0, max = 100),
numericInput("tcaph", "Max:", 70, min = 0, max = 100),
numericInput("tcapconf", "Conf:", 1, min = 1, max = 5),
h4("Control Strength"),
numericInput("csl", "Min:", 40, min = 0, max = 100),
numericInput("csml", "ML:", 50, min = 0, max = 100),
numericInput("csh", "Max:", 60, min = 0, max = 100),
numericInput("csconf", "Conf:", 2, min = 1, max = 5),
h4("Loss Magnitude"),
numericInput("lml", "Min:", 100, min = 0),
numericInput("lmml", "ML:", 500, min = 0), br(),
numericInput("lmh", "Max:", 10000, min = 0),
numericInput("lmconf", "Conf:", 1, min = 1, max = 5), br(),
numericInput("N", "# Iterations:", 50000, min = 1000, step=1000), br(),
actionButton("runmodel", "Run Model"),
div(HTML("<br/><small>(App brought to you by <a href='http://datadrivensecurity.info'>Data Driven Security</a>)</small>"))
),
mainPanel(
tabsetPanel(
tabPanel("Distribution", plotOutput("plot")),
tabPanel("Detail", verbatimTextOutput("detail"), verbatimTextOutput("detail2"))
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment