Documente Academic
Documente Profesional
Documente Cultură
01/04/15 12:48 am
The R environment
R is an integrated suite of software facilities for data manipulation, calculation and
graphical display. It includes
an effective data handling and storage facility,
a suite of operators for calculations on arrays, in particular matrices,
a large, coherent, integrated collection of intermediate tools for data
analysis,
graphical facilities for data analysis and display either on-screen or on
hardcopy, and
a well-developed, simple and effective programming language which includes
conditionals, loops, user-defined recursive functions and input and output
facilities.
The term "environment" is intended to characterize it as a fully planned and
coherent system, rather than an incremental accretion of very specific and
inflexible tools, as is frequently the case with other data analysis software.
http://lpsolve.sourceforge.net/5.5/R.htm
Page 1 of 6
01/04/15 12:48 am
R, like S, is designed around a true computer language, and it allows users to add
additional functionality by defining new functions. Much of the system is itself
written in the R dialect of S, which makes it easy for users to follow the
algorithmic choices made. For computationally-intensive tasks, C, C++ and
Fortran code can be linked and called at run time. Advanced users can write C
code to manipulate R objects directly.
Many users think of R as a statistics system. We prefer to think of it of an
environment within which statistical techniques are implemented. R can be
extended (easily) via packages. There are about eight packages supplied with the
R distribution and many more are available through the CRAN family of Internet
sites covering a very wide range of modern statistics.
We will not discuss the specifics of R here but instead refer the reader to the R
website. Also see An Introduction to R
R and lpsolve
lpsolve is callable from R via an extension or module. As such, it looks like lpsolve
is fully integrated with R. Matrices can directly be transferred between R and
lpsolve in both directions. The complete interface is written in C so it has
maximum performance.
There are currently two R packages based on lp_solve. Both packages are
available from CRAN.
The lpSolve R package is the first implementation of an interface of lpsolve to R. It
provides high-level functions for solving general linear/integer problems,
assignment problems and transportation problems. The following link contains the
version of the driver: lpSolve: Interface to Lp_solve v. 5.5 to solve linear/integer
programs. It does not contain the lpsolve API. Only the higher level calls.
Documentation for this interface can be found on: Interface to Lp_solve v. 5.5 to
solve linear/integer programs
This driver is written and maintained by Sam Buttrey.
The lpSolveAPI R package is a second implementation of an interface of lpsolve to
R. It provides an R API mirroring the lp_solve C API and hence provides a great
deal more functionality but has a steeper learning curve. The R interface to lpsolve
contains its own documentation. See An R interface to the lp_solve library for the
driver.
This driver is written and maintained by Kjell Konis.
http://lpsolve.sourceforge.net/5.5/R.htm
Page 2 of 6
01/04/15 12:48 am
In the RGui menu, there is a menu item 'Packages'. From there a package can be
installed from a CRAN mirror or from a local zip file.
R command line
Packages can also be installed from the R command line. This is a more general
approach that will work under all environments.
Installing the package takes a single command:
The lpSolve R package:
> install.packages("lpSolve")
The > shown before each R command is the R prompt. Only the text after > must
be entered.
Or
> library("lpSolveAPI", character.only=TRUE)
Getting Help
Documentation is provided for each function in the lpSolve package using R's
built-in help system. For example, the command
> ?add.constraint
Page 3 of 6
01/04/15 12:48 am
> ?lp
> ?lp.assign
> ?lp.object
> ?lp.transport
> ?print.lp
Then we assign x to y.
> y <- x
C1
0
1
2
Real
Inf
0
C2
0
3
4
Real
Inf
0
free
free
0
0
The changes we made in x appear in y as well. Although x and y are two distinct
objects in R, they both refer to the same lp_solve 'lprec' structure.
The safest way to use the lpSolve API is inside an R function - do not return the
http://lpsolve.sourceforge.net/5.5/R.htm
Page 4 of 6
01/04/15 12:48 am
COLFOUR
0.1
2.9
0
0.9
Real
48.98
18
>=
<=
>=
92.3
14.8
4
Note that there are some commands that return an answer. For the accessor
functions (generally named get.*) the output should be clear. For other functions
(e.g., solve), the interpretation of the returned value is described in the
documentation. Since solve is generic in R, use the command
> ?solve.lpExtPtr
Page 5 of 6
01/04/15 12:48 am
INDICATES that the operation was successful. Invisible values can also be used
in flow control.
Cleaning up
See also Using lpsolve from MATLAB, Using lpsolve from O-Matrix, Using lpsolve
from Sysquake, Using lpsolve from Octave, Using lpsolve from FreeMat, Using
lpsolve from Euler, Using lpsolve from Python, Using lpsolve from Sage, Using
lpsolve from PHP, Using lpsolve from Scilab Using lpsolve from Microsoft Solver
Foundation
http://lpsolve.sourceforge.net/5.5/R.htm
Page 6 of 6