-
Notifications
You must be signed in to change notification settings - Fork 8
/
limited_Labels_Compact.R
106 lines (94 loc) · 5.39 KB
/
limited_Labels_Compact.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# ***************************************************************************
# Copyright (C) 2016 Juergen Altfeld ([email protected])
# ---------------------------------------------------------------------------
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ***************************************************************************
#' Convert a call stack into a list of printable strings
#'
#' By default the maximum number of source code rows that are printed per call in the full stack trace
#' is 10. You can change this via the option \code{tryCatchLog.max.lines.per.call} (see example).
#'
#' @description Converts a call stack into a list of printable strings ("labels") with a limited length per call.
#' If source code references are available they are also printed in the stack trace using this notation:
#' \code{<file name>#<line number>: executed R expression (call)}
#'
#' @param value a list of calls ("call.stack") generated by \code{\link{sys.calls}}
#' @param compact if TRUE only calls that contain a source code reference (attribute "srcref") are returned
#' (plus always the first call); if FALSE all calls will be returned.
#' @param maxwidth Maximum number of characters per call in the return value (longer strings will be cutted).
#' Must be between 40 and 2000 (until version 1.2.2: 1000)
#'
#' @return A list of strings (one for each call).
#' If \code{compact} is \code{TRUE} at the last call is returned even if it does not contain
#' a source code reference.
#'
#' @details R does track source code references only if you set the option "keep.source" to TRUE via
#' \code{options(keep.source = TRUE)}. Without this option this function cannot enrich source code references.
#' If you use \command{Rscript} to start a non-interactive R script as batch job you
#' have to set this option since it is FALSE by default. You can add this option to your
#' \link{.Rprofile} file or use a startup R script that sets this option and sources your
#' actual R script then.
#'
#' This function is based on the undocumented \code{\link{limitedLabels}} function of the base package.
#' The source code can be viewed by entering \code{limitedLabels} in the R console.
#' The attributes required to add source file names and line numbers to the calls (srcref and srcfile)
#' and how they are created internally are explained in this article:
#' \url{https://journal.r-project.org/archive/2010-2/RJournal_2010-2_Murdoch.pdf}
#'
#' @seealso \code{\link{sys.calls}}, \code{\link{tryCatchLog}}, \code{\link{get.pretty.call.stack}}
#' @examples
#' options(tryCatchLog.max.lines.per.call = 30)
#' limitedLabelsCompact(sys.calls(), TRUE)
#' @export
limitedLabelsCompact <- function(value, compact = FALSE, maxwidth = getOption("width") - 5L) {
# create vector of source references (file and row numbers) for each call item of the stack
srcrefs <- sapply(value, function(v) {
srcref <- attr(v, "srcref")
if (!is.null(srcref)) {
srcfile <- attr(srcref, "srcfile")
paste0(basename(srcfile$filename), "#", srcref[1L], ": ")
}
else ""
})
# # create a list of only first line of code for each call stack item
# if (compact == TRUE)
# value <- lapply(as.character(value), function(x) strsplit(x, "\n")[[1]][1])
# New implementation fixes bug #68 (performance impact with sparse matrix)
if (compact == TRUE)
max.rows <- 1 else
max.rows <- getOption("tryCatchLog.max.lines.per.call", 10)
# nobody wants to see too many rows for each call in the call stack ;-)
# R prints even only one line per call for errors:
# https://github.com/wch/r-source/blob/54f94f0433c487fe55553b0df9bae477c9babdd1/src/main/deparse.c#L356
value <- lapply(value, function(x) {
if (is.language(x))
paste(deparse(x, width.cutoff = 500, control = NULL, nlines = max.rows), collapse = "\n") else
as.character(x)
})
# combine source references with the call stack and ident multi-line code
value <- gsub("\n", "\n ", as.character(value))
value <- paste0(srcrefs, value)
# cut lines that are too long
if (is.null(maxwidth) || maxwidth < 40L)
maxwidth <- 40L
maxwidth <- min(maxwidth, 2000L) # May 19, 2021: Maxwidth changed from 1000 to 2000 due to unit testing problem (issue #64)
value <- strtrim(value, maxwidth)
if (compact == TRUE) {
# return only call stack items that contain a source reference
srcrefs.available <- srcrefs != ""
srcrefs.available[1] <- TRUE # always return the first row!
value <- value[srcrefs.available]
}
return(value)
}