Error in nonlinear programming Shiny app using nloptr

Hi everybody, I am trying to create a simple app that calculates nonlinear optimization based on the entered inputs and I have an error: STRING_ELT () can only be applied to a 'character vector', not a 'NULL'.

If I try add objective function and constraints into raw code to function eval_f, eval_g_eq and eval_g_ineq it calculates everything, but problem is calculate from input.

library(shiny)
library(shinythemes)
library(nloptr)

eval_f <<- function(x)
{
  return (obj)
}

eval_g_eq <<- function(x)
{
  return(eq)
}

eval_g_ineq <<- function(x)
{
  return(ineq)
}  


ui <- fluidPage(theme = shinytheme("united"),
                navbarPage(" Optimization",
                           tabPanel("Nonlinear programming",
                                    sidebarLayout(
                                      sidebarPanel(
                                        h3('Please enter nonlinear problem for solving'),
                                        textInput('obj', 'Objective  function ', "x[1]*x[4]*(x[1] +x[2] + x[3]) + x[3])"),
                                        textInput('eq', 'Equality constraints ', "x[1]^2 + x[2]^2 + x[3]^2 + x[4]^2 - 40"),
                                        textInput('ineq', 'Inequality constraints', "25 - x[1]*x[2]*x[3]*x[4]"),
                                        textInput('lb', 'Lower bounds (comma separated)', "1,1,1,1"),
                                        textInput('ub', 'Upper bounds (comma separated)', "5,5,5,5"),
                                        textInput('x0', 'Initial values (comma separated)', "1,5,5,1"),
                                        submitButton('Submit')
                                      ),
                                      
                                      mainPanel(
                                        h4('The result is:'),
                                        verbatimTextOutput("res")
                                      )
                                    )
                           )
                )
)

server <- function(input, output, session) {
  
  output$res<-renderPrint({ 
    obj<<- as.vector(input$obj)
    eq <<-as.vector(input$eq)
    ineq <<-as.vector(input$ineq)
    lb <<- as.numeric(unlist(strsplit(input$lb,",")))
    ub <<- as.numeric(unlist(strsplit(input$ub,",")))
    x0 <<- as.numeric(unlist(strsplit(input$x0,",")))
    
    
    local_opts <- list( "algorithm" = "NLOPT_LN_COBYLA", "xtol_rel" = 1.0e-15 )
    opts <- list( "algorithm"= "NLOPT_GN_ISRES",
                  "xtol_rel"= 1.0e-15,
                  "maxeval"= 160000,
                  "local_opts" = local_opts,
                  "print_level" = 0 )
    
    
    res <- nloptr ( x0 = x0,
                    eval_f = eval_f,
                    lb = lb,
                    ub = ub,
                    eval_g_ineq = eval_g_ineq,
                    eval_g_eq = eval_g_eq,
                    opts = opts)
    
    cat("Result:\n")
    print(res)
  }
  ) 
}

# Run the application 
shinyApp(ui = ui, server = server)

I appreciate your help
Thank you

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.