rgdx: Read data from GDX into R

View source: R/gdxrrw.R

rgdxR Documentation

Read data from GDX into R

Description

Read one data item (also called a symbol) from GDX into R, returning it as a list. Note that GDX files contain multiple symbols (e.g. sets, parameters). Each symbol is read with a separate call.

Usage

  # generic form - return a symbol as a list
  rgdx(gdxName, requestList = NULL, squeeze = TRUE, useDomInfo = TRUE,
       followAlias = TRUE)

  # return a set or parameter in a data frame
  rgdx.set(gdxName, symName, names=NULL, compress=FALSE, ts=FALSE,
           useDomInfo = TRUE, check.names = TRUE, te = FALSE)
  rgdx.param(gdxName, symName, names=NULL, compress=FALSE, ts=FALSE,
             squeeze=TRUE, useDomInfo = TRUE, check.names = TRUE)

  # return a scalar
  rgdx.scalar(gdxName, symName, ts=FALSE)

Arguments

gdxName

the name of the GDX file to read

requestList

the name of the symbol to read, and (optionally) information about how much information to return and in what format. This argument must be a named list. If omitted, the universe of UELs contained in the GDX file is returned

squeeze

if TRUE/nonzero, squeeze out any zero or EPS stored in the GDX container

useDomInfo

if TRUE, the default filter will be the domain info in the GDX. If no domain info is available, or if useDomInfo=FALSE, the default filter will be the GDX universe

followAlias

if TRUE and the symbol queried is an alias, return information for the real set rather than the alias

check.names

If TRUE then the names of the variables in the data frame are checked to ensure that they are syntactically valid variable names and are not duplicated. If necessary they are adjusted (by make.names so that they are

symName

the name of the GDX symbol to read

names

the column names to use in the data frame returned

compress

if TRUE, compress the factors in the data frame so they only include required levels. For the default compress=FALSE, each factor includes levels for the entire universe of UELs in the GDX file

ts

if TRUE, include the .ts field (i.e. the explanatory text) for the symbol in the return

te

if TRUE, include the associated text (i.e. the .te field) for each set element in the return

Details

The requestList argument to rgdx is essentially a list of arguments specifying what symbol to read and how to read it. Valid list elements are:

name

name of symbol to read from GDX

form

specify representation to use on return: “sparse” (default) or “full”

uels

UEL filter to use when reading

field

specify field to read for equations and variables

te

if true, return the associated text (i.e. the .te field) for each set element in the return

ts

if true, include the explanatory text (i.e. the .ts field) for the symbol in the return

compress

if true, compress UEL lists of return value by removing unused elements in each index position

dim

expected dimension of symbol to be read

When reading sets, one can specify that the associated text is included in the return value. The value returned for set elements where no associated text exists is controlled via the options() mechanism. Setting options(gdx.inventSetText=NA) (the default) returns NA, setting options(gdx.inventSetText=T) returns a string made up from the UEL(s), and setting options(gdx.inventSetText=F) returns an empty string "".

When reading GDX data into data frames (e.g. with rgdx.param), the names() (i.e. the column names) of the output data frame can be passed in via the optional names argument. If not, then the names are either created internally or taken from the domain information and name of the symbol in question. The latter choice is controlled by setting options(gdx.domainNames=FALSE) to use internally generated names for the data frame columns (e.g. "i","j" or "i1","i2","i3","i4"). A setting of TRUE (the default) means use the domain names from the GDX file for the column names.

Value

By default, the return value is a list with elements describing the data item or symbol returned. Elements include:

name

symbol name

type

symbol's data type: set, parameter, variable or equation

dim

symbol dimension

val

array containing the symbol data

form

form of the data in val, i.e. full or sparse

uels

vector of UEL lists, one list per symbol dimension

domains

character vector of length dim containing the symbol's domain info

te

(optional) associated text for sets

The functions rgdx.param and rgdx.set are special-purpose wrappers that read parameters and sets, respectively, and return them as data frames.

The function rgdx.scalar returns a scalar (i.e. a 0-dimensional parameter) as a double.

Note

A common problem is failure to load the external GDX libraries that are required to interface with GDX data. Use igdx to troubleshoot and solve this problem.

Author(s)

Original coding by Rishabh Jain. Adopted and packaged by Steve Dirkse. Maintainer: R@gams.com

See Also

igdx, wgdx, gdxInfo

Examples

  # run R-script trnsport.r from the data subdirectory of the gdxrrw package
  # to load up some data for writing to a GDX file
  data(trnsport)

  wgdx("rgdx1",sf,si,sj,sa,sb,sd)
  ou = rgdx("rgdx1")
  req <- list(name="f")
  of = rgdx("rgdx1",req)
  req <- list(name="i")
  oi = rgdx("rgdx1",req)
  req <- list(name="j")
  oj = rgdx("rgdx1",req)
  req <- list(name="a")
  oa = rgdx("rgdx1",req)
  req <- list(name="b")
  ob = rgdx("rgdx1",req)
  req <- list(name="d")
  od = rgdx("rgdx1",req)

  ## Not run: 
    # complete tests and examples can be run in the
    # extdata directory of the gdxrrw package
    # check .libPaths for a hint on where packages are installed
    setwd(paste(.libPaths()[1],"/gdxrrw/extdata",sep=""))
    source("tAll.R")
  
## End(Not run)

GAMS-dev/gdxrrw-miro documentation built on July 11, 2024, 4:39 p.m.