Giter Site home page Giter Site logo

xl2gdx's Introduction

xl2gdx.R

R script to convert Excel to GDX:

  • Can replace GDXXRW for Excel-to-GDX conversion.
  • Accepts the same arguments and a subset of the options that GDXXRW does.
  • Unlike GDXXRW, works on non-Windows platforms and does not require Office.

Note

Consider alternatives first:

  • Excel files are not a good choice for storing data for automatic processing. If you have a choice, pick a more robust and standard data storage format.
  • With the release of GAMS 39, the GAMS Connect framework was introduced. It provides GAMS with an alternative way to read and convert Excel files in a cross-platform-compatible manner.
  • In the GAMS 41 release notes, GAMS Corp. announced that the gdxrrw package on which this script depends is deprecated.

Tests are located in the separate private xl2gdx-tests repository. That repository is private because the licensing conditions of the corner-case input Excel sheets included with the tests are diverse and were not reviewed. To request access to the tests repository, email the author or post an issue in the issue tracker.

For further information read the header comments in the script and see the GDXXRW documentation.

Installation

This is not an R package, instead xl2gdx.R is a utility script that can be invoked with command line parameters. Just copy it to a handy location. The same holds for the project_to_ASCII.R helper script. The dependencies listed below should first be installed though, and some environment variable may need to be set.

Carefully read the instructions below, heed the warnings, and follow the links where needed.

Dependencies

R

The R language runtime is required to run xl2gdx.R. After installation, ensure that R and Rscript are on-path by adding the right installation subdirectory to the PATH environment variable. On Windows, this directory ends in R-x.y.z\bin\x64 for the 64-bit binaries, where x.y.z is the R version.

Warning

When installing R, old R versions are not automatically removed. Having multiple R versions installed can cause confusion. Remove any older R installations unless you have good reasons to keep it. Be sure to delete any references to a removed R installation as could be present in the PATH, R_GAMS_SYSDIR, and other environment variables.

Note

After updating R, you will need re-install R packages and update the environment variables that point to the R installation directory since the path includes the version number.

Important

When you use RStudio and update R, you should make sure that RStudio is using the new R installation by configuring it under Tools >> Global options ... >> General.

Tidyverse

The tidyverse curated R package collection must be installed. You can do so from the R prompt by issuing:

install.packages("tidyverse")

Alternatively, you can use the RStudio package management tab, but make sure that RStudio is using the on-path R installation.

gdxrrw

gdxrrw is an R package for reading/writing GDX files from R. It is required by xl2gdx.R. You cannot install it from CRAN, you have to install it from GitHub as per these instructions. To make gdxrrw find the GAMS system directory containing the GDX libraries that it needs to read/write GDX files, you can use the sysdir command line option (see below) or make sure a sufficiently recent GAMS installation directory is included in either the PATH (on Windows), or LD_LIBRARY_PATH (on Linux), or DYLD_LIBRARY_PATH (on MacOS) environment variable.

However, it is strongly recommended to instead make your environment configuration explicit and purpose-specific by setting the gdxrrw-dedicated environment variable R_GAMS_SYSDIR to point to a GAMS installation directory. For reasons explained below, it is best to point to the most recent version of GAMS that you have installed. See here for guidance on how to set environment variables.

Warning

Changed environment variables are not picked up until you restart a process. Therefore, after changing one of the above-mentioned environment variables, first restart your command prompt, shell, GAMS IDE or GAMS Studio before testing the installation or invoking xl2gdx.R.

If you use an environment variable to point to the GAMS installation directory, the following should work and report the used environment variable:

$ R
> library(gdxrrw)
> igdx(gamsSysDir='')

Warning

The above will result in an error with recent versions of gdxrrw unless you point gdxxrrw at a GAMS 33 or newer installation directory as per the above instructions. The reason is that gdxrrw has switched to using an improved GDX API that is available as of GAMS 33. You may therefore need to install a newer GAMS version and point gdxrrw at it.

Tip

On Windows, it will likely prevent problems when you first install Rtools so that you can compile the gdxrrw and other R packages from source.

Caution

When installing Rtools 4.0 (the version compatible with R 4.0.x or 4.1.y), make sure to not skip the Putting Rtools on the PATH step listed in its installation instructions. Later versions of Rtools do not require this step.

Note

On Windows without Rtools, you should download a binary gdxrrw package that matches your R version. For a list of which binary package versions match what R versions, see the gdxrrw wiki.

project_to_ASCII.R depends on:

  • The tidyverse curated R package collection.

Usage

Both xl2gdx.R and project_to_ASCII.R can be invoked via the Rscript utility that is part of the R installation. When you followed the instructions in the prior section, the directory containing the Rscript utility will in your PATH environment variable so that you can use Rscript from a command prompt or shell.

xl2gdx.R

To invoke xl2gdx.R from the command line or shell, issue:

[Rscript ]xl2gdx.R <Excel file> [options] [@<options file>] [symbols]

The supported options are listed below. Details for most options are given in the GDXXRW manual.

On Linux/MacOS, you can omit the leading Rscript since Rscript will invoked automatically via the shebang header present in the script.

When replacing a GDXXRW invocation in your GAMS code with xl2gdx.R, you will typically have a $call or execute statement that invokes GDXXRW. Unless unsupported options are used, it should be possible to replace the GDXXRW or <path to GAMS dir>/GDXXRW part of that invocation with Rscript <relative path to xl2gdx.R>/xl2gdx.R and things should work. To verify, the output of both invocations can be compared with GDXDIFF.

Global options (provide these first):

  • output=<GDX file> If omitted, output to <Excel file> but with a .gdx extension. The synonym o is supported, but its use is discouraged.
  • index='<sheet>!<start_colrow>'
  • squeeze=<Y|N> If Y, do not write zero values. Defaults to Y.
  • sysdir=<GAMS system directory> When omitted, the GAMS installation directory must be reachable via an environment variable (see above).
  • maxdupeerrors=<max>

Symbol options (one or more):

  • dset=<name of domain set to write>
  • par=<name of parameter to write>
  • set=<name of set to write>

Symbol attribute options (associated with preceeding symbol):

  • cdim=<number of column dimensions>
  • rdim=<number of row dimensions>
  • rng='[<sheet>!]<start_colrow>[:<end_colrow>]' โš ๏ธ Unlike GDXXRW sheet names are case sensitive.
  • project=<Y|N> Project Latin special characters to ASCII for par= symbols. Defaults to N.

project_to_ASCII.R

Project a windows-1252 or ISO-8859-1 encoded text file to ASCII. Intended to remove special characters from source files. Can be used in conjunction with he project=Y feature of xl2gdx.R to locate and convert special-character references to data in GAMS source files.

Caution

This tool operates in-place, apply it only to source files under version control so that you can review and revert the changes.

To invoke project_to_ASCII.R, issue:

[Rscript ]project_to_ASCII.R <text file with special characters>

This projects the given text file to ASCII when possible, replacing it in-place.

Troubleshooting

Error: function 'Rcpp_precious_remove' not provided by package 'Rcpp'

When using xl2gdx.R produces this error, upgrade the Rcpp package to version 1.0.7 or higher.

xl2gdx's People

Contributors

acjbrouwer avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

s-spillias

xl2gdx's Issues

script doesn't read 0

The script doesn't read a table entry 0. When I read this table from the xlsx file, only the first 6 rows are read. Converting a 0 to a nonzero number changes this behavior.

HRP	HUP	HRR	HUR	FIRM

frisch -1.5 -1.5 -1.5 -1.5
AGR 0.7 0.7 0.7 0.7
FOOD 1.1 1.1 1.1 1.1
OTHIND 1.1 1.1 1.1 1.1
SER 1.05 1.05 1.05 1.05
ADM 1.05 1.05 1.05 1.05
sh0O 0 0 0 0
tr0O 0 0 0 0
ttdh0O 0 0 0 0
ttdf0O 0

This the same as the issue discussed in Example 4 - Reading a GDX File .
The squeeze option takes care of this in gdxxrw.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.