I have already made a r script to calculate queue times using queuing theory but now I wish to make a ui/website where the user can input information like number of people and service time then run the r script I made to give the user the output. But I am unable to get the input from the r shiny website to the r script I made because the values are not coming up in the global variables list. Here is the code so far:
library(shiny)
ui <- fluidPage(
h1("This is a test input"),
numericInput("Lambda","Enter people coming in",20),
actionButton("runScript", "Run")
)
server <- function(input, output, session){
mylist <- reactiveVal()
observe({
mylist(list(
Lambda = input$Lambda))
})
observeEvent(input$runScript, {
source("algo.R", local = list2env(mylist()))
})
}
shinyApp(ui = ui, server = server)
the queuing theory script
Lambda <- Lambda
Mue <- 4
Rho <- Lambda/Mue
matrix <- matrix(0,10,2)
matrix[,1] <- 1:10
calculatewq <- function(c){
P0inv <- Rho^c/(factorial(c)*(1-(Rho/c)))
for (i in 1:c-1) {
P0inv = P0inv (Rho^i)/factorial(i)
}
P0 = 1/P0inv
Lq = (Rho^(c 1))*P0/(factorial(c-1)*(c-Rho)^2)
Wq = 60*Lq/Lambda
Ls <- Lq Rho
Ws <- 60*Ls/Lambda
#print(paste(Lq," is queue length and ",Wq," is the waiting time"))
a <- cbind(Lq,Wq,Ls,Ws)
return(a)
}
for (i in 1:10){
matrix[i,2] <- calculatewq(i)[2]
}
What do I need to do so that I can take two inputs from the r shiny website and then use those inputs in the r script then display the matrix that comes out in the end.
CodePudding user response:
To elaborate on my comment:
library(shiny)
myScriptFunction <- function(Lambda) {
Mue <- 4
Rho <- Lambda/Mue
matrix <- matrix(0,10,2)
matrix[,1] <- 1:10
calculatewq <- function(c){
P0inv <- Rho^c/(factorial(c)*(1-(Rho/c)))
for (i in 1:c-1) {
P0inv = P0inv (Rho^i)/factorial(i)
}
P0 = 1/P0inv
Lq = (Rho^(c 1))*P0/(factorial(c-1)*(c-Rho)^2)
Wq = 60*Lq/Lambda
Ls <- Lq Rho
Ws <- 60*Ls/Lambda
#print(paste(Lq," is queue length and ",Wq," is the waiting time"))
a <- cbind(Lq,Wq,Ls,Ws)
return(a)
}
for (i in 1:10){
matrix[i,2] <- calculatewq(i)[2]
}
matrix
}
ui <- fluidPage(
h1("This is a test input"),
numericInput("Lambda","Enter people coming in",20),
actionButton("runScript", "Run"),
tableOutput("result")
)
server <- function(input, output, session){
mylist <- reactiveVal()
observe({
mylist(list(
Lambda = input$Lambda))
})
output$result <- renderTable({
input$runScript
isolate(as.data.frame(myScriptFunction(input$Lambda)))
})
}
shinyApp(ui = ui, server = server)
You could also eliminate the actionButton
entirely and have the output update immediately the selectInput
is changed by using:
output$result <- renderTable({
as.data.frame(myScriptFunction(input$Lambda))
})