| Title: | Manipulate 'GDX' Files |
|---|---|
| Description: | Read and write 'GDX' files ('GAMS' data exchange) and convert parameters, sets and variables to data frames. Backed by the 'GAMS'-maintained 'gamstransfer' package; no compiled code is shipped. See <https://www.gams.com/latest/docs/UG_GDX.html> for the 'GDX' format. |
| Authors: | Laurent Drouet [aut, cre] |
| Maintainer: | Laurent Drouet <[email protected]> |
| License: | EPL-1.0 |
| Version: | 1.1.1 |
| Built: | 2026-05-29 08:10:50 UTC |
| Source: | https://github.com/lolow/gdxtools |
Return the list of all items
all_items(x, ...)all_items(x, ...)
x |
the gdx object |
... |
ignored |
A named list with four character vectors of symbol names:
variables, parameters, sets and equations.
Laurent Drouet
Extract a list of items from many GDX
batch_extract(items, files = NULL, gdxs = NULL, ...)batch_extract(items, files = NULL, gdxs = NULL, ...)
items |
vector or list of items to extract |
files |
list of files; if |
gdxs |
list of |
... |
passed to |
A named list with one data.frame per requested item,
each the row-bind of that item extracted from every file with a
gdx column identifying the source file.
Laurent Drouet
f1 <- tempfile(fileext = ".gdx") f2 <- tempfile(fileext = ".gdx") write.gdx(f1, list(myparam = data.frame(i = c("a", "b"), value = 1:2))) write.gdx(f2, list(myparam = data.frame(i = c("a", "b"), value = 3:4))) allparam <- batch_extract("myparam", files = c(f1, f2))f1 <- tempfile(fileext = ".gdx") f2 <- tempfile(fileext = ".gdx") write.gdx(f1, list(myparam = data.frame(i = c("a", "b"), value = 1:2))) write.gdx(f2, list(myparam = data.frame(i = c("a", "b"), value = 3:4))) allparam <- batch_extract("myparam", files = c(f1, f2))
Extract data from a gdx
extract(x, ...)extract(x, ...)
x |
the gdx object |
... |
arguments passed to |
A data.frame; see extract.gdx for the column
layout and attributes.
Extract parameter or variable data from the gdx file
## S3 method for class 'gdx' extract(x, item, field = "l", addgdx = FALSE, ...)## S3 method for class 'gdx' extract(x, item, field = "l", addgdx = FALSE, ...)
x |
the gdx object |
item |
the name of the item to extract |
field |
the field of the variable to be extracted. Can be 'l', 'm', 'lo', 'up' (level, marginal, lower, upper). Defaults to level. |
addgdx |
if |
... |
ignored; for backward compatibility. |
A data.frame with one character column per domain index plus
a numeric value column (parameters, variables and equations); sets
have no value column. A gams attribute carries the symbol's
description text, and addgdx = TRUE adds a gdx column with
the source filename.
Laurent Drouet
f <- tempfile(fileext = ".gdx") write.gdx(f, list(travel_cost = data.frame(city = c("paris", "lyon"), value = c(12, 7)))) mygdx <- gdx(f) travel_cost <- mygdx["travel_cost"] travel_cost <- extract(mygdx, "travel_cost")f <- tempfile(fileext = ".gdx") write.gdx(f, list(travel_cost = data.frame(city = c("paris", "lyon"), value = c(12, 7)))) mygdx <- gdx(f) travel_cost <- mygdx["travel_cost"] travel_cost <- extract(mygdx, "travel_cost")
Thin wrapper around system2("gams", ...). Returns the integer exit
status from GAMS.
gams(gmsAndArgs)gams(gmsAndArgs)
gmsAndArgs |
command-line string passed to |
exit status (integer)
Constructs a gdx object backed by a gamstransfer::Container.
By default the file is opened in lazy mode: only symbol metadata
(names, dimensions, domains, descriptions) is read up front. Each symbol's
records are loaded on first access via [.gdx / extract() and
then cached on the container for subsequent calls. Pass lazy = FALSE
to read everything eagerly (legacy 1.0.0 behavior).
gdx(filename, lazy = TRUE, ...)gdx(filename, lazy = TRUE, ...)
filename |
filename of the gdx file |
lazy |
if |
... |
extra fields stored on the resulting object |
An object of class gdx: an S3 list holding the live
gamstransfer::Container(s) and per-type symbol metadata
(variables, parameters, sets, equations
data.frames with name, text and dim columns).
Laurent Drouet
f <- tempfile(fileext = ".gdx") write.gdx(f, list(demand = data.frame(city = c("paris", "lyon"), value = c(50, 20)))) mygdx <- gdx(f) # lazy by default mygdx["demand"] # triggers a targeted read eager <- gdx(f, lazy = FALSE)f <- tempfile(fileext = ".gdx") write.gdx(f, list(demand = data.frame(city = c("paris", "lyon"), value = c(50, 20)))) mygdx <- gdx(f) # lazy by default mygdx["demand"] # triggers a targeted read eager <- gdx(f, lazy = FALSE)
Pre-1.0 gdxtools relied on the gdxrrw API and required the user to point
it at a GAMS install. The gamstransfer backend is self-contained and does
not need this. igdx now simply discovers gams on the PATH
(or uses the directory the user supplies) and returns it; calling it has
no side effect on gdx I/O.
igdx(gamsSysDir = NULL, silent = FALSE, returnStr = FALSE)igdx(gamsSysDir = NULL, silent = FALSE, returnStr = FALSE)
gamsSysDir |
path to the GAMS system directory (optional) |
silent |
if |
returnStr |
if |
the discovered GAMS system directory, or "" if none found
Lazy-opened gdx objects defer reading records until first access. When you
know up front which symbols you will use (or you want to amortize the I/O
cost), call load_records() to read them in a single batched
gamstransfer::Container$read() call.
load_records(x, symbols = NULL)load_records(x, symbols = NULL)
x |
a |
symbols |
character vector of symbol names; |
x invisibly. Records are stored on the underlying container.
Laurent Drouet
f <- tempfile(fileext = ".gdx") write.gdx(f, list(a = data.frame(i = c("x", "y"), value = 1:2), b = data.frame(i = c("x", "y"), value = 3:4))) g <- gdx(f) load_records(g, c("a", "b")) g["a"] # already cached, no I/Of <- tempfile(fileext = ".gdx") write.gdx(f, list(a = data.frame(i = c("x", "y"), value = 1:2), b = data.frame(i = c("x", "y"), value = 3:4))) g <- gdx(f) load_records(g, c("a", "b")) g["a"] # already cached, no I/O
Builds a gamstransfer::Container from the supplied data and writes it
to file. Variables can be given as separate level / lower / upper
data.frames (keyed by the same variable name across the three lists);
missing entries fall back to the gamstransfer defaults (level 0,
lower -Inf, upper +Inf for free variables).
write.gdx( file, params = list(), vars_l = list(), vars_lo = list(), vars_up = list(), sets = list(), removeLST = TRUE, usetempdir = TRUE, digits = 16, compress = FALSE, na = c("drop", "keep", "error"), dup = c("first", "last", "error") )write.gdx( file, params = list(), vars_l = list(), vars_lo = list(), vars_up = list(), sets = list(), removeLST = TRUE, usetempdir = TRUE, digits = 16, compress = FALSE, na = c("drop", "keep", "error"), dup = c("first", "last", "error") )
file |
the output gdx filename |
params |
named list of parameter data.frames |
vars_l |
named list of variable level data.frames |
vars_lo |
named list of variable lower-bound data.frames |
vars_up |
named list of variable upper-bound data.frames |
sets |
named list of set data.frames |
removeLST |
kept for backward compatibility; ignored. |
usetempdir |
kept for backward compatibility; ignored. |
digits |
kept for backward compatibility; ignored (gamstransfer preserves full numeric precision). |
compress |
when |
na |
how to handle NA / NaN values in parameter |
dup |
how to collapse duplicate index keys: |
Invisibly returns 0; called for the side effect of writing
the gdx file at file.
Laurent Drouet
param1 <- data.frame(x = c('1', '2'), value = 1:2) param2 <- data.frame(a = c('london', 'paris'), value = c(50, 0.2)) write.gdx(tempfile(fileext = ".gdx"), list(param1 = param1, param2 = param2))param1 <- data.frame(x = c('1', '2'), value = 1:2) param2 <- data.frame(a = c('london', 'paris'), value = c(50, 0.2)) write.gdx(tempfile(fileext = ".gdx"), list(param1 = param1, param2 = param2))
Historically this was a faster path that bypassed the GAMS process used by
the legacy write.gdx. With the gamstransfer backend both functions
use the same fast path; this entry point is kept for code that calls it
explicitly.
write2.gdx( file, params = list(), sets = list(), na = c("drop", "keep", "error"), dup = c("first", "last", "error") )write2.gdx( file, params = list(), sets = list(), na = c("drop", "keep", "error"), dup = c("first", "last", "error") )
file |
the output gdx filename |
params |
named list of parameter data.frames |
sets |
named list of set data.frames |
na |
how to handle NA / NaN values; see |
dup |
how to collapse duplicate index keys; see |
Invisibly returns 0; called for the side effect of writing
the gdx file at file.
Laurent Drouet