Tools for tidy interactions between R and GAMS
# install.packages("remotes")
remotes::install_github("christophe-gouel/gamsr");gamsr is just an opinionated interface to GAMS original package gamstransfer. gamstransfer is an R package to read and write GAMS gdx files. It is very complete but also a bit complex when one just wants to quickly read from one or several gdx files. Hence, this package.
Read gdx files
fpath <- system.file("extdata", "trnsport.gdx", package = "gamsr")
read_gdx(fpath, "a") # as tibble
read_gdx(fpath, "a", data_type = "dt") # as data.table
read_gdx(fpath) # Return a data.frame with the list of available symbolsLaunch GAMS from R
fpath <- file.path(where_is_gams(), "gamslib_ml", "trnsport.1")
gams(fpath, options = list(output = "NUL", lp = "cplex"))Parallel launch using furrr package and read all the gdx files at once
# install.packages("furrr")
furrr::future_walk(
1:10,
\(num) gams(fpath, options = list(lo = 0, output = "NUL", lp = "cplex"),
gdx = paste0(num, ".gdx"))
)
read_gdx(paste0(1:10, ".gdx"), "a")Write data to a gms file
df <- data.frame(
i = c(rep("seattle", 3), rep("san-diego", 3)),
j = rep(c("new-york", "chicago", "topeka"), 2),
value = c(2.5, 1.7, 1.8, 2.5, 1.8, 1.4)
)
write_gms(df, "distance.gms")This version of gamsr uses GAMS Transfer R as a backend. GAMS Transfer R has been introduced with GAMS 40.1.0. Previous GAMS releases used GDXRRW to interact with R. The last version of gamsr compatible with GDXRRW can be found in commit 17470d3.
For use with GAMS >= 45, at least release 1.2 should be used because of a change in GAMS Transfer R API.