TreeHarp-class {autoharp}R Documentation

An R expression as a tree.

Description

This class is used to represent a single R expression as a tree.

Usage

TreeHarp(lang_obj, quote_arg, ...)

TreeHarp(lang_obj, quote_arg, ...)

## S4 method for signature 'logical'
TreeHarp(lang_obj, quote_arg, ...)

## S4 method for signature 'missing'
TreeHarp(lang_obj, quote_arg, ...)

## S4 method for signature 'TreeHarp'
length(x)

## S4 method for signature 'TreeHarp'
show(object)

## S4 method for signature 'TreeHarp'
names(x)

Arguments

lang_obj

This should be an adjacency list for a tree (not a graph), or the adjacency matrix of a tree, or the expression to be parsed. If it is a list, only child nodes should be indicated (see the examples).

quote_arg

If this argument is missing or FALSE, then the class of lang_obj will be evaluated, and, if it is either a list or matrix, the TreeHarp object will be returned.

If this argument is TRUE, the lang_obj argument will be quoted and a parse tree for the expression will be computed and used as the tree.

...

Unused at the moment.

x

A Treeharp object.

object

A TreeHarp object.

Details

The following validity checks are conducted on the object:

  1. Is the graph connected? If no, the object is invalid.

  2. Are there cycles? If yes, the object is invalid.

  3. Are the nodes labelled in a BFS ordering? If not, the object is not valid.

Value

Constructors return an object of class TreeHarp.

length: An integer of length 1.

print: Returns NULL. It prints a string representation of a TreeHarp object.

names: A character vector with length equal to the number of nodes.

Methods (by generic)

Slots

adjList

The adjacency list of the tree. The list must be named. The nodes should be labelled in Breadth-First Order. The first component must be the root of the tree. Leaves of the tree should be NULL elements.

nodeTypes

A data frame describing the type of node. The columns in the data frame will be derived from the expression used to instantiate the object. The column names will be id (node id), name, call_status, formal_arg and depth. This slot can be left missing (i.e., populated with NA). This latter feature is useful when we just wish to test something out.

This slot is only populated automatically when an R expression is provided as lang_obj and quote_arg is TRUE.

repr

A string representation of the tree. This will be printed when the show method of TreeHarp is called.

call

The language object that was used to construct the tree (if it was). If the object was constructed from a list/matrix, this will be NA.

Examples

l1 <- list(a=c(2,3), b=NULL, c=NULL)
# directly using new() 
treeharp1 <- new("TreeHarp", adjList = l1, nodeTypes = NA)

# using one of the constructor methods (for lists)
treeharp2 <- TreeHarp(l1)

# using the constructor for matrices.
m1 <- matrix(0L, 3, 3)
dimnames(m1) <- list(letters[1:3], letters[1:3])
m1[1, ] <- c(0, 1L, 1L)
m1[, 1] <- c(0, 1L, 1L)
treeharp3 <- TreeHarp(m1)

# Supplying a language object to get the same tree (with nodeTypes 
# populated)
ex1 <- quote(a(b,c))
TreeHarp(ex1, TRUE)


[Package autoharp version 0.0.10 Index]