How to use trace() in case of reactives and observers?

I have posted this question on SO as well: r - Debugging shiny - usage of trace() in case of reactives and observers - Stack Overflow

I would like to insert browser() function using trace() in shiny app. I know I could insert browser() manually, but I'm looking for the way to do this programmatically.

MRE:

library(shiny)
library(rlang)

ui <- fluidPage(
)

server <- function(input, output, session) {
  
  fun1 <- function() {
    a <- 1
  }
  
  fun2 <- reactive({
    b <- 2
  })
  
  observe({
    c <- 3
  })
  
  observe({
    trace(fun1, browser, where = env_parent(n = 2)) # it works
    fun1()
    #trace(fun2, browser, where = env_parent(n = 2)) # it doesn't work
    #fun2()
    #trace(observer_above, browser, where = env_parent(n = 2)) # no even idea how to refer
  })
}

shinyApp(ui, server)

I see it is possible for functions, but I can't do this in case of reactives and observers. Is it possible with trace() or debug() ?

EDIT:

in SO I got a link to "Debugging in Shiny" (Shiny - Debugging Shiny applications), where is paragraph saying:

What about trace()?

If you’re a seasoned R programmer, you may have used the trace() function to add tracing without modifying your script. Unfortunately, it’s not possible to use this utility (or any that depend on it, such as setBreakpoint) with Shiny. trace() works by rewriting the body of the function to be traced, so the function must already exist when you run it. Shiny generates functions at runtime that aren’t easily addressable.

However, I'm still skeptic, because this paragraph suggests that something is impossible because something is not easily addressable - it shouldn't be like that, right? If something is not easly addressable and that's the reason for something, then we would say that something is not easy, not impossible. And the second thing - this paragraph is saying about functions in general, but in my example it was possible to use trace() for one function which wasn't defined before app started.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.