Skip to main content

R Inputs Overview

Version 8.0.5

Encoding types

DeployR-specific encodings are used to encode R object data for use on the API. Encoded object data can be sent to the server as parameter values on the following calls:

The complete list of supported DeployR-encoding types is shown here. These encodings can encode a wide range of different classes of R objects:

deployr.RInput JavaScript R Object R Class
.logical() Boolean primitive logical
.numeric() Number primitive integer
.integer() Number primative numeric
.character() String primitive character
.date() Date date Date
.posix() Date date POSIXct
.list() Array [RInput] list list
.dataframe() Array [RInput] dataframe data.frame
.factor() Number factor ordered
.ordered() Number factor ordered factor
.logicalVector() Array [Boolean] primative(s) vector
.numericVector() Array [Number] primitive(s) vector
.integerVector() Array [Number] primitive(s) vector
.characterVector() Array [String] primative(s) vector
.dateVector() Array [Date] date(s) vector
.posixVector() Array [Date] date(s) vector
.numericMatrix Array [[Numeric]] primative(s) matrix
.integerMatrix() Array [[Numeric]] primative(s) matrix
.logicalMatrix() Array [[Boolean]] primative(s) matrix
.characterMatrix() Array [[String]] primative(s) matrix

To pass values from JavaScript as input to an R script you use one of the defined R data type methods above in the first column. Each DeployR-specific encoded object can be defined by the following required properties:

  • name - first argument defining the name of the encoded object
  • value - second argument defining the data encapsulated by the encoded object

For example .character(name, value)

Following this syntax .character(name, value) we produce thecharacter input 'Hello' to be sent to the R script:

deployr.io('/r/repository/script/execute')   
   .data({ filename: 'script.R', author: 'testuser' })
   .character('name_character', 'Hello')
   .end(function(res) {
      // handle successful response
   })

The exception is .factor() and .ordered() which takes an additional set of properties:

  • levels - third argument defines the input levels in the factor
  • labels - fourth argument defines the output labels in the factor

Following this syntax for .factor(name, value, levels, labels) and .ordered(name, value, levels, labels) we produce both an ordered and unordered factor as input to an R script:

deployr.io('/r/repository/script/execute')   
   .data({ filename: 'script.R', author: 'testuser' })
   .ordered('name_ofactor', [1, 2, 3], [1, 3, 3], ['a', 'b' , 'c'])  
   .factor('name_unofactor', [1, 2, 3], [2, 3, 3], ['a', 'b' , 'c'])
   .end(function(res) {
      // handle successful response
   });

The R script script.R has reviced the input decoded into their proper types:

###############################################################################
# File: script.R
#
# Accepts as input:
#
# - `name_character`  (character)
# - `name_ofactor`    (ordered factor)
# - `name_unodfactor` (unordered factor)
###############################################################################

print(name_character)

print(name_ofactor)

print(name_unofactor)

Helpers

At times it might not be convenient to use the R data type methods inlined like this so we provide both the .rinput() and .rinputs() methods:

deployr.RInput JavaScript R Object R Class
.rinput() RInput Any Any
.rinputs() Array [RInput] Any Any

These allow you to construct R inputs ahead of time using the deployr.RInput factory and pass them in an inputs:

For example, using the .rinputs() method to specify multiple R inputs at once:

var RIn     = deployr.RInput; // alias
var rinputs = []; 

// -- populate the Array with ` deployr.RInput` --
rinputs.push(RIn.character('name_character', 'Hello');
rinputs.push(RIn.ordered('name_ofactor', [1, 2, 3], [1, 3, 3], ['a', 'b' , 'c']));
rinputs.push(RIn.factor('name_unofactor', [1, 2, 3], [2, 3, 3], ['a', 'b' , 'c']));

deployr.io('/r/repository/script/execute')   
   .data({ filename: 'script.R', author: 'testuser' })
   .rinputs(rinputs)   
   .end(function(res) {
      // handle successful response
   });

Or a single R input at a time using the .rinput() method:

var RIn           = deployr.RInput; // alias
var nameCharacter = RIn.character('name_character', 'Hello');
var nameOFactor   = RIn.ordered('name_ofactor', [1, 2, 3], [1, 3, 3], ['a', 'b' , 'c']);
var nameUnOFactor = RIn.factor('name_unofactor', [1, 2, 3], [2, 3, 3], ['a', 'b' , 'c']);

deployr.io('/r/repository/script/execute')   
   .data({ filename: 'map.R', author: 'testuser' })
   .rinput(nameCharacter)
   .rinput(nameOFactor)   
   .rinput(nameUnOFactor)   
   .end(function(res) {
      // handle successful response
   });

Note both examples are equivalent to one another.