# Prints class and structure of expressions as captured by a callback.
#' A tool for experimentation with R's callback mechanism.
#'
#' Enter demo_expr() to begin and bye() to finish.
#' For every valid command entered, the command itself, its class,
#' and an abstract syntax tree will be displayed, preceded by a
#' count of the number of times the callback has been invoked.
#' The count shows that the callback is invoked for every valid
#' expression entered. If multiple expressions separated
#' by semicolons are entered on the same line, the callback will
#' be invoked once for each.
#' @import pryr
#' @export
demo_expr <- function(){
removeTaskCallback("demo_expr")
cb_count <- 0
cb <- function(expr, val, blah1, blah2){
e <- environment(cb)
e$cb_count <- e$cb_count + 1
cat(paste0("\nThis is callback invocation ", e$cb_count, "."))
cat(paste0("\nExpression, '", deparse(expr), "', is of class '", class(expr),"', with syntax tree:\n"))
call_tree(expr)
return(TRUE)
}
addTaskCallback(cb, name="demo_expr")
invisible()
}
#' Remove callback added by demo_expr.
#' @export
bye <- function(){
removeTaskCallback("demo_expr")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.