Table of Contents

Condition Trapping with AnalysisPageServer

In R "conditions" are messages, warnings and errors. AnalysisPageServer contains an API, completely independent of its other components, to trap all of these and be able to examine the call stacks.

First let's make some functions so that we'll have non-trivial call stacks.

e2 <- function(...)  stop(...)
e1 <- function(...)  e2(...)

w2 <- function(...)  warning(...)
w1 <- function(...)  w2(...)

m2 <- function(...)  message(...)
m1 <- function(...)  m2(...)

To use this functionality wrap your code in a call to tryKeepConditions(), which returns an object of class class "AnalysisPageValueWithConditions". Despite the name this is completely independent of the rest of AnalysisPageServer. We'll build two such objects, one without an error and one with

library(AnalysisPageServer)
vwc <- tryKeepConditions({
  m1("first message")
  m1("second message")
  w1("a warning")
  42
})
vwcErr <- tryKeepConditions({
  w1("a warning before the error")
  e1("this is a bad error")
  42
})

All of the functions to access these types of objects begin with vwc..

Check if your expression resulted in an error, and, if not, see what the value was (NULL if it did result in an error):

vwc.is.error(vwc)
## [1] FALSE
vwc.is.error(vwcErr)
## [1] TRUE
vwc.value(vwc)
## [1] 42
vwc.value(vwcErr)
## NULL

To inspect the three types of conditions there are a family of functions called vwc.TYPE(s).condition(s)(), vwc.TYPE(s)() and vwc.TYPE(s).traceback(s)() where TYPE is message, warning or error. The (s) is present in the function names for messages and warnings since there can be more than one, but absent for errors, since there can be only one.

vwc.TYPE(s).condition(s)() returns the condition object, or, for messages and warnings, a list of all the condition objects:

vwc.messages.conditions(vwc)
## [[1]]
## <simpleMessage in message(...): first message
## >
## 
## [[2]]
## <simpleMessage in message(...): second message
## >
vwc.messages.conditions(vwcErr)
## list()
vwc.warnings.conditions(vwc)
## [[1]]
## <simpleWarning in w2(...): a warning>
vwc.warnings.conditions(vwcErr)
## [[1]]
## <simpleWarning in w2(...): a warning before the error>
vwc.error.condition(vwc)
## NULL
vwc.error.condition(vwcErr)
## <simpleError in e2(...): this is a bad error>

vwc.TYPE(s) returns the condition messages as strings or character vectors:

vwc.messages(vwc)
## [1] "first message\n"  "second message\n"
vwc.messages(vwcErr)
## character(0)
vwc.warnings(vwc)
## [1] "a warning"
vwc.warnings(vwcErr)
## [1] "a warning before the error"
vwc.error(vwcErr)
## [1] "this is a bad error"

(vwc.error(vwc) would throw a (new) error if the expression did not result in an error, so we don't call it here.)

Finally, vwc.TYPE(s).traceback(s)() return tracebacks as character vectors. Note that the "real" tracebacks can be extracted from the condition objects as returned by vwc.TYPE(s).conditions(s)(), but these functions return something suitable for showing to an end user.

(Since this vignette was rendered with knitr the call stacks are fairly complicated, but if you run it in an R session you would just see the top few levels.)

vwc.messages.tracebacks(vwc)
## [[1]]
##  [1] "18: message(...) at <text>#7"                                                                            
##  [2] "17: m2(...) at <text>#8"                                                                                 
##  [3] "16: m1(\"first message\") at <text>#3"                                                                   
##  [4] "15: eval(expr, envir, enclos)"                                                                           
##  [5] "14: eval(expr, envir, enclos)"                                                                           
##  [6] "13: withVisible(eval(expr, envir, enclos))"                                                              
##  [7] "12: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, "                               
##  [8] "11: evaluate::evaluate(code, envir = env, new_device = FALSE, keep_warning = !isFALSE(options$warning), "
##  [9] "10: in_dir(opts_knit$get(\"root.dir\") %n% input_dir(), evaluate::evaluate(code, "                       
## [10] "9: block_exec(params)"                                                                                   
## [11] "8: call_block(x)"                                                                                        
## [12] "7: process_group.block(group)"                                                                           
## [13] "6: process_group(group)"                                                                                 
## [14] "5: process_file(text, output)"                                                                           
## [15] "4: knit(input, text = text, envir = envir, encoding = encoding, "                                        
## [16] "3: (if (grepl(\"\\\\.[Rr]md$\", file)) knit2html else if (grepl(\"\\\\.[Rr]rst$\", "                     
## [17] "2: engine$weave(file, quiet = quiet, encoding = enc)"                                                    
## [18] "1: tools::buildVignettes(dir = \".\", tangle = TRUE)"                                                    
## 
## [[2]]
##  [1] "18: message(...) at <text>#7"                                                                            
##  [2] "17: m2(...) at <text>#8"                                                                                 
##  [3] "16: m1(\"second message\") at <text>#4"                                                                  
##  [4] "15: eval(expr, envir, enclos)"                                                                           
##  [5] "14: eval(expr, envir, enclos)"                                                                           
##  [6] "13: withVisible(eval(expr, envir, enclos))"                                                              
##  [7] "12: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, "                               
##  [8] "11: evaluate::evaluate(code, envir = env, new_device = FALSE, keep_warning = !isFALSE(options$warning), "
##  [9] "10: in_dir(opts_knit$get(\"root.dir\") %n% input_dir(), evaluate::evaluate(code, "                       
## [10] "9: block_exec(params)"                                                                                   
## [11] "8: call_block(x)"                                                                                        
## [12] "7: process_group.block(group)"                                                                           
## [13] "6: process_group(group)"                                                                                 
## [14] "5: process_file(text, output)"                                                                           
## [15] "4: knit(input, text = text, envir = envir, encoding = encoding, "                                        
## [16] "3: (if (grepl(\"\\\\.[Rr]md$\", file)) knit2html else if (grepl(\"\\\\.[Rr]rst$\", "                     
## [17] "2: engine$weave(file, quiet = quiet, encoding = enc)"                                                    
## [18] "1: tools::buildVignettes(dir = \".\", tangle = TRUE)"
vwc.messages.tracebacks(vwcErr)
## list()
vwc.warnings.tracebacks(vwc)
## [[1]]
##  [1] "18: warning(...) at <text>#4"                                                                            
##  [2] "17: w2(...) at <text>#5"                                                                                 
##  [3] "16: w1(\"a warning\") at <text>#5"                                                                       
##  [4] "15: eval(expr, envir, enclos)"                                                                           
##  [5] "14: eval(expr, envir, enclos)"                                                                           
##  [6] "13: withVisible(eval(expr, envir, enclos))"                                                              
##  [7] "12: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, "                               
##  [8] "11: evaluate::evaluate(code, envir = env, new_device = FALSE, keep_warning = !isFALSE(options$warning), "
##  [9] "10: in_dir(opts_knit$get(\"root.dir\") %n% input_dir(), evaluate::evaluate(code, "                       
## [10] "9: block_exec(params)"                                                                                   
## [11] "8: call_block(x)"                                                                                        
## [12] "7: process_group.block(group)"                                                                           
## [13] "6: process_group(group)"                                                                                 
## [14] "5: process_file(text, output)"                                                                           
## [15] "4: knit(input, text = text, envir = envir, encoding = encoding, "                                        
## [16] "3: (if (grepl(\"\\\\.[Rr]md$\", file)) knit2html else if (grepl(\"\\\\.[Rr]rst$\", "                     
## [17] "2: engine$weave(file, quiet = quiet, encoding = enc)"                                                    
## [18] "1: tools::buildVignettes(dir = \".\", tangle = TRUE)"
vwc.warnings.tracebacks(vwcErr)
## [[1]]
##  [1] "18: warning(...) at <text>#4"                                                                            
##  [2] "17: w2(...) at <text>#5"                                                                                 
##  [3] "16: w1(\"a warning before the error\") at <text>#9"                                                      
##  [4] "15: eval(expr, envir, enclos)"                                                                           
##  [5] "14: eval(expr, envir, enclos)"                                                                           
##  [6] "13: withVisible(eval(expr, envir, enclos))"                                                              
##  [7] "12: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, "                               
##  [8] "11: evaluate::evaluate(code, envir = env, new_device = FALSE, keep_warning = !isFALSE(options$warning), "
##  [9] "10: in_dir(opts_knit$get(\"root.dir\") %n% input_dir(), evaluate::evaluate(code, "                       
## [10] "9: block_exec(params)"                                                                                   
## [11] "8: call_block(x)"                                                                                        
## [12] "7: process_group.block(group)"                                                                           
## [13] "6: process_group(group)"                                                                                 
## [14] "5: process_file(text, output)"                                                                           
## [15] "4: knit(input, text = text, envir = envir, encoding = encoding, "                                        
## [16] "3: (if (grepl(\"\\\\.[Rr]md$\", file)) knit2html else if (grepl(\"\\\\.[Rr]rst$\", "                     
## [17] "2: engine$weave(file, quiet = quiet, encoding = enc)"                                                    
## [18] "1: tools::buildVignettes(dir = \".\", tangle = TRUE)"
vwc.error.traceback(vwc)
## NULL
vwc.error.traceback(vwcErr)
##  [1] "18: stop(...) at <text>#1"                                                                               
##  [2] "17: e2(...) at <text>#2"                                                                                 
##  [3] "16: e1(\"this is a bad error\") at <text>#10"                                                            
##  [4] "15: eval(expr, envir, enclos)"                                                                           
##  [5] "14: eval(expr, envir, enclos)"                                                                           
##  [6] "13: withVisible(eval(expr, envir, enclos))"                                                              
##  [7] "12: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, "                               
##  [8] "11: evaluate::evaluate(code, envir = env, new_device = FALSE, keep_warning = !isFALSE(options$warning), "
##  [9] "10: in_dir(opts_knit$get(\"root.dir\") %n% input_dir(), evaluate::evaluate(code, "                       
## [10] "9: block_exec(params)"                                                                                   
## [11] "8: call_block(x)"                                                                                        
## [12] "7: process_group.block(group)"                                                                           
## [13] "6: process_group(group)"                                                                                 
## [14] "5: process_file(text, output)"                                                                           
## [15] "4: knit(input, text = text, envir = envir, encoding = encoding, "                                        
## [16] "3: (if (grepl(\"\\\\.[Rr]md$\", file)) knit2html else if (grepl(\"\\\\.[Rr]rst$\", "                     
## [17] "2: engine$weave(file, quiet = quiet, encoding = enc)"                                                    
## [18] "1: tools::buildVignettes(dir = \".\", tangle = TRUE)"

SessionInfo

sessionInfo()
## R version 3.2.3 (2015-12-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 14.04.3 LTS
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=C              
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggplot2_1.0.1            knitr_1.11              
## [3] AnalysisPageServer_1.4.1
## 
## loaded via a namespace (and not attached):
##  [1] graph_1.48.0        Rcpp_0.12.2         magrittr_1.5       
##  [4] BiocGenerics_0.16.1 MASS_7.3-45         munsell_0.4.2      
##  [7] colorspace_1.2-6    rjson_0.2.15        stringr_1.0.0      
## [10] plyr_1.8.3          tools_3.2.3         parallel_3.2.3     
## [13] grid_3.2.3          Biobase_2.30.0      gtable_0.1.2       
## [16] log4r_0.2           digest_0.6.8        reshape2_1.4.1     
## [19] formatR_1.2.1       evaluate_0.8        labeling_0.3       
## [22] stringi_1.0-1       scales_0.3.0        stats4_3.2.3       
## [25] markdown_0.7.7      proto_0.3-10