-
Notifications
You must be signed in to change notification settings - Fork 7
Working with the R API
In some cases it is useful to interact with the VTL engine directly from R code. For example you may want to include a VTL transformation in your R program that performs many other tasks that are not related to VTL. In the following R program we show how to submit and compile a VTL transformation from R to the engine and how to retrieve the results back into R.
# load the RVTL package
library(RVTL)
# Create a local data.frame in the global environment
rand = rnorm(n = length(letters), mean = 0)
ds1=data.frame(l=letters, # character
n=as.integer(1:length(letters)), # integer
m=rand, # numeric
b=rand > 0, # boolean
d=as.Date('2020-01-01') + 1:length(letters)) # date
# since we are going to leverage the R environment (which is enabled by default),
# we set some metadata to instruct the engine about the roles of the components
attr(ds1, 'identifiers') <- c('l', 'n')
attr(ds1, 'measures') <- c('m')
# we submit a simple transformation to the engine,
# filtering out all the negative values in component m
vtl_code = paste('ds2:=ds1;',
'ds3:=ds2[filter m > 0];')
vtlAddStatements('test', vtl_code)
# we compile the transformation
vtlCompile('test')
# the transformation will be executed as soon as we retrieve a dataset
ds3=vtlEvalNodes('test', 'ds3')[['ds3']]
head(ds3)
Now, in R, we have a ds3 data.frame that is the subset of ds1.

You can use R code to execute VTL transformation on any kind of data and metadata that is available in the engine (SDMX, CSV, JSON), provided that you have correctly configured the engine environments and repositories as explained in the previous sections.
A detailed list of R functions provided by the RVTL package can be checked typing
help(vtlStudio)
and selecting the index page.