If you want to see the call stack without an error being thrown, you could try something like this:
show_stack <- function() {
cat("#----- Stack containing call to show_stack -----#\n\n")
x <- sys.calls()
lapply(head(x, -1), function(x) {print(x); cat("\n")})
cat("#-----------------------------------------------#\n\n")
}
Which you just insert in whichever function you want to trace:
foo <- function() bar()
bar <- function() baz()
baz <- function() show_stack()
Resulting in:
foo()
#> #----- Stack containing call to show_stack -----#
#>
#> foo()
#>
#> function() bar()
#>
#> function() baz()
#>
#> #-----------------------------------------------#
Or, for a more real-world example:
my_mean <- function(x) {
show_stack()
sum(x)/length(x)
}
Which may be run multiple times in a call:
tapply(iris$Sepal.Length, iris$Species, my_mean)
#> #----- Stack containing call to show_stack -----#
#>
#> tapply(iris$Sepal.Length, iris$Species, my_mean)
#>
#> lapply(X = ans[index], FUN = FUN, ...)
#>
#> FUN(X[[i]], ...)
#>
#> #-----------------------------------------------#
#>
#> #----- Stack containing call to show_stack -----#
#>
#> tapply(iris$Sepal.Length, iris$Species, my_mean)
#>
#> lapply(X = ans[index], FUN = FUN, ...)
#>
#> FUN(X[[i]], ...)
#>
#> #-----------------------------------------------#
#>
#> #----- Stack containing call to show_stack -----#
#>
#> tapply(iris$Sepal.Length, iris$Species, my_mean)
#>
#> lapply(X = ans[index], FUN = FUN, ...)
#>
#> FUN(X[[i]], ...)
#>
#> #-----------------------------------------------#
#>
#> setosa versicolor virginica
#> 5.006 5.936 6.588
CLICK HERE to find out more related problems solutions.