6

While debug some R code, I'd like to save the workspace (i.e. all present objects) in some particular frame so that I can utilize those objects outside of the the debugging browser. Following the example given in this answer:

x <- 1:5
y <- x + rnorm(length(x),0,1)
f <- function(x,y) {
  y <- c(y,1)
  lm(y~x)
}

Setting options(error = recover) and running f(x,y) allows us to pick which frame to enter. Here I'll pick 2 and check my workspace with ls() like so:

Browse[1]> ls()
 [1] "cl" "contrasts" "data" "formula" "m" "method" "mf" "model" "na.action" "offset" "qr"         
[12] "ret.x" "ret.y" "singular.ok" "subset" "weights" "x" "y"

I'd like to be able to save all of these objects to use them later. Using save.image() in the browser, or inserting it into the relevant function, saves the environment f(x,y) was originally called from. I can use dump.frames() and call debugger() on the resulting dump.frames classed object, but I still have to work interactively from within the debugging browser. All I really want is an .RData file containing the 18 above listed objects.

The point of all this is to reproduce certain errors within an R Markdown document. If anyone has an idea for that particular application it would be appreciated.

Community
  • 1
  • 1
Zoë Clark
  • 1,334
  • 2
  • 13
  • 25
  • 1
    I would encourage you to ask about your R markdown errors, rather than ask how to implement a solution you've thought of. Someone might know the solution to your markdown errors that would make this question moot. – Joshua Ulrich Aug 02 '13 at 18:08
  • @JoshuaUlrich - in short, because I don't know how to get an `.Rmd` file to interact with the debugging browser. When `recover()` asks me to "Enter a frame number, or 0 to exit" how would I write that into an `.Rmd` file? The resulting `.html` shows the error and moves on to the next line of code. See [this gist](https://gist.github.com/duncandonutz/9f0fcd0aeff8278c8fa0). – Zoë Clark Aug 02 '13 at 19:51

1 Answers1

9
save(list=ls(), file="mylocals.Rda")

The hurdle I had to get over to realize this was the way forward was the name of that argument in save. Why did the authors use the argument name, "list", when it was a character vector (and not a list)? Same whine applies to the rm function argument names.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Why not just `save.image(file="mylocals.Rda")`? – hadley Aug 02 '13 at 19:12
  • 2
    @hadley: The `save.image` code says it does this: `save(list = ls(envir = .GlobalEnv, all.names = TRUE), ...)` – IRTFM Aug 02 '13 at 19:24
  • Sorry, I was tricked (again!) by the documentation, which says: "save.image() is just a short-cut for ‘save my current workspace’, i.e., save(list = ls(all = TRUE), file = ".RData")." – hadley Aug 02 '13 at 19:43
  • @DWin - I'd love a quick "why" in addition to the "how". – Zoë Clark Aug 02 '13 at 20:06
  • Color me puzzled. I thought you had provided the why and only needed the how. – IRTFM Aug 02 '13 at 20:23
  • Nevermind, it's all about the difference between `ls(envir = .GlobalEnv)` and `ls()` while inside of the browser, which I understand now. Thanks! – Zoë Clark Aug 02 '13 at 20:54
  • Sorry to be so dense. I thought you already understood that `ls()` was only returning the local objects in the browsing environment and that those would be different than the objects resident in `.GlobalEnv`. – IRTFM Aug 02 '13 at 20:56