--- title: "TreeHarp S4 class" output: rmarkdown::html_vignette: toc: true toc_depth: 2 vignette: > %\VignetteIndexEntry{TreeHarp S4 class} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} bibliography: references.bib --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` ```{r setup, echo=FALSE} library(magrittr) ``` # Introduction 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. ```{r th-example-1, echo=TRUE} library(autoharp) tree1 <- TreeHarp(quote(lm(y ~ x1 + x2, data=mydata)), TRUE) ``` 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. ```{r th-example-1-plot, echo=TRUE, fig.align='center', collapse=TRUE, fig.cap='Example TreeHarp object'} opar <- par(mar=c(0,0,0,0)) plot(tree1, vertex.size=25, asp=0.6, vertex.color="gray", vertex.frame.color=NA) par(opar) ``` # Slots There are 4 slots in a TreeHarp object. The only required one for valid instantiation is the adjList. ## adjList ```{r th1-adjlist} slot(tree1, "adjList") ``` This 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. ## nodeTypes If the TreeHarp object was constructed from an R language object, this slot will be automatically populated. To identify node types, functions from [rlang](https://cran.r-project.org/package=rlang) are applied to sub-expressions defined by nodes recursively. Each node is then identified as either: * a function call, or * a formal argument. The `nodeTypes` slot stores the information in a data frame with one row per node. The columns are: 1. *id* (node id). The root node has id 1. 2. *name*. The name of the node. 3. *call_status*. A TRUE/FALSE column indicating if the node was a call or not. 4. *formal_arg*. If the node is not a call, then this column will indicate if it is a formal argument or not. If it is not a call and not a formal argument, it is a symbol representing an R object - we call this an actual argument. 5. *depth*. This is the depth of the node in the tree. The root of the tree has depth 1. autoharp provides a getter function to retrieve the node types easily. ```{r th1-nodetypes, echo=TRUE} get_node_types(tree1) ``` ## call The call slot stores the original expression that was used to construct the TreeHarp object, just in case it needs to be executed later. ```{r ex1_call} slot(tree1, "call") ``` ## repr 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. ```{r ex1_repr} tree1 ``` # TreeHarp Methods 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: ```{r th-example-2-plot, echo=TRUE, fig.align='center', fig.cap='TreeHarp object with colored nodes'} opar <- par(mar=c(0,0,0,0)) plot(tree1, vertex.size=25, asp=0.6, vertex.color=tree1@nodeTypes$call_status) par(opar) ``` 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. In the table below, we list functions that have not been registered as S4 methods for TreeHarp. They are not immediately useful for typical grading sessions, but they are nonetheless useful when working with tree expressions. ```{r th-routines, echo=FALSE} fn_names <- c("subtree_at", "generate_all_subtrees", "carve_subtree", "is_subtree_rooted_at", "path_to_root", "get_recursive_index", "get_parent_call_id") fn_desc <- c("Returns a subtree, given a node number (integer) as an argument.", "Enumerates and prints all subtrees of a TreeHarp object.", "Given a list of nodes, this function returns a TreeHarp object with only the nodes indicated.", "This function can be used to check if one tree is rooted at a particular node of another tree.", "Identifies the nodes on the path from a node up to the root of a TreeHarp object.", "Returns a recursive call index that can be used to extract a sub-call from an R call (language expression).", "Returns the most recent call from a particular node id.") th_routines_df <- data.frame(fn_names, fn_desc) knitr::kable(th_routines_df, booktabs = TRUE, col.names = c("Function name", "Function description"), align = "l") %>% kableExtra::column_spec(2, width = "10cm") %>% kableExtra::kable_styling(font_size = 12, latex_options = c("hold_position")) # kableExtra::kable_styling(latex_options = c("hold_position")) ``` # ForestHarp Helpers A TreeHarp object is a single R expression. However, student scripts contain multiple expressions; hence the `autoharp` provides functionality through `fapply()` to process collections of expressions. It works on ForestHarp objects. A ForestHarp is a loosely defined idea, in the sense that there is no such S4 class defined. But it is an R object with a precise structure: it is a list of TreeHarp objects, and an accompanying set of line numbers corresponding to these expressions in the original student script. A ForestHarp object is best created using `rmd_to_forestharp()` function. ```{r fh1, echo=TRUE} stud_script_path <- system.file("examples", "example-01", "student_scripts", "qn01_scr_02.R", package = "autoharp") f1 <- rmd_to_forestharp(stud_script_path) str(f1, max.level = 2) ``` To work with such lists, the `autoharp` provides `fapply()`, for applying functions to ForestHarp objects, and then combining them back together again. The function `fapply()` takes inspiration from the family of apply functions within R. Suppose that we wished to count the number of times a `for` loop was used in the whole script. Then this would do the trick: ```{r fh2-num-for-loops, echo=TRUE} fapply(f1, count_fn_call, pattern = "^for$") ``` `count_fn_call` is a ForestHarp helper function. `fapply` applies it to each TreeHarp object in the list, and then combines that output into a scalar. By default, it combines them by taking the sum. However, it is possible to specify the combining function, or to just return the uncombined list. The following table lists the currently available forestharp helper functions. They work on a single TreeHarp expression at a time, and can be combined with `fapply`. In all of them, the first argument is a TreeHarp expression. ```{r fh-helpers, echo=FALSE} fn_names <- c("count_self_fn", "count_lam_fn", "count_fn_call", "extract_fn_call", "extract_formal_args", "extract_actual_args", "extract_assigned_objects", "count_num_lines_for_loop", "detect_growing", "detect_for_in_fn_def", "count_fn_in_fn", "detect_fn_call_in_for", "detect_fn_arg", "detect_nested_for") fn_desc <- c("Counts the number of functions defined by the student, in the script.", "Counts the number of anonymous functions (used in `sapply()`, etc).", "Counts the number of times a function with a particular name was executed. The function can be described by providing a regular expression, or a package namespace.", "Instead of just counting, this extracts the entire function call as a string.", "Extracts the formal arguments in a function call.", "Extracts the actual arguments in a function call.", "Extracts the name of object in an assignment expression.", "Counts the number of sub-expressions (lines) in a for loop.", "Detects use of a `for` loop without pre-allocating an iterable of the correct length.", "Detects the presence of a `for` loop in a function definition.", "Given functions A and B, this counts the number of times B is called within A.", "Detects the call of a function within a `for` loop.", "Detects if a specified argument was part of a function call.", "Detects the presence of a nested `for` loop.") fh_helpers_df <- data.frame(fn_names, fn_desc) knitr::kable(fh_helpers_df, booktabs = TRUE, col.names = c("Function name", "Function description"), align = "l") %>% #caption = "ForestHarp helpers. The reader is referrred to the help page on 'forestharp-helpers' for examples on usage.") %>% kableExtra::column_spec(2, width = "9cm") %>% kableExtra::kable_styling(font_size = 12, latex_options = c("hold_position")) ```