When applying a transform with R in Panopticon for small and simple cases, you have the option of typing or pasting the code directly into the Transforms window. However, when you have several different transforms, or when each transform is applied in several data tables, it is highly recommended to follow the steps outlined below on how to apply functional programming and the D.R.Y. principle (Don’t Repeat Yourself) to the R transforms in Panopticon.
1. Save your code in R-files for R. This option gives you the freedom to work on the code in RStudio.
2. Instead of using an imperative coding approach, define one or several functions in the file, which when invoked, runs your code, takes a data frame as an input argument, and then returns the resulting data frame.
3. In the Transforms window of the Panopticon Designer, reference this external code file at the very top:
You can then invoke (call) any function that you have defined in your code file with a function call in the transform code window. Ideally, the function will return the transformed data frame.
4. The path to the external code file needs to be valid both from the point of view of the Panopticon Designer on your local workstation, and from the point of view of the Panopticon Visualization Server you publish to. This can be assured by introducing a global parameter in the Visualization Server under the Parameters tab.
For example, you can name the parameter R_code_path and define its value as the full path to the folder that contains your code files. Next, on the Panopticon Visualization Server, define a global parameter with the same name, but with a value that is the path to the server-side folder containing your code files. Copy the code files to the server-side folder then edit the path specified in your sourcing call in the transform so that it contains the parameter. For example:
This will achieve a path reference to your code file which is valid in both the Designer and Server. It is also useful when promoting or migrating a Panopticon workbook from one server environment to another.
· If there is a need to apply different transforms to different data sets, you can solve this by defining several different functions in your code file.
· For very similar functions, avoid repeating the same code in a file by factoring out the common parts and placing them in a separate function, which can be invoked by the other functions.
· For a transform that needs to have different outputs based on certain conditions or variables, this can be controlled by adding another input parameter to the function. Depending on the argument given to that parameter, you can make the function do things differently by evaluating a condition. In addition, this argument can – if you want to – be supplied via a Panopticon parameter and thereby be put under a dashboard end-user control.