The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.
Within autoharp, the TreeHarp S4 class is used to
represent an R expression. It can then be manipulated in several ways in
order to perform static code analysis of student submissions.
The lintr package does an excellent job of parsing R
code, but it provides too much detail for the simpler tasks that
autoharp carries out. It is still used in the constructor,
but some of the parsed output from lintr are dropped. For
instance, the ( parentheses are dropped.
To understand the elements in a TreeHarp, let us consider the use of
a simple expression. Suppose we fit a linear model to variables in a
dataset. To create a TreeHarp object from an expression, we provide the
expression together with the quote = TRUE argument. This is
important because method dispatch is performed based on that second
argument, not the first! If we were to dispatch on the first, R would
evalate the expression in order to check its class - thus destroying the
expression we intended to capture.
TreeHarp objects have an associated plot method for visualisation of
the expression. This method relies on the plotting functions from the
igraph package. The full set of parameter options from
igraph can be utilised when plotting TreeHarp objects.
Figure 1 displays the visualisation of the tree1 object
created earlier.
opar <- par(mar=c(0,0,0,0))
plot(tree1, vertex.size=25, asp=0.6, vertex.color="gray", vertex.frame.color=NA)Example TreeHarp object
There are 4 slots in a TreeHarp object. The only required one for valid instantiation is the adjList.
slot(tree1, "adjList")
#> $lm
#> [1] 2 3
#>
#> $`~`
#> [1] 4 5
#>
#> $data
#> [1] 6
#>
#> $y
#> NULL
#>
#> $`+`
#> [1] 7 8
#>
#> $mydata
#> NULL
#>
#> $x1
#> NULL
#>
#> $x2
#> NULLThis slot contains an adjacency list that represents the tree structure of the code. Nodes in a tree are labelled in Breadth-First Search (BFS) order. Thus the root node has id 1, and does not appear in the adjacency list. To avoid redundancy, the TreeHarp convention is to list each edge only once, as a child. Here’s what we mean: node 2 in the example above is a neighbour of node 1 and 4, but it only appears under node 1. It does not appear as an adjacent node of node 4 because it is not child of node 4. Terminal nodes (leafs) have a NULL entry in the list.
If the TreeHarp object was constructed from an R language object, this slot will be automatically populated. To identify node types, functions from rlang are applied to sub-expressions defined by nodes recursively. Each node is then identified as either:
The nodeTypes slot stores the information in a data
frame with one row per node. The columns are:
autoharp provides a getter function to retrieve the node types easily.
The call slot stores the original expression that was used to construct the TreeHarp object, just in case it needs to be executed later.
The repr slot contains a string representation of the object. If the
original TreeHarp object has been modified, then it may not be a proper
R expression, so this slot stores the best representation of it. This
slot is used when the object is printed, or when show is
called on the S4 object.
As we have already demonstrated, the plot method exists for this class. It relies on the tree layout of igraph package, but additional arguments can be used to customise the plot. For instance, we could use colour to distinguish between calls and non-call nodes:
opar <- par(mar=c(0,0,0,0))
plot(tree1, vertex.size=25, asp=0.6, vertex.color=tree1@nodeTypes$call_status)TreeHarp object with colored nodes
These are the other S4 methods defined for the TreeHarp class:
length: returns the number of nodes.names: returns the node names.get_parent_id: returns the parent id of a node.get_child_ids: returns the ids of the children of a
node.get_node_types: returns the nodeTypes slot from a
TreeHarp object.get_adj_list: returns the adjacency list slot from a
TreeHarp object.These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.