FME Transformers: 2024.2
FME Transformers: 2024.2
RCaller
Executes an R script that has the ability to access feature data from a temporary R data frame.
How does it work?
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:
https://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:
https://www.r-bloggers.com/2015/12/how-to-learn-r-2/
https://www.r-tutor.com/r-introduction
Examples
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
Example: 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.
Installing the R Interpreter
Download R Installers from:
Windows:
Install R following the usual Windows installation steps.
For more information, please see https://cran.r-project.org/bin/windows/base/.
macOS:
For installing R on macOS, either you can either download the latest R image from:
Or, alternatively use the homebrew, package manager command:
brew install R
This requires the homebrew package manager to be installed on the system. Homebrew can be downloaded from:
Linux:
Run /opt/<FME version>/utilities/rcaller_setup from the command line (for example, /opt/fme-desktop-2020/utilities/rcaller_setup)
Installing R Packages
The Linux setup script in the Installing the R Interpreter section will install commonly used R packages. If more are required, first try to install them via system r-cran-* packages (often available on Debian and Ubuntu). Only if a system package does not exist should they be installed via the R Command Prompt instructions below.
- Open an R command prompt.
- Windows: Run the R GUI as an administrator by right-clicking it in the Start menu and selecting “Run as administrator…” .
- Linux/macOS: Launch the R Console (as root unless R has already been configured to find per-user packages).
- Run the following command at the R command prompt:
install.packages("<package name>")
For example:
install.packages("sqldf") - This will launch a window prompting you to select a download mirror. Once a mirror is selected, the package will be installed to the system-wide R library. It is important that this is done with administrative privileges, otherwise the package will be installed to a user library and FME will not be able to use it.
- To verify that the package was installed correctly, check the location listed when you run
.libPaths()
at the command line. There should be a folder called “<package name>”.
Using Shared Resources
Optionally, you can place your R libraries in a shared resources folder. The root location of this folder is set in FME Workbench under Tools > FME Options > Default Paths > Shared FME Folders. Within the Shared FME Folder structure, R packages must be placed in the R directory, located under a Plugins directory. You may need to manually create these directories:
<Shared_FME_Folder>\Plugins\R
For more information, see Default Paths.
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 contains useful information about using R in FME: https://support.safe.com/hc/en-us/articles/25407631484301-RCaller-Ins-and-outs-of-using-R-in-FME
-
The following link describes a particular Windows issue with RCaller: https://community.safe.com/transformers-9/rcaller-no-package-called-sqldf-13801
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
Configuration
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 FME 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.
Output Attributes
Attributes to Expose |
Exposes attributes so they can be used elsewhere in the workspace. Attribute names may be entered directly or provided in the Enter Values for Attributes to Expose dialog accessed via the ellipsis button, where data type may also be specified. For more information on exposed and unexposed attributes, see Understanding Feature Types and Attributes. |
Editing Transformer Parameters
Transformer parameters can be set by directly entering values, using expressions, or referencing other elements in the workspace such as attribute values or user parameters. Various editors and context menus are available to assist. To see what is available, click beside the applicable parameter.
How to Set Parameter Values
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.
Using the Text Editor
The Text Editor provides a convenient way to construct text strings (including regular expressions) from various data sources, such as attributes, parameters, and constants, where the result is used directly inside a parameter.
Using the Arithmetic Editor
The Arithmetic Editor provides a convenient way to construct math expressions from various data sources, such as attributes, parameters, and feature functions, where the result is used directly inside a parameter.
Conditional Values
Set values depending on one or more test conditions that either pass or fail.
Parameter Condition Definition Dialog
Content
Expressions and strings can include a number of functions, characters, parameters, and more.
When setting values - whether entered directly in a parameter or constructed using one of the editors - strings and expressions containing String, Math, Date/Time or FME Feature Functions will have those functions evaluated. Therefore, the names of these functions (in the form @<function_name>) should not be used as literal string values.
Content Types
These functions manipulate and format strings. | |
Special Characters |
A set of control characters is available in the Text Editor. |
Math functions are available in both editors. | |
Date/Time Functions | Date and time functions are available in the Text Editor. |
These operators are available in the Arithmetic Editor. | |
These return primarily feature-specific values. | |
FME and workspace-specific parameters may be used. | |
Creating and Modifying User Parameters | Create your own editable parameters. |
Dialog Options - Tables
Table Tools
Transformers with table-style parameters have additional tools for populating and manipulating values.
Row Reordering
|
Enabled once you have clicked on a row item. Choices include:
|
Cut, Copy, and Paste
|
Enabled once you have clicked on a row item. Choices include:
Cut, copy, and paste may be used within a transformer, or between transformers. |
Filter
|
Start typing a string, and the matrix will only display rows matching those characters. Searches all columns. This only affects the display of attributes within the transformer - it does not alter which attributes are output. |
Import
|
Import populates the table with a set of new attributes read from a dataset. Specific application varies between transformers. |
Reset/Refresh
|
Generally resets the table to its initial state, and may provide additional options to remove invalid entries. Behavior varies between transformers. |
Note: Not all tools are available in all transformers.
For more information, see Transformer Parameter Menu Options.
FME Community
The FME Community has a wealth of FME knowledge with over 20,000 active members worldwide. Get help with FME, share knowledge, and connect with users globally.
Search for all results about the RCaller on the FME Community.