Why does ltrace show so many parameters and arguments? - ltrace

It seems like ltrace is showing more parameters then the functions provide, take for instance this
getauxval(31, 0x7ffe5ee5a5c0, 0, 0x7066732e6d657473) = 0x7f4f747cd030
Why are there 4 parameters there, two integers two memory locations rather than one parameter which is all Linux's getauxval seems to support? What's the right way to read the output of ltrace?

I've stumbled upon the same question. After some research and comparison with the source file, I found out that these arguments are the values of the stack variables at the time the function is called. So If you expect (know) your function to have only 1 argument, this will be the first parameter, everything after that are the variables on the function stack

Related

R: Getting more informative error messages in R

I am still not very good at using R’s standard debugging tools, and I often find that neither the error nor the traceback tell me enough to figure out what is going on. I would like to change R’s default behavior on error to provide more information.
Specifically, I would always like the call, including the formals, the expression assigned to each formal (the default expression if the default is the expression assigned), and the value of each of the argument expressions as evaluated in place, all returned in a format that makes it unambiguous which expression has been matched to which formal and which values go with which expression. Since the values might be large or of unexpected or evanescent type, I’d like them to be returned in a format, such as a str(), that makes intelligent choices about truncation and correctly identifies promises and other object types that tend to evaluate themselves into something else before you see them.
And finally, I’d like all these things, together with the return value of each call, for every function on the call stack from the error back to (and including) some piece of code that I wrote. It seems to me that the natural structure would be a single R object, a list of lists, one list per call (perhaps tidied, broom-like, into a tibble with some list columns) that I could single-step through in the obvious way.
I apologize if I have described some standard R debugging tool that I just haven't learned how to use properly yet. Is this even possible? If it is, could it be implemented via R's available error handlers, or would it need some package-scale coding project?
I would most prefer a solution that changes the default error response to this, but if that is impracticable, I'd accept a solution that requires that I rerun a code chunk with a wrapper or something similar.

R: What does `rm(list=ls())` do? [duplicate]

This question already has answers here:
What is the difference between rm() and rm(list=ls())?
(2 answers)
Closed 4 years ago.
While looking at some R code written by someone else, I find near the top of the document rm(list=ls()). I tried looking up what the rm function does and as far as I can tell it deletes specified variables from the workspace, so that if you had x=3 somewhere, then after running rm(x) the variable x would behave as if it had never been assigned (so for instance would throw an error if you tried to print it).
But then the rest of the code is strange if I have this right, because it seems to be removing the empty list. So it's not doing anything at all, right? Is there a reason to have this code here?
I think the parameter list for the rm function is confusing, at least it was for me when I started using R. The list parameter is not actually supposed to be a list but rather a character vector. The ls-function does return a character vector of all the named objects visible in the environment where it is called and it has a default value that gets used if no other value is offered for its envir argument. If you do this at the console, then the default environment is the global environment. So this will clear all the "visible" objects (but not the ones defined in other namespaces or environments like the ones that exist in the base, graphics, stats or other loaded package-namespaces.
So now go look at ?ls and ?rm to better understand their capabilities. In particular new useRs should get their heads clear on the differences between R names, i.e. symbols, versus the character representations of them. The ls function is really reaching into the language level of R's implementation and returning a non-language-level value, whereas rm typically takes a language level input ... unless (as in this case) it is offered a value to its "list"-argument, ... which is not an R list. Clear? Maybe, hopefully, a bit more so.
It clears all objects from the workspace.

Why do the R functions mean() and sum() behave differently with vectors vs. raw strings?

I was wondering if there was an underlying programming logic as to why some basic R functions behave differently towards raw data input into them vs. vectors.
For example, if I do this
mean(1,2,3)
I don't get the correct answer, and don't get an error
But if I do this
sum(1,2,3)
I do get the right answer, even though I'd assume proper syntax would be sum(c(1,2,3))
And if I do this
sd(1,2,3)
I get an error Error in sd(1, 2, 3) : unused argument (3)
I'm interested into what, if any, the underlying programming logic of these different behaviors are. (I'm sure if I rooted around in the source code I could figure out exactly why they behave differently, but I want to know if there is a reason why the code might have been written that way).
Practically, I'm teaching a basic R class and want to explain to my students why things work that way; they get a bit tired of me saying "That's just how R works, live with it; and always put things in vectors to make life easy."
EDITS: I have bolded some sections to add emphasis. My question is largely about software design, not how these particular function happen to operate or how to determine their exact operation. That is, not "what arguments do these functions accept" but "why do simple mathematical functions in R appear (to a biologist) to have been designed differently".
the second argument taken by mean is trim, which is not a listed argument for sum. the first argument for sum is \dots, so, I believe, the function will try to compute the sum of all values entered as unnamed arguments.
mean and sum are generic functions, so they get deployed differently depending on an object's class.

How to check all the arguments of a function in R

Usually in html help of a package in R argument list, ends with
........ other arguments passed.
but how can we print all the arguments of a function in R.
If I understand your question correctly, then I tend to say: Most of the time, it is not possible to list all the possible arguments that may be passed under the ... 'section' of the function.
Please look at the very simple ?plot function. There, only two arguments, x and y are given on the help page. However, under ... there is a range of additional possible arguments. Most of them are (as the help page says) graphical parameters. In the ?lm case, I understand that the arguments in ... are passed to lm.fit and lm.wfit. Check those help pages to see which parameters these functions take.
I guess the main problem is that you have to check ALL functions, that arguments under ... may be passed to, to know ALL the possible arguments that may be passed under .... Since the number of functions may be very large, also the number of arguments working in ... may be very large. So we don't want to have them on the "top-level" help page.
I hope that made any sense...

Returning Multiple Output Parameters from Optim

Im running an optimisation routine using optim in R and im telling the programme what i want returned. for example, if i put return(op1$par), it will return all 4 of my variable values. Thats fine, and if i run return(op1), I obviously get all the information from the optimisation routine (par, value, convergence etc). However, in this format, the par values arent accessible in the output, it simply details that there are 4 values.
Now what i need is to the get the parameter values and the convergence information at the same time. R wont let me call this return(op1$par, op1$convergence) so im looking for the best way to get these two entities in one run?
I should specify that im writing this to a file for 1000s of iterations and not just looking to call it up once on screen.
Cheers
Try something like this:
return(c(Parameters=op1$par, Convergence=op1$convergence))
The names Parameters and Convergence are only for identifying what are the parameters and what is the convergence, since this result will be a vector.
By design, a function can return only one object (or else assignments like a <- fn(b) would get confusing; which thing do you assign?). But that object can be a vector, or a list (which is what optim does). So wrap your arguments in something like
return(c(par=op1$par, convergence=op1$convergence))
or more generally (for objects of different types),
return(list(par=op1$par, convergence=op1$convergence))

Resources