Determining argument descriptions within R - r

I need a way to determine the description of an argument within R.
For example, if I'm using the function qplot() from the package ggplot2, I need to be able to extract a description of each argument in qplot(). From ggplot2's reference manual, it's easy to see that there are several arguments to consider. One of them is called "data", which is described as: "data frame to use (optional). If not specified, will create one, extracting vectors from the current environment."
Is there a way to get this information from within an R session, rather than by reading a reference manual? Maybe an R function similar to packageDescription(), but for a function's arguments?
Thanks!
edit: I found a variant on my question answered here:
How to access the help/documentation .rd source files in R?
Reading the .Rd files seems like the safest way to get the information I need. For anyone interested, the following code returns a list of arguments and their descriptions, where "package_name" can be any package you want:
db <- Rd_db("package_name")
lapply(db, tools:::.Rd_get_metadata, "arguments")
Thank you for your help, everyone.

From the R console in the Mac GUI R.app ... When I look at the text output from help'seq', help_type="text") (which goes to a temporary file) I see that the beginning of hte descriptions you want are demarcated by:
_A_r_g_u_m_e_n_t_s: # Those underscores were ^H's before I pasted
And then the arguments appear in are name:description pairs:
...: arguments passed to or from methods.
from, to: the starting and (maximal) end values of the sequence. Of
length ‘1’ unless just ‘from’ is supplied as an unnamed
argument.
by: number: increment of the sequence.
length.out: desired length of the sequence. A non-negative number,
which for ‘seq’ and ‘seq.int’ will be rounded up if
fractional.
along.with: take the length from the length of this argument.
When I use a Terminal session to get that same output it appears in the same window but as a Unix help page like:
Arguments:
...: arguments passed to or from methods.
from, to: the starting and (maximal) end values of the sequence. Of
length ‘1’ unless just ‘from’ is supplied as an unnamed
argument.
by: number: increment of the sequence.
length.out: desired length of the sequence. A non-negative number,
which for ‘seq’ and ‘seq.int’ will be rounded up if
fractional.
along.with: take the length from the length of this argument.
I believe these are displayed by whatever system program is called by the value of options("pager"). In my case, that is the program "less".

Related

Convert Numbers to Letters within a data variable column

I am trying to convert a set of variables in my data file from numbers to letters, such that every 2 is b every 3 is c and so on.
I have tried the following code and receive the error listed below:
B3_cfile$Row <- as.character(LETTERS(B3_cfile$Row))
Error: could not find function LETTERS
My code may also be incorrect as I am very new to R. However my understanding is that LETTERS is a function built into R that does not require an additional package.
As Phil writes in the comment, LETTERS is not a function, but a vector (an array), to access the content, you need to use brackets: LETTERS[B3_cfile$Row]
if you are new to R, you can check the types easily as:
str(LETTERS)

Is there a way to create a function that uses multiple argument, and run the same function independently for each one of them?

I'm pretty new in programming, but I'm trying to learn through practice. I'm coding a function which accepts a variable number of arguments, with the ... function. Here's an example of what I've done:
decyph_test = function(...) {
decript = letters[...]
return(decript)
}
If I enter only one argument (in this case, the required argument is a number, 'cause its used to locate that position in the letter constant) there's no big problem. But, what I want to do is to enter multiple numbers, so each one of them is located to its respective letter, and then is compiled into a vector, which would be the return of the function. Is there any specific way in which I can make any individual argument run the same process of locating its letter counterpart?

In Julia, how do I find out why Dict{String, Any} is Any?

I am very new to Julia and mostly code in Python these days. I am using Julia to work with and manipulate HDF5 files.
So when I get to writing out (h5write), I get an error because the data argument is of mixed type and I need to find out why.
The error message says Array{Dict{String,Any},4} is what I am trying to pass in, but when I look at the values (and it is a huge structure), I see a lot of 0xff and values like this. How do I quickly find why the Any and not a single type?
Just to make this an answer:
If my_dicts is an Array{Dict{String, Any}, 4}, then one way of working out what types are hiding in the Any part of the dict is:
unique(typeof.(values(my_dicts[1])))
To explain:
my_dicts[1] picks out the first element of your Array, i.e. one of your Dict{String, Any}
values then extracts the values, which is the Any part of the dictionary,
typeof. (notice the dot) broadcasts the typeof function over all elements returned by values, returning the types of all of these elements; and
unique takes the list of all these types and reduces it to its unique elements, so you'll end up with a list of each separate type contained in the Any partof your dictionary.

Function argument matching: by name vs by position

What is the difference between this lines of code?
mean(some_argument)
mean(x = some_argument)
The output is the same, but has the explicit mention of x any advantages?
People typically don't add argument names for commonly used arguments, such as the x in mean, but almost always refer to the na.rm arguments when removing missing values.
While neglecting the argument name makes for compact code, here are four (related) reasons for including the names of arguments rather than relying on their position.
Re-order arguments as needed. When you refer to the arguments by name, you can arbitrarily re-order the arguments and still produce the desired result. Sometimes it is useful to re-order your arguments. For example, when running a loop over one of the arguments, you might prefer to put the looped argument in the front of the function.
It is typically safer / more future-proof. As an example, if some user-written function or package re-orders the arguments in an update, and you relied on the positions of the arguments, this would break your code. In the best scenario, you would get an error. In the worst scenario the function would run, but would an incorrect result. Including the argument names greatly reduces the chances of running into either case.
For greater code clarity. If an argument is rarely used or you want to be explicit for future readers of your code (including you 2 months from now), adding the names can make for easier reading.
Ability to skip arguments. If you want to only change the third argument, then referring to it by name is probably preferable.
See also the R Language Definition: 4.3.2 Argument matching

Working with "..." input in R function

I am putting together an R function that takes some undefined input through the ... argument described in the docs as:
"..." the special variable length argument ***
The idea is that the user will enter a number of column names here, each belonging to a dataset also specified by the user. These columns will then be cross-tabulated in comparison to the dependent variable by tapply. The function is to return a table (independent variable x indedependent variable).
Thus, I tried:
plotter=function(dataset, dependent_variable, ...)
{
indi_variables=list(...); # making a list of the ... input as described in the docs
result=with (dataset, tapply(dependent_variable, indi_variables, mean); # this fails
}
I figured this should work as tapply can take a list as input.
But it does not in this case ('Error in tapply...arguments must have same length') and I think it is because indi_variables is a list of strings.
If I input the contents of the list by hand and leave out the quotation marks, everything works just fine.
However, if the user feeds the function the column names as non-strings, R will interpret them as variable names; and I cannot figure out how to transform the list indi_variables in the right way, unsuccessfully trying things like this:
indi_variables=lapply(indi_variables, as.factor)
So I am wondering
What causes the error described above? Is my interpretation correct?
How would one go about transforming the list created through ... in the right way?
Is there an overall better way of doing this, in the input or the implementation of tapply?
Any help is much appreciated!
Thanks to Joran's helpful reading, I have come up with these improvements than make things work out...
indi_variables=substitute(list(...));
result=with (dataset, tapply(dependent_variable, eval(indi_variables, dataset), FUN=mean));

Resources