Join Mark Niemann-Ross for an in-depth discussion in this video Debugging with browser(), part of Code Clinic: R.
- At several points in this course, I'll show you how the code works. To do this, I'll use the R function called Browser. Browser stops program execution and opens a debugging session. Let's take a quick look at how this works. Here I have a program, demonstrate_browser.r with a simple loop that counts to 10. Let's go ahead and take a look at that. First of all, delete that. And I'll go ahead and select all the text and hit the button that says Run.
And what that does is it goes ahead and runs all of the highlighted code. You can see that the console window now shows one through 10 and in the global environment, I have an index value of 10, and a test run vector that contains the numbers one through 10. I also have a function defined called dosomething. Well let's slow this down and take a look at how it actually worked. What I'll do is I'll click into this source code and I'll type in the word, Browser with a parentheses.
Now what that's gonna do is when it hits the dosomething function, it will stop and bring us into the Browser. In order to make this work, I need to save this file, and I also need to source it, which is to bring it into local memory. When I click on the source button, it's not only going to load it into memory, it's going to immediately run it and you'll see something change. So let's go ahead and click on Source. And the change that you've seen happens in the upper left hand corner, of course, where we see the browser is now highlighted on line six and there's a small green arrow.
In the console window, you can see that we now have the words, Next, a couple of mysterious green arrows, Continue, and the word Stop. Plus, the prompt itself also says Browse. All of this indicates to us that we're in the debugger mode now. Once we're in debugger, I can take a look at different values. So for example, in the environment area in the upper right hand corner, I see that I have a value called x and I can determine what the value is by going to the console, and clicking on x, and I hit enter.
And you'll see that the value of x is equal to one. I can also take a look at the structure of x with the command structure or str and I type in a parentheses because it's a function and the vector that I would like to take a look at and I hit return and I see that x happens to be an int, which is an integer. Now, what can I do in the browser? Well, I can go ahead and I can step to the next iteration through the function. To do that, I'll go ahead and in the console in the left hand corner, I'm going to click on the word Next.
And what that will do is go to the next command, which says, print the value of x, line seven, in the upper left hand corner. And if I go ahead and say, yep, that's fine, go ahead and do that, we'll see that in the console now on the third line from the bottom is the value one, which happen to be the value of x when the print command line is seven is called. Because we're in a loop in dosomething, it's going to call the Browser again, which we can ignore, but now I'll call Next and it's going to print out another value.
It's going to print the value of x again, and we can confirm that that's in fact the value of x again, by going to the console in the lower left hand corner, typing in x, and, oh, we can see now that the value is equal to three. That's because lines 10, 11, and 12 are stepping through the values of testrun. We can go ahead and see what testrun looks like just simply by typing in that variable, and we can see that testrun happens to contain the numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
We can also check the structure of testrun. And we can see that it's a vector of type int with 10 values and the values happen to be one through 10. When I'm done using the browser, I can go ahead and either hit Continue, which will run the program all the way through, or if things are really messed up I can go ahead and hit Stop and it will stop execution of the program exactly where I'm at and bring me back to the original place I was in. That's what Browser does.
It brings you into the R Studio debugger and allows you to take a look at values. We'll use this extensively as we work through the different problems.
Mark introduces challenges and then provides an overview of his solutions in R. Challenges include topics such as statistical analysis, searching directories for images, and accessing peripheral devices.
Skill Level Intermediate
Q: R Studio tells me that it can't find files I expect to be available. Where can I find them?
A: Use the setwd() command to set the working directory to match the folder you're working in.
Q: I am unable to access the Lake Pend Oreille data from outside the U.S.
A: A static copy of this data is provided here for lynda.com members outside of the U.S.