Assign/Get Octave Variables

Description

o_assign assigns a variable in Octave. o_assignin is an alias for o_assign.

o_get fetches Octave variables/functions and possibly rename them on the fly with the provided argument names when present. Functions are returned as objects of class OctaveFunction-class, that can be called subsequently (see the examples).

Usage

o_assign(...)

o_assignin(...)

o_get(..., unlist = TRUE, rm.ans = TRUE, pattern)

Arguments

...
variables to assign in Octave global context for o_assign , or object names to retrieve from Octave for o_get.
unlist
a logical that specifies it single variables should be returned as a single value (default), or as a list.
rm.ans
a logical that indicates if the automatic Octave variable ans should be included in the result. Default is not to include it unless otherwise explicitly specified with this argument, or if it is part of the requested variables in .... When present, argument rm.ans is always honoured.
pattern
regular expression used to filter the requested variable names. Only names matching the pattern are returned.

Value

o_assign returns invisibly the names of the assigned variables.

o_get returns a list of the retrieved variable/object. If unlist=TRUE and a single -- not re-named -- variable/object is requested then only its value is returned.

Details

o_assign assigns the variables using the arguments' names if present. Variables can also be specified as a single named list or environment. Single variable assignments can also be specified as o_assign('a', 10). See Examples for more details.

Note

The function o_get is the equivalent of R get function and is not related to the Octave function get which returns graphics' properties.

Octave Documentation for <em>assignin</em>

 -- Built-in Function: assignin (CONTEXT, VARNAME, VALUE)
     Assign VALUE to VARNAME in context CONTEXT, which may be either
     '"base"' or '"caller"'.

     See also: evalin

[Generated from Octave-3.6.4 on 2014-05-21 11:08:10 ]

Examples


## Don't show: 
# roxygen generated flag
options(R_CHECK_RUNNING_EXAMPLES_=TRUE)
## End Don't show

#----------
# o_assign
#----------
## Don't show: 
o_clear()
## End Don't show

## directly assign variables
o_assign(a=1, b=2, c=matrix(1:9, 3))
# retrieve their values
o_get()
## $a
## [1] 1
## 
## $b
## [1] 2
## 
## $c
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
## Don't show: 
 stopifnot( identical(o_get(), list(a=1, b=2, c=matrix(1:9, 3))) ) 
## End Don't show

## assign a variable for each element in a list
x <- list(a=10, b=20, c=matrix(101:109, 3))
o_assign(x)
o_get()
## $a
## [1] 10
## 
## $b
## [1] 20
## 
## $c
##      [,1] [,2] [,3]
## [1,]  101  104  107
## [2,]  102  105  108
## [3,]  103  106  109
## Don't show: 
 stopifnot( identical(o_get(), x) ) 
## End Don't show

## assign the content of an environment
e <- list2env(setNames(x, paste('env', names(x), sep='_')))
o_assign(e)
o_get(pattern="^env_")
## $env_a
## [1] 10
## 
## $env_b
## [1] 20
## 
## $env_c
##      [,1] [,2] [,3]
## [1,]  101  104  107
## [2,]  102  105  108
## [3,]  103  106  109

#----------
# o_get
#----------
## Don't show: 
o_clear();
## End Don't show

# get all currently defined variables
o_get()
## list()

# by default, the automatic variable `ans` is not returned but might be there
# from unstored previous computation
o_eval('svd(rand(3,3))')
##        [,1]
## [1,] 1.9764
## [2,] 0.5585
## [3,] 0.1591
o_get()
## list()
o_get(rm.ans=FALSE)
## $ans
##        [,1]
## [1,] 1.9764
## [2,] 0.5585
## [3,] 0.1591

# load some variables
x <- list(b=1, c=3, d=matrix(1:9, 3))
o_assign(x)

# re-fetch all variables
o_get()
## $b
## [1] 1
## 
## $c
## [1] 3
## 
## $d
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
## Don't show: 
 stopifnot( identical(o_get(), x) ) 
## End Don't show

# only fetch specific variables
o_get('b')
## [1] 1
o_get('b', 'c')
## $b
## [1] 1
## 
## $c
## [1] 3
# one can rename variables on the fly
o_get(a='b', 'c')
## $a
## [1] 1
## 
## $c
## [1] 3
o_get(c(othername='b'))
## $othername
## [1] 1
o_get(c(othername='b', 'c'))
## $othername
## [1] 1
## 
## $c
## [1] 3

# fetching using a regular expression
o_assign( setNames(1:3, paste("test", 1:3, sep='_')))
o_get()
## $b
## [1] 1
## 
## $c
## [1] 3
## 
## $d
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
## 
## $test_1
## [1] 1
## 
## $test_2
## [1] 2
## 
## $test_3
## [1] 3
o_get(pattern="^test")
## $test_1
## [1] 1
## 
## $test_2
## [1] 2
## 
## $test_3
## [1] 3

# works with functions
f <- o_get('svd')
f
## <OctaveFunction::`svd`>
f(matrix(1:9,3))
##           [,1]
## [1,] 1.685e+01
## [2,] 1.068e+00
## [3,] 5.039e-17
f(matrix(1:9,3), argout=3)
## [[1]]
##         [,1]     [,2]    [,3]
## [1,] -0.4797  0.77669  0.4082
## [2,] -0.5724  0.07569 -0.8165
## [3,] -0.6651 -0.62532  0.4082
## 
## [[2]]
##       [,1]  [,2]      [,3]
## [1,] 16.85 0.000 0.000e+00
## [2,]  0.00 1.068 0.000e+00
## [3,]  0.00 0.000 5.039e-17
## 
## [[3]]
##         [,1]    [,2]    [,3]
## [1,] -0.2148 -0.8872 -0.4082
## [2,] -0.5206 -0.2496  0.8165
## [3,] -0.8263  0.3879 -0.4082

# an error is thrown in the case of multiple matches (the alternatives are shown)
o_clear()
o_assign(aaa=1, ab=2)
try(o_get('a'))