generateListenerExports: Build tables from messages saved by the listner

View source: R/Listeners.R

generateListenerExportsR Documentation

Build tables from messages saved by the listner

Description

The function updateTable extracts a data table from the listener named by which, saves it into the named file. It then registers the generated file using registerOutput.

The function generateListenerExports calls the updateTable for each element in the export list, which should be a list of arguments to updateTable.

Usage

generateListenerExports(ls, exportlist, appid, outdir, process = ls$sender)
updateTable(ls, which, type, appid, outdir, fname = "<app>_<name>.csv", 
            process = ls$sender, flattener = jsonlite::flatten, doc="",
            name=which) 

Arguments

ls

The ListenerSet which contains both the listener and the registry.

exportlist

A list of lists of arguments to updateTable.

appid

A character scalar giving the name of the application. This should be the long name (e.g., “ecd://org/unit/assessment” not the short name (“assessment”).

outdir

The path to the directory where the output should be stored.

process

A character scalare giving the name of the generating process. Passed to registerOutput.

which

An itentifier for which listener will generate the table, in other words, the name of one of the listeners.

type

A character string identifying the type of the output. Passed to registerOutput.

fname

A character vector giving a pattern for a file name. The string “<app>” is substituted for basename(app), the string “<name>” is substituted for name.

flattener

A function or string naming a function which is used to flatten nested data. See details.

name

Used to label the table in the registrity.

doc

A doc string added to the registrty.

Details

The updateTable function calls the listenerDataTable on the listener ls$listners[[which]]. As the details fields of the messages, could be nested, it might need to be flattened so that it can be exported as a CSV file, so the flattener function is called. Then the resulting data table is written out to outdir/fname.

The generateListenerExports is fed a list of arguments for updateTable. The idea is that this information can be included in the config.json file. Each element should be a list with the following components:

which

Required, the name of the listener.

type

Optional, the type of the output (for the registry); defaults to “data”.

name

Optional, the name of table in the registry. Defaults to which.

fname

Optional, the file name. This is actually a pattern, and “<app>” is replaced with basename(appid) and “<name>” is replaced with name. Default is “<app>_<name>.csv”.

flattener

Optional, The name of the flatterner function. Defaults to flatten.

doc

Optional, a character string describing the table in the registry.

Note that the appid, outdir and process fields are taken from the call to generateListenerExports.

Value

These functions are mainly used for their side effects. The updateTable function returns the generated table invisibly, or NULL if listenerDataTable returns NULL. The generateListenerExports returns the last exported table.

Flattening Complex Data

The data stored in the messages can in fact be nested deeply. So the raw dataframe returned by listenerDataTable could have columns that are themselves data frames. The function jsonlite::flatten function unrolls these columns into individual components.

Another frequenly used function is Peanut::flattenStats. In particular, the PnodeMargin statistic returns a labeled vector as output. This function splits it into columns with headers name.state. Note that to call a function from another package, that package must be named, so a call to require is in order.

Author(s)

Russell Almond

See Also

ListenerSet, Listener

listenerDataTable, registerOutput

flatten, flattenStats

Examples


## Not run: 

  config.json <-
    '"listeners":[
	{"name":"ToAS",
	 "type":"InjectionListener",
	 "dbname":"ASRecords",
	 "colname":"Statistics",
	 "jsonEncoder":"unparseData",
	 "jsonDecoder":"parseData",
	 "messages":["Statistics"]
	},
	{"name":"PPStats",
	 "type":"UpdateListener",
	 "targetField":"data",
	 "jsonEncoder":"stats2json",
	 "colname":"Statistics",
	 "messages":["Statistics"]
	}
	    
    ],
    "listenerExports":[
	{"which":"PPStats",
	 "type": "data",
	 "fname":"stats-<app>.csv",
	 "flattener":"flattenStats",
	 "doc": "Reporting statistics"
        },
	{"which":"ToAS",
	 "type": "hist",
	 "fname":"hist-<app>.csv",
	 "flattener":"flattenStats",
	 "doc": "History of history variables."
        }
    ]'
  config <- jsonlite::fromJSON(config.json,FALSE)
  appid <- "ecd://example.edu/testgroup/test"
  outdir <- tempdir()
  ls <- buildListenerSet("EA",config$listeners, appid,
                         lscol="Messages",dbname="test",
                         dburi=mongo::makeDBuri(),
                         sslops=mongolite::ssl_options(),
                         registrycol="files",registrydbname="test")
  ## Need to make sure Peanut::flattenStats is recognized
  require(Peanut)

  updateTable(ls,"PPstats","data",appid,outdir)

  generateListenerExports(ls,config$listenerExports,appid,outdir)


## End(Not run)


ralmond/Proc4 documentation built on June 13, 2025, 7:13 a.m.