R language environment and shared libraries

This example uses the following file name: libenv.R.

Code

The environment and shared library classes provide information about the environment and libraries that are associated with an AE. This example AE takes one argument, an integer. If the integer is equal to 0, the AE outputs the environment that it sees. If the integer is equal 1, the AE outputs the libraries that it sees.

Enter the following code in the /tmp/libenv.R file:
nz.fun <- function () {
getNext() # this can be called only once
input <- getInputColumn(0)
if (input == 1) {
apply(getLibraryInfo(), 1, function(ROW) {
setOutput(0, ROW[1])
setOutput(1, ROW[2])
outputResult()
})
}
else if (input == 0) {
entry <- getFirstEnvironmentEntry()
while (!is.null(entry)) {
setOutput(0, entry[1])
setOutput(1, entry[2])
outputResult()
entry <- getNextEnvironmentEntry()
}
}
else
stop('incorrect input: ', input)
}

Compilation

From the directory where the source file is, compile the code by using the udtf template:
/nz/export/ae/utilities/bin/compile_ae --language r --template compile \
--version 3 --db dev --user nz libenv.R

Registration

Register the example:
/nz/export/ae/utilities/bin/register_ae --language r --version 3 \
--template udtf --exe libenv.R --sig "lib_env(INT4)" \
--return "TABLE(name VARCHAR(1000), value VARCHAR(1000))" \
--db dev --user nz
Given a positive integer value, the second argument acts as a limit to the number of results returned. This is used in place of keyword LIMIT to exit the AE cleanly when the number of results returned must be limited.

Running

SELECT * FROM TABLE WITH FINAL(lib_env(1));
NAME | VALUE
-----------------------------
+-----------------------------------------------------------------
INZA....LIBNZAEADAPTERS |
/nz/data.1.0/base/202701/library/202809/host/libnzaeadapters.so
INZA....LIBNZAECHILD |
/nz/data.1.0/base/202701/library/202811/host/libnzaechild.so
INZA....LIBNZAEPARENT |
/nz/data.1.0/base/202701/library/202808/host/libnzaeparent.so
(3 rows)
SELECT * FROM TABLE WITH FINAL(lib_env(0)) limit 5;
NAME | VALUE
------------------+-------------------------------------------
R_LIBS_SITE |
R_SESSION_TMPDIR | /tmp/Rtmpp0196y
R_LIBS_USER | ~/R/x86_64-unknown-linux-gnu-library/2.11
TAR | /bin/gtar
TR | /usr/bin/tr
(5 rows)