出于调试目的,我想打印调用当前函数的位置的行号(和函数名称)。我如何在 R 中得到这个?
我见过获取源文件名的解决方案 但是如何获取行号和函数名?]
编辑:我找到了如何以traceback()某种形式获取这些数据,回溯能够将其打印出来,但我不确定如何从中解码信息:
f <- function () {
traceback(x = 3, max.lines = 1)
}
g <- function()
{
f()
}
x <- g()
source("file.R") # file with this code
# 5: g() at file.R#20
# 4: eval(ei, envir)
# 3: eval(ei, envir)
# 2: withVisible(eval(ei, envir))
# 1: source("file.R")
str(x[[1]])
# chr "g()"
# - attr(*, "srcref")= 'srcref' int [1:8] 20 1 20 8 1 8 20 20
# ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x0000000013a31700>
Run Code Online (Sandbox Code Playgroud)
找到了解决办法!从 traceback() 的代码中得到:
f <- function ()
{
x <- .traceback(x = 1)
srcloc <- if (!is.null(srcref <- attr(x[[1]], "srcref"))) {
srcfile <- attr(srcref, "srcfile")
paste0("Called from ", x[[2]], ", at ", basename(srcfile$filename), "#", srcref[1])
}
cat(srcloc, "\n")
}
g <- function()
{
f()
}
g()
# Called from g(), at file.R#15
Run Code Online (Sandbox Code Playgroud)
为它编写了一个很好的包装函数:
# returns a list, unless fmtstring is specified
# level: 1 - caller of the caller of this function; 2 - its parent, 3 - its grand-parent etc.
# fmtstring: return format string: %f (function), %s (source file), %l (line)
#
# example: str <- caller_info("Called from %f at %s#%l\n")
# !!! it won't work with e.g. cat(caller_info("Called from %f at %s#%l\n"))
# or cat(paste0(caller_info("Called from %f at %s#%l\n"))) !!!
caller_info <- function (fmtstring = NULL, level = 1) # /sf/ask/4167623771/
{
x <- .traceback(x = level + 1)
i <- 1
repeat { # loop for subexpressions case; find the first one with source reference
srcref <- getSrcref(x[[i]])
if (is.null(srcref)) {
if (i < length(x)) {
i <- i + 1
next;
} else {
warning("caller_info(): not found\n")
return (NULL)
}
}
srcloc <- list(fun = getSrcref(x[[i+1]]), file = getSrcFilename(x[[i]]), line = getSrcLocation(x[[i]]))
break;
}
if (is.null(fmtstring))
return (srcloc)
fmtstring <- sub("%f", paste0(srcloc$fun, collapse = ""), fmtstring)
fmtstring <- sub("%s", srcloc$file, fmtstring)
fmtstring <- sub("%l", srcloc$line, fmtstring)
fmtstring
}
Run Code Online (Sandbox Code Playgroud)
这是它的使用方式:
f <- function ()
{
str <- caller_info("Called from %f at %s#%l\n")
cat(str)
}
Run Code Online (Sandbox Code Playgroud)
唯一的(次要)限制是,当在像cat(caller_info("Called from %f at %s#%l\n"))or 之类的子表达式中调用时cat(paste0(caller_info("Called from %f at %s#%l\n"))),R 会混淆地将这些子表达式计算为堆栈级别,这将其搞砸了。所以最好避免在表达式中使用这个包装器。
没有简单的函数可以满足您的要求,但出于调试目的,您可以调用browser()函数,然后运行where命令来查看当前的调用堆栈。例如,您可能会看到这样的内容:
where 1: calls()
where 2 at ~/temp/test.R#6: print(calls())
where 3 at ~/temp/test.R#9: f()
where 4: eval(ei, envir)
where 5: eval(ei, envir)
where 6: withVisible(eval(ei, envir))
where 7: source("~/temp/test.R", echo = TRUE)
Run Code Online (Sandbox Code Playgroud)
这提供了几个呼叫的位置,但不是全部。
如果您确实想要随行打印的内容(例如C/C++ 中的__LINE__和__FILE__宏),那就有点困难了。这会打印当前位置:
cat("This is line ", getSrcLocation(function() {}, "line"),
" of ", getSrcFilename(function() {}))
Run Code Online (Sandbox Code Playgroud)
并非所有函数都有名称,R 函数不知道您用什么名称调用它们,但您可以使用 . 查看当前调用sys.call()。所以这会打印所有内容:
cat("This is line ", getSrcLocation(function() {}, "line"),
" of ", getSrcFilename(function() {}),
" called as", deparse(sys.call()),
"\n")
Run Code Online (Sandbox Code Playgroud)
这可能会打印
This is line 3 of test.R called as f()
Run Code Online (Sandbox Code Playgroud)
sys.call有一个参数可以向上移动堆栈,但我不知道如何获取行号信息。
您可以使用以下命令获取进行当前调用的函数的开始位置
cat("Called from ", getSrcFilename(sys.function(-1)), " line ", getSrcLocation(sys.function(-1), "line"),
" as ", deparse(sys.call()), "\n")
Run Code Online (Sandbox Code Playgroud)
这将向您显示进行调用的代码,但行号仅适用于它来自的函数。这是保持函数简短的一个很好的论据!
| 归档时间: |
|
| 查看次数: |
844 次 |
| 最近记录: |