线性规划中的 R Shiny App 错误:找不到函数 "lp"?

R Shiny App error in linear programming: could not find function "lp"?

我正在写我的第一个闪亮的应用程序,用户输入数字作为向量,输出应该是 objective 函数的值和最终变量的值。我是 shiny 的新手,我不确定我是否正确创建了输入和函数。以下是申请代码:

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)

lp 函数来自 lpSolve 未加载。此外,在 print 语句中存在一些问题,其中调用 print(value_var)print(value_obj) 是不正确的,因为这些对象未创建。 output$value_varoutput$value_obj 是基于 hodnota_obj 对象

创建的输出对象名称
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)

-输出