The five minutes you spend each week will provide you with a building block you can use in the next two hours at work. Review language basics, discover methods to improve existing R code, explore new and interesting features, and learn about useful development tools and libraries that will make your time programming with R that much more productive.
All series code samples can be downloaded at https://github.com/mnr/five-minutes-of-R.
Skill Level Intermediate
- [Instructor] Sometimes it's helpful to be able to examine the inside of a function to find out what the programmer intended or maybe some of the hidden features that a programmer has placed inside. So let's take a minute to look at how you can examine the inside and return values of functions. I've already installed the CalSAVE package and then we can play with it a little bit. You'll recall that say is a function from CalSAVE, and I can say, say good morning (keys clacking) by a cat, (keys clacking) and when I hit return I get a cat that says good morning.
Very convenient. But what is actually going on inside of say? And the way that you can do that is just simply typing in the function without the parentheses and when I hit return, what you'll see is the actual code is being run when I type in say. In this case here's the function definition, what=Hello world, by=cat, type=message and the code then that uses those parameters. (keys clacking) Now there are other functions we can use. One of them is ls.
And I can type in type in parentheses, let's go with the package colon cowsay and what this will do is tell us what the various parts are available for the package cowsay and you can see that there are three elements available. One of them is animals, one of them is endless_horse, and one of them is say. Well, we've already played with say. Let's take a quick look at endless_horse. And there is endless_horse, endless_horse is a function that uses what=hello world and endless is true, so what does that do? Let's go ahead and select that, and run it, and what we get is a horse that is, well, endless.
I hit Escape to get rid of the endless horse and Control + L to clear the screen. Now there's always the help file that you can rely on. If I type in question mark, and then cowsay, and let's say that I wanna pull up a particular part of cowsay, I can go, cowsay, colon, colon, let's look at animals and find out what cowsay animals actually is. The question mark pulls up the help file for that and if we go over to the help file panel in our studio, you can see that there's a description of the animals part of the package.
In this case, it's a named vector of animals. Now, if I'd like to take another look at what animals actually is, I can use the str. Commonly mistaken as string, it's actually structure of animals. What this will do, is describe to us the names and structure of animals. So in this case, it's a named character and there are 41 elements inside of this particular vector. As you work in R, experiment with these different functions to find out what's going on with the programs that you're using.
As you use these functions in your own programming, you'll learn a bit about what other programmers have done, and how they're structuring the functions that you're using in your day to day work.