I am writing my first shiny application where the user enters numeric inputs as vectors, and the output should be the value of the objective function and the values ​​of the final variables. I’m new in shiny and I’m not sure if I have the inputs and function created correctly. Below is the application code:
library(shiny)
library(shinythemes)
library(lpSolveAPI)
ui <- fluidPage(theme = shinytheme("united"),
navbarPage(" Optimization",
tabPanel("Linear programming",
sidebarLayout(
sidebarPanel(
h3('Please enter initial values'),
textInput('obj', 'Objective function values (comma separated)', "1,2"),
textInput('con', 'ohran vector (comma separated)', "0,1,2,1"),
numericInput('nrow', 'number of rows - constraints', "2"),
textInput('dir', 'Direction - constraints (comma separated)', ">=,<="),
textInput('rhs', 'Right-hand sides - constraint (comma separated)', "2,8"),
submitButton('Submit')
),
mainPanel(
h4('The value of the objective function:'),
verbatimTextOutput("value_obj"),
h4('values of variables: '),
verbatimTextOutput("value_var")
)
)
)
)
)
server <- function(input, output, session) {
output$value_obj<-renderPrint({
f.obj <- as.numeric(unlist(strsplit(input$obj,",")))
f.con <- as.matrix(as.numeric(unlist(strsplit(input$con,","))),nrow=input$nrow, byrow = TRUE )
f.dir <- as.array(unlist(strsplit(input$dir,",")))
f.rhs <- as.numeric(unlist(strsplit(input$rhs,",")))
hodnota_obj = lp("max", f.obj, f.con, f.dir, f.rhs)$objval
cat("The value of the objective function:\n")
print(value_obj)
}
)
output$value_var<-renderPrint({
f.obj <- as.numeric(unlist(strsplit(input$obj,",")))
f.con <- as.matrix(as.numeric(unlist(strsplit(input$con,","))),nrow=input$nrow, byrow = TRUE )
f.dir <- as.array(unlist(strsplit(input$dir,",")))
f.rhs <- as.numeric(unlist(strsplit(input$rhs,",")))
hodnota_obj = lp("max", f.obj, f.con, f.dir, f.rhs)$solution
cat("The value of the varieaables :\n")
print(value_var)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
>Solution :
The lp function is from lpSolve which is not loaded. In addition, there are some issue in the print statements where it is called print(value_var) or print(value_obj) which is not correct because those objects are not created. The output$value_var and output$value_obj are the output object names created based on the hodnota_obj object
library(shiny)
library(shinythemes)
library(lpSolveAPI)
library(lpSolve)
ui <- fluidPage(theme = shinytheme("united"),
navbarPage(" Optimization",
tabPanel("Linear programming",
sidebarLayout(
sidebarPanel(
h3('Please enter initial values'),
textInput('obj', 'Objective function values (comma separated)', "1,2"),
textInput('con', 'ohran vector (comma separated)', "0,1,2,1"),
numericInput('nrow', 'number of rows - constraints', "2"),
textInput('dir', 'Direction - constraints (comma separated)', ">=,<="),
textInput('rhs', 'Right-hand sides - constraint (comma separated)', "2,8"),
submitButton('Submit')
),
mainPanel(
h4('The value of the objective function:'),
verbatimTextOutput("value_obj"),
h4('values of variables: '),
verbatimTextOutput("value_var")
)
)
)
)
)
server <- function(input, output, session) {
output$value_obj<-renderPrint({
f.obj <- as.numeric(unlist(strsplit(input$obj,",")))
f.con <- as.matrix(as.numeric(unlist(strsplit(input$con,","))),nrow=input$nrow, byrow = TRUE )
f.dir <- as.array(unlist(strsplit(input$dir,",")))
f.rhs <- as.numeric(unlist(strsplit(input$rhs,",")))
hodnota_obj = lp("max", f.obj, f.con, f.dir, f.rhs)$objval
cat("The value of the objective function:\n")
hodnota_obj
}
)
output$value_var<-renderPrint({
f.obj <- as.numeric(unlist(strsplit(input$obj,",")))
f.con <- as.matrix(as.numeric(unlist(strsplit(input$con,","))),nrow=input$nrow, byrow = TRUE )
f.dir <- as.array(unlist(strsplit(input$dir,",")))
f.rhs <- as.numeric(unlist(strsplit(input$rhs,",")))
hodnota_obj = lp("max", f.obj, f.con, f.dir, f.rhs)$solution
cat("The value of the varieaables :\n")
hodnota_obj
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
-output
