Skip to content

R specific

Cristian Lussana edited this page May 11, 2022 · 7 revisions

After installing the package, run the following code in R to have access to all the functions of titanlib:

titanlib_path <- "your-own-path/titanlib/build/extras"
dyn.load( file.path( titanlib_path,
                     paste("SWIG/R/titanlib", .Platform$dynlib.ext, sep="")))
source(   file.path( titanlib_path,"SWIG/R/titanlib.R"))

Read the header file documentation to see available functions. All functions available in the C++ library are also made available in R, with the same function signature. For example, the C++ function:

vec2 range_check(const vec &values, const vec &min, const vec& max)

can be used in R as follows:

titanlib_path <- "/home/cristianl/projects/titanlib/build/extras"
dyn.load( file.path( titanlib_path, paste( "SWIG/R/titanlib", .Platform$dynlib.ext, sep="")))
source( file.path( titanlib_path, "SWIG/R/titanlib.R"))
range_check( as.numeric(1:10), 5, 10)

Values are returned as integers in a vector. When a function is expected to return more than one element, then they are returned as elements of a list. In that case, if the results are stored in the variable res , then it is possible to access them with the syntax res[[1]] for the first vector/value, res[[2]] for the second and so on.

Installation tips and tricks

  • Problem when loading titanlib with Rscript. The package methods is needed to execute dyn.load. When using R to run the script, methods is among the default libraries. However, when using Rscript methods is not loaded by default. Then you need to do it. One way is to set the environmental variable R_DEFAULT_PACKAGES as follows:

    export R_DEFAULT_PACKAGES="datasets","utils","grDevices","graphics","stats","methods"
    
  • Bug in SWIG Version 2.0.10. When using this version to create titanlib.R, I had to manually comment out all the lines with:

    ans <- new("_p_std__vectorT_int_std__allocatorT_int_t_t", ref=ans)
    

    The issue was not present with SWIG Version 3.0.12.

  • if you are installing titanlib on a system where you do not have super-user access try something like this (which works on MET Norway's PPI):

    #!/bin/bash
    cd titanlib
    mkdir build
    cd build
    module load R/R-3.5.2
    module load boost/1.69.0
    module load cmake
    cmake .. -DCMAKE_INSTALL_PREFIX=/home/cristianl/.local/lib -DGSL_INCLUDE_DIR=/modules/centos7/gsl/2.5/include -DGSL_LIBRARY=/modules/centos7/gsl/2.5/lib/libgsl.so -DGSL_CBLAS_LIBRARY=/modules/centos7/gsl/2.5/lib/libgslcblas.so
    make install
    make build-r
    

Setup for examples

To get ready for the examples in the next sections, run the code below to set up necessary variables.

titanlib_path <- "your-own-path/titanlib/build/extras"
dyn.load( file.path( titanlib_path,
                     paste("SWIG/R/titanlib", .Platform$dynlib.ext, sep="")))
source(   file.path( titanlib_path,"SWIG/R/titanlib.R"))"))
library(ncdf4)
nc <- nc_open("obs.nc")
for (i in 1:nc$nvars) {
  if ( nc$var[[i]]$name == "latitude") lats <- ncvar_get( nc, nc$var[[i]])
  if ( nc$var[[i]]$name == "longitude") lons <- ncvar_get( nc, nc$var[[i]])
  if ( nc$var[[i]]$name == "altitude") elevs <- ncvar_get( nc, nc$var[[i]])
  if ( nc$var[[i]]$name == "air_temperature_2m") temp_obs <- ncvar_get( nc, nc$var[[i]])
  if ( nc$var[[i]]$name == "precipitation_amount") precip_obs <- ncvar_get( nc, nc$var[[i]])
}
nc_close(nc)