# Getting started with R

The Knitro interface for R, called *KnitroR*, is provided
with your Knitro distribution. In order to install it, you need R 3.0 or
later and run the following command in the R prompt:

```
install.packages('<path to the KnitroR archive>', repos= NULL)
```

Do not forget to define an environment variable KNITRODIR pointing to your local installation directory of Knitro. From the Knitro_11.0 release, the KnitroR package is open-source and needs to be built and installed before running the R command above, via:

```
R CMD INSTALL --build KnitroR
```

To test whether your installation is correct, type in the expression:

```
knitro(objective=function(x) x[1]*x[2], x0=c(1,1))
```

at the R command prompt. If your installation was successful, *KnitroR*
returns the following message:

```
$statusMessage
[1] "Optimal solution found !"
$x
[1] 0 0
$lambda
[1] 0 0
$objective
[1] 0
$constraints
numeric(0)
$iter
[1] 1
$objEval
[1] 9
$gradEval
[1] 0
```

If Knitro is found and called but returns an error, it probably means that no license was found. In any of these situations, please see Troubleshooting.

## The *KnitroR* interface

The Knitro solver can be called via the following optimization function:

```
sol <- knitro(nvar=..., ncon=..., x0=...,
objective=..., gradient=..., constraints=...,
jacobian=..., jacIndexCons=..., jacIndexVars=...,
hessianLag=..., hessIndexRows=..., hessIndexCols=...,
xL=..., xU=..., cL=..., cU=...,
options=...)
```

but the simplest function call reduces to:

```
sol <- knitro(objective=..., x0=...)
sol <- knitro(objective=..., xL=...)
sol <- knitro(objective=..., xU=...)
sol <- knitro(nvar=..., objective=...)
```

## First R example

The following introductory examples shows how to solve the Rosenbrock banana function.

```
library('KnitroR')
# Rosenbrock Banana function
eval_f <- function(x) {
return( 100 * (x[2] - x[1] * x[1])^2 + (1 - x[1])^2 )
}
eval_grad_f <- function(x) {
grad_f <-rep(0, length(x))
grad_f[1] <- 2*x[1]-2+400*x[1]^3-400*x[1]*x[2]
grad_f[2] <- 200*(x[2]-x[1]^2)
return( grad_f )
}
# initial values
x0 <- c( -1.2, 1 )
sol <- knitro(x0 = x0, objective = eval_f)
```

We can save this code in a file ‘example.R’ and run it from the R command prompt via the following command:

```
source('example.R')
```

*KnitroR* returns the following output:

```
=======================================
Commercial License
Artelys Knitro 10.0.1
=======================================
Knitro performing finite-difference gradient computation with 1 thread.
Knitro presolve eliminated 0 variables and 0 constraints.
gradopt: 2
hessopt: 2
outlev: 1
par_concurrent_evals: 0
The problem is identified as unconstrained.
Knitro changing algorithm from AUTO to 1.
Knitro changing bar_initpt from AUTO to 3.
Knitro changing bar_murule from AUTO to 4.
Knitro changing bar_penaltycons from AUTO to 1.
Knitro changing bar_penaltyrule from AUTO to 2.
Knitro changing bar_switchrule from AUTO to 1.
Knitro changing linsolver from AUTO to 2.
Knitro performing finite-difference gradient computation with 1 thread.
Problem Characteristics ( Presolved)
-----------------------
Objective goal: Minimize
Number of variables: 2 ( 2)
bounded below: 0 ( 0)
bounded above: 0 ( 0)
bounded below and above: 0 ( 0)
fixed: 0 ( 0)
free: 2 ( 2)
Number of constraints: 0 ( 0)
linear equalities: 0 ( 0)
nonlinear equalities: 0 ( 0)
linear inequalities: 0 ( 0)
nonlinear inequalities: 0 ( 0)
range: 0 ( 0)
Number of nonzeros in Jacobian: 0 ( 0)
Number of nonzeros in Hessian: 3 ( 3)
EXIT: Locally optimal solution found.
Final Statistics
----------------
Final objective value = 2.00430825877435e-011
Final feasibility error (abs / rel) = 0.00e+000 / 0.00e+000
Final optimality error (abs / rel) = 1.66e-007 / 1.66e-007
# of iterations = 36
# of CG iterations = 5
# of function evaluations = 134
# of gradient evaluations = 0
Total program time (secs) = 0.069 ( 0.000 CPU time)
Time spent in evaluations (secs) = 0.001
===============================================================================
```

## Further information

More functions are availables and R interface can be used to solve MINLP and least squares problems as well. Another chapter of this documentation is dedicated to the R interface (Knitro / R reference) and provides exhaustive references.

Any Knitro option can also be provided to the R interface. A comprehensive documentation of Knitro options is available in the section Knitro user options.

## Additional examples

More examples using the R interface are provided in
the `examples/R`

directory of the Knitro distribution.