5

I'm using RStudio and I want to be able to stop the code execution at a specific line.

The functions are defined in the first script file and called from a second.

I source the first file into the second one using source("C:/R/script1.R")

I used run from beginning to line: where I start running from the second script which has the function calls and have highlighted a line in the first script where the function definitions are.

I then use browser() to view the variables. However this is not ideal as there are some large matrices involved. Is there a way to make these variables appear in RStudio's workspace?

Also when I restart using run from line to end it only runs to the end of the called first script file it does not return to the calling function and complete the running of the second file.

How can I achieve these goals in RStudio?

OK here is a trivial example the function adder below is defined in one script

adder<-function(a,b) {  
  browser()
  return(a+b)
 }

I than call is from a second script

x=adder(3,4)

When adder is called in the second script is starts browser() in the first one. From here I can use get("a") to get the value of a, but the values of a and b do not appear in the workspace in RStudio?

In the example here it does not really matter but when you have several large matrices it does.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
Bazman
  • 2,058
  • 9
  • 45
  • 65
  • Is the problem that an error is being thrown in script 2? Try `options(error = recover)`. – Richie Cotton Sep 20 '12 at 15:59
  • Sorry not sure what happened but I restarted and now its returning to the calling function. Thanks Hugh – Bazman Sep 20 '12 at 16:04
  • Would still like to know how to add the variables in the functions being browsed() to RStudios workspace. – Bazman Sep 20 '12 at 16:06
  • When you call `browser`, you just pause the execution of what you were doing. All the variables in the current environment should automatically show in the RStudio environment browser. If you want more specifics, then please provide a reproducible example in your question. – Richie Cotton Sep 20 '12 at 16:08
  • No probs how can I attach the script files? – Bazman Sep 20 '12 at 16:13
  • Don't attach your original scripts, you need to create a _minimal_ example that demonstrates your problem. I know this sounds like a pain, but it makes the question more applicable to other people, and the process of distilling it down into something smaller often helps clarify things in your mind. – Richie Cotton Sep 20 '12 at 16:22
  • OK have given a simple example as requested. Please take each code section and save it as a separate script. Then run the first one and then the second (or source the first one in the second if you prefer). When you run the second script browser() will start and you can find the value of a using get("a"), but the value of a (or b) do not appear in RStudio's workspace. – Bazman Sep 20 '12 at 17:22

2 Answers2

3

What you refer to as RStudio's workspace is the global environment in an R session. Each function lives in its own small environment, not exposing its local variables to the global environment. Therefore a is not present in the object inspector of RStudio.

This is good programming practice as it shields sections of a larger script from each other, reducing the amount of unwanted interaction. For example, if you use i as a counter in one function, this does not influence the value of a counter i in another function.

You can inspect a when you are in the browser session by using any of the usual functions. For example,

head(a)
str(a)
summary(a)
View(a)
attributes(a)

One common tactic after calling browser is to get a summary of all variables in the current (parent) environment. Make it a habit that every time you stop code with browser, you immediately type ls.str() at the command line.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • Well I have large matrices that I need to compare with each other this not easily done in the browser session? Ideally I could see them all in the workspace. Is is possible to change the environment to which RStudio's workspace points? – Bazman Sep 20 '12 at 21:18
  • 1
    @HughPatience, maybe something like [.ls.objects](http://stackoverflow.com/questions/1358003/tricks-to-manage-the-available-memory-in-an-r-session) would be useful instead of RStudio's "Workspace" tab. – GSee Sep 20 '12 at 21:22
  • You can inspect the objects on the command line using e.g. `str` or `summary`. This works fine in a browser session. I don't use the object inspector myself coming from a text editor + terminal background. – Paul Hiemstra Sep 20 '12 at 21:24
  • @HughPatience you can "send" those big matrices to the global environment via `<<-` (there are special cases where this may not hold, see `?"<<-"`). – Roman Luštrik Sep 21 '12 at 09:16
3

If you assign the data, into the .GlobalEnv it will be shown in RStudio's "Workspace" tab.

> adder(3, 4)
Called from: adder(3, 4)
Browse[1]> a
[1] 3
Browse[1]> b
[1] 4
Browse[1]> assign('a', a, pos=.GlobalEnv)
Browse[1]> assign('b', b, pos=.GlobalEnv)
Browse[1]> c
[1] 7
> a
[1] 3
> b
[1] 4
GSee
  • 48,880
  • 13
  • 125
  • 145