Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8207463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:00:09+00:00 2026-06-07T09:00:09+00:00

Preamble: R’s trace() is a powerful debugging tool, allowing users to insert debugging code

  • 0

Preamble:

R’s trace() is a powerful debugging tool, allowing users to “insert debugging code at chosen places in any function”. Unfortunately, using it from the command-line can be fairly laborious.

As an artificial example, let’s say I want to insert debugging code that will report the between-tick interval calculated by pretty.default(). I’d like to insert the code immediately after the value of delta is calculated, about four lines up from the bottom of the function definition. (Type pretty.default to see where I mean.)
To indicate that line, I need to find which step in the code it corresponds to. The answer turns out to be step list(c(12, 3, 3)), which I zero in on by running through the following steps:

as.list(body(pretty.default))
as.list(as.list(body(pretty.default))[[12]])
as.list(as.list(as.list(body(pretty.default))[[12]])[[3]])
as.list(as.list(as.list(body(pretty.default))[[12]])[[3]])[[3]]

I can then insert debugging code like this:

trace(what = 'pretty.default',
      tracer = quote(cat("\nThe value of delta is: ", delta, "\n\n")), 
      at = list(c(12,3,3)))
## Try it
a <- pretty(c(1, 7843))
b <- pretty(c(2, 23))
## Clean up
untrace('pretty.default')

Questions:

So here are my questions: Is there a way to print out a function (or a parsed version of it) with the lines nicely labeled by the steps to which they belong? (According to Venables and Ripley, S-plus has a function tprint() that “produces a numbered listing of the body of a function for use with the at argument of trace“, but R seems to have no equivalent.) Alternatively, is there another easier way, from the command line, to quickly set debugging code for a specific line within a function?

Addendum:

I used the pretty.default() example because it is reasonably tame, but with real/interesting functions, repeatedly using as.list() quickly gets tiresome and distracting. Here’s an example:

as.list(as.list(as.list(as.list(as.list(as.list(as.list(as.list(as.list(body(#
model.frame.default))[[26]])[[3]])[[2]])[[4]])[[3]])[[4]])[[4]])[[4]])[[3]]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:00:16+00:00Added an answer on June 7, 2026 at 9:00 am

    Here is a convenient wrapper for detecting the piece:

    library(codetools)
    ff <- function(f, tar) {
      cc <- function(e, w) {
        if(length(w$pos) > 0 &&
          grepl(w$tar, paste(deparse(e), collapse = "\n"), fixed = TRUE)) {
          cat(rev(w$pos), ": ", deparse(e), "\n")
          w$ret$vals <- c(w$ret$vals, list(rev(w$pos)))
        }
        w$pos <- c(0, w$pos)
        for (ee in as.list(e)){
          if (!missing(ee)) {      
            w$pos[1] <- w$pos[1] + 1
            walkCode(ee, w)
          }
        }
      }
    
      w <- list(pos = c(),
                tar = tar,
                ret = new.env(),
                handler = function(v, w) NULL,
                call = cc,
                leaf = function(e, w) NULL)
      walkCode(body(f), w = w)
      w$ret$vals
    }
    

    and then,

    > r <- ff(pretty.default, "delta <- diff(range(z$l, z$u))/z$n")
    12 :  if (!eps.correct && z$n) {     delta <- diff(range(z$l, z$u))/z$n     if (any(small <- abs(s) < 1e-14 * delta))          s[small] <- 0 } 
    12 3 :  {     delta <- diff(range(z$l, z$u))/z$n     if (any(small <- abs(s) < 1e-14 * delta))          s[small] <- 0 } 
    12 3 2 :  delta <- diff(range(z$l, z$u))/z$n 
    > r
    [[1]]
    [1] 12
    
    [[2]]
    [1] 12  3
    
    [[3]]
    [1] 12  3  2
    
    > r <- ff(model.frame.default, "stop(gettextf(\"factor '%s' has new level(s) %s\", nm, paste(nxl[m],")
    26 3 2 4 3 4 4 4 3 :  stop(gettextf("factor '%s' has new level(s) %s", nm, paste(nxl[m],      collapse = ", ")), domain = NA) 
    > r
    [[1]]
    [1] 26  3  2  4  3  4  4  4  3
    

    and you can define the tracer by contents:

    traceby <- function(fun, tar, cer) {
      untrace(deparse(substitute(fun)))
      r <- ff(fun, tar)
      r <- r[which.max(sapply(r, length))]
      trace(what = deparse(substitute(fun)), tracer = cer, at = r)
    }
    

    then,

    > traceby(pretty.default, "if (any(small <- abs(s) < 1e-14 * delta)) s[small] <- 0", quote(cat("\nThe value of delta is: ", delta, "\n\n")))
    Untracing function "pretty.default" in package "base"
    12 3 3 :  if (any(small <- abs(s) < 1e-14 * delta)) s[small] <- 0 
    Tracing function "pretty.default" in package "base"
    [1] "pretty.default"
    > a <- pretty(c(1, 7843))
    Tracing pretty.default(c(1, 7843)) step 12,3,3 
    
    The value of delta is:  2000 
    
    > b <- pretty(c(2, 23))
    Tracing pretty.default(c(2, 23)) step 12,3,3 
    
    The value of delta is:  5 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got Python code with a preamble containing the line: from numpy import array,arccosh,random_integers
Without any preamble I want to show you problem I have in my program,
What sort of code should I put in preamble, such that every math fomula
Preamble: All data connection strings, connections, etc are created using DbProviderFactories. Code is mixed
Is there any single magic preamble that will make a Perl script run under
I am beginner at C++, the homework ask us to document our code (preamble,
Preamble To build dynamic web-sites, we have to master at least four languages: HTML
Small preamble. I was good java developer on 1.4 jdk. After it I have
In the spirit of the Perl Preamble where a script works properly whether executed
Preamble: I know, disabling warnings is not a good idea. Anyway, I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.