Max_IT
Max_IT

Reputation: 626

how to make comments appear from custom functions

using RStudio I have noticed that, when calling a function, I can hit tab and a popup will appear with the possible parameters that can be chosen, e.g. if I type round( and hit tab, x= and digits= will appear as possible choices. This also happens with the custom functions I write. The difference is that built-in functions popups also have comments and explanations regarding the individual parameters. Is it possible to recreate such a behavior with custom functions as well?

Upvotes: 3

Views: 2108

Answers (1)

lebelinoz
lebelinoz

Reputation: 5068

I see what you mean. If you write a customised function

foo = function(x,y) { ... }

Then you go foo( and hit tab, the code completion pop-up menu will give you the options x = and y =. However, when you type an existing R function such as round(, not only does tab give you the options, but there's an explanation beneath each variable, telling you its role in the function:

enter image description here

The only way I could think of doing this for your own functions is to package your functions in your own customised package, and to make sure the "help" documentations includes your functions' parameters. This is getting beyond the realm of a stackoverflow question, but I'll point you to a couple of blogs where I learned the basics of R packages.

The Not So Standard Deviation blog explains how to write a simple package with help documentation, which is precisely what you need to see your customised functions appear with explanations inside RStudio's autocomplete. In a nutshell, you'll need to install roxygen2, devtools and, with each customised function, you'll need to thoroughly comment the function like this :

enter image description here

(disclaimer: the goofy cat example is the blogger's, not mine)

Here's a more detailed tutorial on creating R packages, and here's another blog on getting organised with R packages. Good luck!

Upvotes: 5

Related Questions