0

As a non-programmer I find tracing non-fatal errors which occur outside the global environment (functions, markdown, shiny) challenging. Usually I write and debug code in the global environment and then recreate it as a function or in markdown or shiny. Unfortunately this is cumbersome at best and presumably there is a better way. I know that function environments vanish upon completion of the function. Do markdown and shiny environments behave similarly? What is the best way to get a "snapshot" of the variables which were created? Is there a completely different approach I should be considering?

Ron Sokoloff
  • 130
  • 1
  • 11
  • 4
    This seems like you are asking for very broad general debugging advice which is difficult to provide a specific answer for. Can you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of some code you are trying to debug? Have you used the built in debugger `?browser`? This question currently seems to vague to be answerable. – MrFlick Nov 06 '17 at 21:02
  • 1
    If you would like to investigate some of the variables after the non-fatal function execution, you may return those variables in a list: `return(list(...))` – Heikki Nov 06 '17 at 21:02
  • If you want to debug a shiny app, this page is helpful: https://shiny.rstudio.com/articles/debugging.html – Kelli-Jean Nov 06 '17 at 21:37
  • Adding this debugging statement to your function `assign(".save", environment(), .GlobalEnv)` will place a snapshot of the current environment in your workspace for later examination. Alternately, `assign(".save", as.list(environment()), .GlobalEnv)` to save it as a list. – G. Grothendieck Nov 06 '17 at 21:37

1 Answers1

0

My prefered practise when debugging shiny apps is to place a browser() statement in the section i want to debug.

Now when you are running the app it will run as usual until it finds the browser statement it will pause and let you run the code step by step or try your own code and see the value of all the variables availeble in this context. When you are finished you can continue running the code as usual.

Sometimes I even use this to code new code. Especially together with the autocomplete from R-Studio can this be quite powerfull.

Bertil Baron
  • 4,923
  • 1
  • 15
  • 24