Monday, December 21, 2020

GAMS + R Markdown scripting

In [1] I discussed a small script that exported GAMS data to R for plotting. Here I expand on that and show how we can use R Markdown [2] to generate documents that contain GAMS data and results. Here we have just a small example with just a parameter. But it is easy to see that this can also be used with larger models, where we have a document built around more complex model results. 

R Markdown can handle as input text, R code, R graphics and LaTeX math (among others). Output can be HTML, PDF (via LaTeX), and DOCX (MS Word).

Here is my example:


$ontext

  
Generate data in GAMS and produce a document in Rmarkdown.

$offtext

*---------------------------------------------------
* generate some random data points
*---------------------------------------------------

set
   i
'points' /p1*p70/
   c
'coordinates' /x,y/
;

parameter p(i,c) 'points';
p(i,c) = uniform(0,100);
display p;


*---------------------------------------------------
* make document
*---------------------------------------------------

* GAMS macros
$set script     script.R
$set gdx        data.gdx
$set doc_in     document.Rmd
* Output file is .html,.pdf, or .docx
$set doc_out    document.pdf
$set plottitle    data points
$set rcommand   '"c:\Program Files\R\R-4.0.3\bin\Rscript.exe" --vanilla %script%'
$set pandocdir  c:/Program Files/rstudio/bin/pandoc

* create gdx file with data
execute_unload "%gdx%",p;

* run the R script
execute '%rcommand%';

* launch a pdf viewer to show the result
execute 'shellexecute %doc_out%';


*---------------------------------------------------
* R script
*---------------------------------------------------
$onecho > %script%
options(echo=TRUE)
library(rmarkdown)
rmarkdown::find_pandoc(dir="%pandocdir%")
rmarkdown::pandoc_version()
format = switch(tools::file_ext("%doc_out%"),
             
"html" = "html_document",
             
"docx" = "word_document",
             
"pdf"  = "pdf_document")
if (is.null(format)) stop("unknown output format")
rmarkdown::render("%doc_in%",
                 
output_file="%doc_out%",
                 
output_format = format)
$offecho



*---------------------------------------------------
* RMarkdown document
*---------------------------------------------------
$onecho > %doc_in%
---
title: "Small demo"
author: "Erwin Kalvelagen"
date: "%system.date%"
output:
 
pdf_document: default
 
html_document: default
 
word_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(tidyverse)  # includes ggplot
library(pander)     # tables
library(gdxrrw)     # read gdx file
```

In this example, we read data from GAMS. The data is from the uniform
distribution:
 
$$
   
p_i \sim U(0,100)
 
$$

A plot can look like this:

```{r plot, echo=FALSE}
# read parameter p from GDX file
p <- rgdx.param("%gdx%","p")

# convert from long to wide format. x,y as columns.
p2 <- pivot_wider(p,id_cols=i,names_from=c,values_from=p)

ggplot(p2, aes(x=x,y=y)) + geom_point() + ggtitle("%plottitle%")
```

There are facilities to create good looking tables.

```{r table1, echo=FALSE, message=FALSE, warnings=FALSE, results='asis'}
set.caption("A few records from the GAMS parameter")
pander(head(p2))
```
$offecho


This GAMS file has three parts:

  • GAMS code to generate some data and to execute the R script
  • R code to run the R Markdown process
  • R Markdown code

Notes

  1. pandoc is a document translation system used by markdown. It comes with RStudio. That is the version I am using here.
  2. The output type is determined by the extension specified in doc_out.
  3. rmarkdown::render is the workhorse of the document creation process.
  4. After generating the document, it will be opened by shellexecute.

The LaTeX/pdf output looks like:


The advantage of this setup is that we can produce good looking reports basically automatically. Just run the GAMS model and get a report with the results.


References


  1. GAMS + R scripting, http://yetanothermathprogrammingconsultant.blogspot.com/2020/12/gams-r-scripting.html
  2. Yihui Xie and J.J. Allaire and Garrett Grolemund (2018). R Markdown: The Definitive Guide. Chapman and Hall/CRC.

No comments:

Post a Comment