Rstudio own Method/Function minihelp [duplicate] - r

This question already has answers here:
Is it possible to get RStudio to show function arguments and descriptions for custom functions?
(2 answers)
Closed 5 years ago.
If I write a method or function, how can i get that "minihelp" (whose special term I dont know) which is shown while writing a function?
E.g. for "plot" it exists; Type
>plot([TAB]
then the following shows in a kind of "Tooltip"
x=
y=
... =
and if you choose for example "x", then after a second the following tooltip shows:
x
the coordinates of points in the plot. Alternatively, a single plotting
structure, function or any R object with a plot method can be provided.
Some info but not crucial to the problem: I am working with Rstudio and writing multiple S4 generics/methods for the class ExpressionSet.
Sadly I can't manage to find a cool google keyword, so I hope you can help me out!
Edith:
The following question is about the same as mine but i have still the ongoing problem that I already wrote a package, every function is documented with roxygen, I followed hadleys descriptions. Nevertheless, the tooltips do not show up.
In the linked question it is said that "help files must be generated" - which I assume are generated as ?myS4Method is showing the appropriate help pages. Any ideas?

The functionality you are looking for comes from the way RStudio parses the documentation of packages. If you create a package, you can add Roxygen comments to your functions or classes. These comments are then parsed when you create the package into documentation files that you see as the help for a function.
If you run the command ?plot you will see a list of Arguments. These are the parameters that can be passed to the function and that is what the tooltip in RStudio is telling you about.
To get RStudio to give you info about the functions you are using, you should bundle your S4 classes into a package (Hadley Wickham's tutorial) and make sure they are correctly documented. RStudio will take care of the rest.

Related

Using R's exams package for assignments: Is it possible to add question hints?

The exams package is a really fantastic tool for generating exams from R.
I am interested in the possibilities of using it for (programming) assignments. The main difference from an exam is that besides solutions I'd also like hints to be included in the PDF / HTML output file.
Typically I put the hints for (sub)-questions in a separate section at the end of the PDF assignment (using a separate Latex section), but this requires manual labour. These are for students to consult if they need help getting started on any particular exercise, and it avoids having them look at the solutions directly for hints on how to start.
An assignment might look like:
Question 1
Question 2 ...
Question 10
Hints to all questions
I'd be open to changing the exact format as long it is possible to look up hints without looking up the answer, and it remains optional to read the hints.
So in fact I am looking for an intermediate "hints" section between the between the "question" and "solution" section, which is present for some questions but not for all.
My questions: Is this already possible? If not, how could this be implemented using the exams package?
R/exams does not have dedicated/native support for this kind of assignment so it isn't available out of the box. So if you want to get this kind of processing you have to ensure it yourself using LaTeX for PDF or CSS for HTML.
In LaTeX I think it should be possible to do what you want using the newfloat and endfloat packages in the LaTeX template that you pass to exams2pdf(). Any LaTeX template needs to provide {question} and {solution} environments, e.g., the plain.tex template shipped with the package has
\newenvironment{question}{\item \textbf{Problem}\newline}{}
\newenvironment{solution}{\textbf{Solution}\newline}{}
with the exercises embedded as
\begin{enumerate}
%% \exinput{exercises}
\end{enumerate}
Now instead of the \newenvironment{solution}... you could use
\usepackage{newfloat,endfloat}
\DeclareFloatingEnvironment{hint}
\DeclareDelayedFloat{hint}{Hint}
\DeclareFloatingEnvironment{solution}
\DeclareDelayedFloat{solution}{Solution}
This defines two new floating environments {hint} and {solution} which are then declared delayed floats. And then you would need to customize these environments regarding the text displayed within the questions at the beginning and the listing at the end. I'm not sure if this can get you exactly what you want, though, but hopefully it is a useful place to start from.

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?
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:
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 :
(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!

Hide the in-function plot from R package [duplicate]

This question already has an answer here:
How can I suppress the creation of a plot while calling a function in R?
(1 answer)
Closed 6 years ago.
I am using the R package "GeoDE". When I use the function "chdirAnalysis", a figure will be plotted automatically, since there is a command "plot" in the source code of "chdirAnalysis". But I don't want that. How can I stop this?
A similar problem is to hide the in-function printed messages, and I've found the solution which is to use invisible
capture.output(value <- function_name(input))
That can help hide the output from "function_name", but this solution doesn't work on the plot.
Options:
Ask the maintainer to add a plot=FALSE option to the function (and maybe a verbose=FALSE option to stop the text outputs).
Edit the source for chdirAnalysis and remove the function call that does the plotting, or hide it behind a new plot=FALSE options. I think this is chdirplots, which is called but doesn't do anything with its return value. If you are doing this outside the GeoDE package source then you'll need to add the GeoDE::: prefix to any unexported GeoDE functions called by chdirAnalysis (such as chdirSig).
Make it plot to some dummy or throwaway graphics device file, as described in other questions and answers.

List of functions of a package [duplicate]

This question already has answers here:
Show names of everything in a package
(4 answers)
Closed 5 years ago.
Is there an easy, friendly way to list all functions of a package without downloading those huge PDFs (package references)? I need this for getting me familiar with the package, finding proper functions etc.
I tried ?rjags but it doesn't do what I expected.
Load the package (for example the carpackage). Then use ls()
ls("package:car")
The closest thing I've been able to find for this is:
help(,"rjags")
The first parameter specifies the searched thing, second one specifies the package. By keeping only the second one, I hope to get all help pages that relate to that package. This is equivalent of
help(package = "rjags")
This might not work in general though, as in ?help the functionality of omitting the first parameter is described as
topic is not optional: if it is omitted R will give
If a package is specified, (text or, in interactive use only, HTML) information on the package, including hints/links to suitable help
topics.

View the source of an builtin R package [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
View the source of an R package
I want to see the source code of stats::reorder.
This answer seems not apply to built in packages which are compiled to bytecode:
> stats::reorder
function (x, ...)
UseMethod("reorder")
>bytecode: 0x103321718<
>environment: namespace:stats<
This has nothing to do with reorder being compiled to bytecode and everything to do with it being a generic function.
My answer here elaborates on this.
But specifically for this situation if you want to see the code you can use
# Find what methods are available for reorder
methods(reorder)
# Attempt to check out the code for reorder.default
reorder.default
# Use getAnywhere to view code regardless of if it is exported
getAnywhere(reorder.default)
As others have said, you want methods(reorder). But for your mode general question, the best way is to download the source code of R, and search the code with grep. You can also browse the code online but it's not always obvious in which file a particular function might live.
This isn't a matter of compilation, what you're seeing is the result of the fact that reorder is written to do different things depending on the class of what you want to reorder. There are separate reorder functions for different possible options, and you can list them by calling methods(reorder). You can then examine the source of whichever one is appropriate.

Resources