How can I use R in PetroVisor?

PetroVisor can import and execute R scripts. Each script must contain one or more function definitions. Each function may be used as an activity in a workflow.

The R activity

An R activity is a function adhering to a special scheme:

#definition of the function’s name and the input parameters
functionName <- function (inputSignal1, inputSignal2, inputSignalXY)
{
# source functions from other files
# take care to use the correct path to the function’s file!
source('C:/myrconn/R_code/RFunctions/apply_on_timeframe.R')

# load libraries
library(parallel)

# create the result variable (has to be of type list)
result <- list()

# convert input parameters to matrix
input1 <- as.matrix(inputSignal1)
.
.
.

# Your calculations here
.
.
.

# assign the return parameters to the result variable
# (if needed convert them to matrices)
result$output1 <- variable1      # as.matrix(variable1)
result$output2 <- variable2      # as.matrix(variable2)
.
.
.

# return the result to PetroVisor
return(result)
}

Creating R activities in PV

PetroVisor’s Workflow Automation tool features a basic R editor to create R activities.

  • Open the PetroVisor Workflow Automation tool
  • Navigate to the R scripts module
  • Enter your function’s code. You can use the console to create test data sets, source scripts on your local machine, etc.
  • Extract the activity using the extract button (each function given in the script will be extracted as an activity)

Running R activities in PV

R activities can be run from the R scripts module in PetroVisor. Make sure that the activity is extracted from the code. All available R activities are shown in the settings flyout. Please note that the R scripts module is like a sandbox, i.e. running an R script will not save the results to the database. Once the activity has been implemented and tested, it can be used as a workflow activity. Running an activity in the workflow will save results to the database.

  • Select one of the shown activites
  • Click the run button. A new window will show up allowing to map PetroVisor signals to the input arguments and to specify the context for the run
  •  Specify the desired settings and click the run button
  • PetroVisor will execute the function and return the output in separate tabs per output signal 

Importing existing R activities

Using the Server Configuration tool, it is possible to import already existing scripts into a workspace. Keep in mind that each script must adhere to the scheme stated above.

  • In the Server Configuration tool, navigate to the workspaces module and select the workspace the activity shall be imported to
  • Select Custom Activities from the group Content on the right side
  • In the new window, navigate to the R activities tab. All existing R activities will be shown in a list
  • Click new
  • Select a provider and specify the respective settings
  • Click connect to r server to get a list of scripts available at the specified location
  • Select the desired script from the dropdown. All functions defined in the script will be extracted
  • Select the desired function from the dropdown
  • Optionally, specify a name for the R activity (the name of the selected function will be used as activity name by default)
  • Check the source code, input and output arguments
  • Select add to import the activity 

Remarks

PetroVisor uses providers to communicate with R. The following providers are available:

  •  “RNET”: provider to connect to an R instance on the local machine using R.NET component (this provider is used by the client automatically)
  • Rserve: provider to connect to any R instance (on the local and remote machines) running Rserve on top of it.
 

Extracting activities in the R-module automatically creates activities using the provider “RNET”