RCaller

Executes an R script that has the ability to access feature data from a temporary R data frame. Input data is set up in the form of tables that will become R data frames. R data frames are tables similar to those of a relational database that support columns of varying types. More information on R data frames can be found at:

http://www.r-tutor.com/r-introduction/data-frame

This transformer requires that the system has R and the sqldf package installed in order to run. See Installing R in the Usage Notes section.

Any number of input data frames can be created, and each will be assigned an input port. Any features can be routed to that input port as long as they supply values for each column defined for the table. The R Script can involve any and all data frames and columns defined in the input. Output is taken from the fmeOutput data frame that the user can populate with the results of statistical analysis on any of the input tables.

Any number of input ports can be created either by connecting to the Connect Input port or by editing the transformer properties and manually adding new inputs or by importing port definitions from existing feature types. Once imported the table definitions will not automatically change as their source changes, in the event an attribute name is changed upstream the name of the corresponding table column will need to be manually adjusted in the table parameters. Users will need to manually expose the output attributes which will be imported from the column names of the fmeOutput data frame at runtime.

The success of the translation relies on the user supplying a valid R Script that adheres to proper R syntax. A guide on the R Language is listed below:

https://cran.r-project.org/doc/manuals/r-release/R-lang.html

To learn more about how to use R and to get ideas for different types of statistical analysis that may be possible, the following links are recommended:

http://www.r-bloggers.com/how-to-learn-r-2/

http://www.r-tutor.com/r-introduction

Parameters

Inputs

The RCaller requires definition of one or more Tables, which will become input ports to the transformer. The Import… button provides a quick way to populate the input table definitions from the source feature types in the workspace.

Note: For performance reasons, you should define as few columns as possible.

Note that certain FME attribute and table names may not be valid in R as data frame or column names (notably attribute names starting with an underscore "_"). To avoid issues, these names will be converted to valid R names. The adjusted names will be shown in the Data frames section on the left of the script editor.

R Script

The RCaller has a single output port. Attributes created in the script need to be entered by the user in the Attributes to Expose parameter in order to have these appear in subsequent transformers or the data inspector table view. The attributes set on the feature are determined by the columns set on the fmeOutput data frame at runtime. A helpful editor is used to construct the R Script, and provides convenient drag and drop access to data frames, columns, and published and private parameters which can be used within the script.

The number of features output will depend on the length of the largest column in the fmeOutput data frame. In this way the RCaller can be used to output a single value, or a list, or matrix of values.

A note on table contents: The data frames defined for the input ports require the attributes of the source features against which the queries will be performed. They do not have to – and should not – contain additional attributes.

A note on features routed to input ports: Features routed to input ports should have attributes on them which match the schema defined for the input port data frame. If they do not, null values will be inserted in place of missing attributes for the columns defined for the input table. An upstream AttributeRenamer or NullAttributeMapper can be used to ensure that attribute values are present for defined columns.

When the R raster package is loaded, rasters are supported on input and output. On input, raster geometry in FME will be converted to a RasterBrick object in R under the raster column for that table. That is, the column InputTableName$raster will contain a RasterBrick object for each feature that had a raster geometry, and NA for each feature that didn’t. On output, raster objects in the fmeOutput$raster column in R will be converted to raster geometry in FME.

Example

Sample R script to calculate the mean and median of a list of numbers

list<-c(1,2,2,2,2,3,3,3)

fmeOutput<-data.frame(mean=mean(list), median=median(list))

Note in the above example you can assign list to the value of a numeric input table column (for example, list<-myInput$testColumn)

The output of this script will be a single feature with outputs mean and median

Extending the above example to work with values from a dataset

Imagine that you have source tree data from a nearby park, including a tree trunk diameter attribute, Diameter.

If the dataset name is Trees, when you connect the dataset to the RCaller, an input port Trees will be created.

Ensure in the Columns section of the Inputs that Diameter is a numeric datatype.

In the R Script section, specifying fmeOutput<-data.frame(MeanDiameter=mean(Trees$Diameter)) will calculate the mean diameter of the trees.

Specifying MeanDiameter in the Attributes to Expose parameter will expose the mean diameter attribute, making it possible to use it later on in the workflow.

Usage Notes

Installing R

To use this transformer, you must install both R and the sqldf package. Using raster objects additionally requires the raster package.

Troubleshooting Tips

Double check the types on your input tables. If you want to do numeric calculations on certain columns make sure that they are configured as numeric types.

The following link describes a particular Windows issue with RCaller:

https://knowledge.safe.com/questions/107172/rcaller-no-package-caled-sqldf.html

Specifying the R Interpreter

FME will try its best to find R as installed on your system; however, if R is installed in a non-default location, or you have multiple R interpreters installed, it may be necessary to specify the R Interpreter Path under Tools > FME Options > Translation > R Interpreter

Editing Transformer Parameters

Using a set of menu options, transformer parameters can be assigned by referencing other elements in the workspace. More advanced functions, such as an advanced editor and an arithmetic editor, are also available in some transformers. To access a menu of these options, click beside the applicable parameter. For more information, see Transformer Parameter Menu Options.

Defining Values

There are several ways to define a value for use in a Transformer. The simplest is to simply type in a value or string, which can include functions of various types such as attribute references, math and string functions, and workspace parameters. There are a number of tools and shortcuts that can assist in constructing values, generally available from the drop-down context menu adjacent to the value field.

Dialog Options - Tables

Transformers with table-style parameters have additional tools for populating and manipulating values.

FME Community

The FME Community is the place for demos, how-tos, articles, FAQs, and more. Get answers to your questions, learn from other users, and suggest, vote, and comment on new features.

Search for samples and information about this transformer on the FME Community.