Usage in R via reticulate
message_ix
and ixmp
are fully usable in R via reticulate, a package that allows nearly seamless access to the Python API.
See Installation for instructions on installing R and reticulate.
No additional R packages are needed. [1]
Once installed, use reticulate to import the Python packages:
library(reticulate)
ixmp <- import("ixmp")
message_ix <- import("message_ix")
This creates two global variables, ixmp
and message_ix
, that can be used much like the Python modules:
mp <- ixmp$Platform(name = "default")
scen <- message_ix$Scenario(mp, "model name", "scenario name")
# etc.
See the R versions of the Austria tutorials for full examples of building models.
Some tips:
If using Anaconda, you may need to direct reticulate to use the Python executable from the same conda environment where
message_ix
andixmp
are installed. See the reticulate documentation for usage of commands like:# Specify python binaries and environment under which messageix is installed use_condaenv("message_env") # or use_python("C:/.../anaconda3/envs/message_env/")
As shown above, R uses the
$
character instead of.
to access methods and properties of objects. Where Python code examples show, for instance,scen.add_par(...)
, R code should instead usescen$add_par(...)
.MESSAGEix model parameters with dimensions indexed by the
year
set (e.g. dimensions namedyear_act
oryear_vtg
) must be indexed by integers; but R treats numeric literals as floating point values. Therefore, instead of:ya1 = 2010 ya2 = c(2020, 2030, 2040) ya3 = seq(2050, 2100, 10) # ...store parameter data using year_act = ya1, ya2, or ya3
…use
as.integer()
to convert:ya1 = as.integer(2010) ya2 = sapply(c(2020, 2030, 2040), as.integer) ya3 = as.integer(seq(2050, 2100, 10))