ID EN
Function Utilities

trace

R Base 3.6.2 🇮🇩 Bahasa Indonesia

Panggilan untuk melacak memungkinkan Anda memasukkan kode debug (misalnya, panggilan ke browser atau pemulihan) di tempat yang dipilih dalam fungsi apa pun. Panggilan untuk membatalkan penelusuran akan membatalkan penelusuran. Metode yang ditentukan dapat ditelusuri dengan cara yang sama, tanpa menelusuri semua panggilan ke fungsi generik. Kode jejak (tracer) dapat berupa ekspresi R apa pun. Penelusuran untuk sementara dapat diaktifkan atau dinonaktifkan secara global dengan memanggil tracingState.

Syntax

R
trace(what, tracer, exit, at, print, signature,
      where = topenv(parent.frame()), edit = FALSE)
untrace(what, signature = NULL, where = topenv(parent.frame()))<p></p><p>tracingState(on = NULL)
.doTrace(expr, msg)
returnValue(default = NULL)</p>

Arguments

Parameter Deskripsi
what the name, possibly quote()d, of a function to be traced or untraced. For untrace or for trace with more than one argument, more than one name can be given in the quoted form, and the same action will be applied to each one. For “hidden” functions such as S3 methods in a namespace, where = * typically needs to be specified as well.
tracer either a function or an unevaluated expression. The function will be called or the expression will be evaluated either at the beginning of the call, or before those steps in the call specified by the argument at. See the details section.
exit either a function or an unevaluated expression. The function will be called or the expression will be evaluated on exiting the function. See the details section.
at optional numeric vector or list. If supplied, tracer will be called just before the corresponding step in the body of the function. See the details section.
print If TRUE (as per default), a descriptive line is printed before any trace expression is evaluated.
signature If this argument is supplied, it should be a signature for a method for function what. In this case, the method, and not the function itself, is traced.
edit For complicated tracing, such as tracing within a loop inside the function, you will need to insert the desired calls by editing the body of the function. If so, supply the edit argument either as TRUE, or as the name of the editor you want to use. Then trace() will call edit and use the version of the function after you edit it. See the details section for additional information.
where where to look for the function to be traced; by default, the top-level environment of the call to trace. An important use of this argument is to trace functions from a package which are “hidden” or called from another package. The namespace mechanism imports the functions to be called (with the exception of functions in the base package). The functions being called are not the same objects seen from the top-level (in general, the imported packages may not even be attached). Therefore, you must ensure that the correct versions are being traced. The way to do this is to set argument where to a function in the namespace (or that namespace). The tracing computations will then start looking in the environment of that function (which will be the namespace of the corresponding package). (Yes, it's subtle, but the semantics here are central to how namespaces work in R.)
on logical; a call to the support function tracingState returns TRUE if tracing is globally turned on, FALSE otherwise. An argument of one or the other of those values sets the state. If the tracing state is FALSE, none of the trace actions will actually occur (used, for example, by debugging functions to shut off tracing during debugging).
expr, msg arguments to the support function .doTrace, calls to which are inserted into the modified function or method: expr is the tracing action (such as a call to browser()), and msg is a string identifying the place where the trace action occurs.
default If returnValue finds no return value (e.g. a function exited because of an error, restart or as a result of evaluating a return from a caller function), it will return default instead.

Return Value

Dalam versi sederhana (hanya argumen pertama), trace mengembalikan NULL yang tidak terlihat. Jika tidak, nama fungsi yang dilacak. Konsekuensi yang relevan adalah penugasan yang berlangsung. untrace mengembalikan nama fungsi tanpa terlihat. tracingState mengembalikan status penelusuran global saat ini, dan mungkin mengubahnya. Saat dipanggil selama pemrosesan on.exit, returnValue mengembalikan nilai yang akan dikembalikan oleh fungsi keluar. Perilaku dalam keadaan lain tidak ditentukan.

Details

Fungsi jejak beroperasi dengan membuat versi fungsi yang direvisi (atau metode, jika tanda tangan diberikan), dan menetapkan objek baru kembali ke tempat objek asli ditemukan. Jika hanya argumen apa yang diberikan, baris pencetakan jejak dihasilkan untuk setiap panggilan ke fungsi (kembali kompatibel dengan versi jejak sebelumnya). Objek yang dibangun oleh trace berasal dari kelas yang memperluas "fungsi" dan berisi versi asli yang belum dilacak. Panggilan untuk membatalkan jejak akan menetapkan ulang versi ini. Jika argumen tracer atau exit adalah nama suatu fungsi, ekspresi penelusuran akan menjadi panggilan ke fungsi tersebut, tanpa argumen. Ini adalah kasus termudah dan paling umum, dengan fungsi browser dan memulihkan kandidat yang paling mungkin; yang pertama menjelajah dalam bingkai fungsi menjadi

Contoh

Example
R
# NOT RUN {
require(stats)

##  Very simple use
trace(sum)
hist(rnorm(100)) # shows about 3-4 calls to sum()
untrace(sum)

## Show how pt() is called from inside power.t.test():
if(FALSE)
  trace(pt) ## would show ~20 calls, but we want to see more:
trace(pt, tracer = quote(cat(sprintf("tracing pt(*, ncp = %.15g)\n", ncp))),
      print = FALSE) # <- not showing typical extra
power.t.test(20, 1, power=0.8, sd=NULL)  ##--> showing the ncp root finding:
untrace(pt)

# }
# NOT RUN {
<!-- %% methods is loaded when needed -->
# }
# NOT RUN {
<!-- %% if(.isMethodsDispatchOn()) { # non-simple use needs 'methods' package -->
# }
# NOT RUN {
f <- function(x, y) {
    y <- pmax(y, 0.001)
    if (x > 0) x ^ y else stop("x must be positive")
}

## arrange to call the browser on entering and exiting
## function f
trace("f", quote(browser(skipCalls = 4)),
      exit = quote(browser(skipCalls = 4)))

## instead, conditionally assign some data, and then browse
## on exit, but only then.  Don't bother me otherwise

trace("f", quote(if(any(y < 0)) yOrig <- y),
      exit = quote(if(exists("yOrig")) browser(skipCalls = 4)),
      print = FALSE)

## Enter the browser just before stop() is called.  First, find
## the step numbers

untrace(f) # (as it has changed f's body !)
as.list(body(f))
as.list(body(f)[[3]]) # -> stop(..) is [[4]]

## Now call the browser there

trace("f", quote(browser(skipCalls = 4)), at = list(c(3,4)))
# }
# NOT RUN {
f(-1,2) # --> enters browser just before stop(..)
# }
# NOT RUN {
## trace a utility function, with recover so we
## can browse in the calling functions as well.

trace("as.matrix", recover)

## turn off the tracing (that happened above)

untrace(c("f", "as.matrix"))

# }
# NOT RUN {
## Useful to find how system2() is called in a higher-up function:
trace(base::system2, quote(print(ls.str())))
# }
# NOT RUN {
##-------- Tracing hidden functions : need 'where = *'
##
## 'where' can be a function whose environment is meant:
trace(quote(ar.yw.default), where = ar)
a <- ar(rnorm(100)) # "Tracing ..."
untrace(quote(ar.yw.default), where = ar)

## trace() more than one function simultaneously:
##         expression(E1, E2, ...)  here is equivalent to
##          c(quote(E1), quote(E2), quote(.*), ..)
trace(expression(ar.yw, ar.yw.default), where = ar)
a <- ar(rnorm(100)) # --> 2 x "Tracing ..."
# and turn it off:
untrace(expression(ar.yw, ar.yw.default), where = ar)


# }
# NOT RUN {
## trace calls to the function lm() that come from
## the nlme package.
## (The function nlme is in that package, and the package
## has a namespace, so the where= argument must be used
## to get the right version of lm)

trace(lm, exit = recover, where = asNamespace("nlme"))
# }
# NOT RUN {
<!-- %%}% only if 'methods' -->
# }

See Also

browser and recover the likeliest tracing functions; also quote and substitute for constructing general expressions.