Printing name of object defined as a function in R - r

I have read In R, how to get an object's name after it is sent to a function? and have a similar question. The above gets the object name after it is defined. What I want is to get the name while it is being defined in a function i.e. print the object name while the function is being called.
The thing that is different is that I want to find the name of the object before it is defined in the local environment.
I want my function foo to print the object name in the output as well. So my function foo would look something like this.
foo<-function(x) {
print('name of object that is calling foo')
return(x)
}
This will return the character string "name of object that is calling foo" and 1. What I want is for the function to print "object" and 1 when I call object = foo(1). I know that match.call() returns the function and arguments but I cant find a function that will return the object name that is currently calling the function.
A plausible workaround that I can think of would go through the history and print the first object that matches the match.call() but I hope that theres a simpler way to do this.
Feel free to ask me for any clarification.

Related

Changing imported R function globally

I want to globally add a parameter to a function after import. So in future function calls the function should be always called with the set parameter.
In this case, I want to add the function parameter in_schema("abc") to the function tbl from dplyr.
Normally, I would use the source code and modify the function parameters, save and source it. But in this case, I am already failing to get a proper source code file.
getAnywhere("tbl.DBIConnection")
A single object matching 'tbl.DBIConnection' was found
It was found in the following places
registered S3 method for tbl from namespace dplyr
namespace:dplyr
with value
function (src, from, ...)
{
check_dbplyr()
tbl(dbplyr::src_dbi(src, auto_disconnect = FALSE), from = from,
...)
}
How could I modify the tbl-function (in a script file) so future calls always use a certain Scheme?
like so:
tbl(connection, table, in_schema("abc"))
without having to provide the in_schema parameter all the time.
Don't copy and modify the function, it's messy, do something like this instead :
tbl_abc <- function(src, from, ...){
tbl(src, in_schema("abc", from), ...)
}
btw tbl(connection, table, in_schema("abc")) is improper syntax, in_schema("abc") needs a second argument, and it's passed to the ..., which are not used by tbl.DBIConnection()

Call Arguments of Function inside Function / R language

I have a function:
func <- function (x)
{
arguments <- match.call()
return(arguments)
}
1) If I call my function with specifying argument in the call:
func("value")
I get:
func(x = "value")
2) If I call my function by passing a variable:
my_variable <-"value"
func(my_variable)
I get:
func(x = my_variable)
Why is the first and the second result different?
Can I somehow get in the second call "func(x = "value")"?
I'm thinking my problem is that the Environment inside a function simply doesn't contain values if they were passed by variables. The Environment contains only names of variables for further lookup. Is there a way to follow such reference and get value from inside a function?
In R, when you pass my_variable as formal argument x into a function, the value of my_variable will only be retrieved when the function tries to read x (if it does not use x, my_variable will not be read at all). The same applies when you pass more complicated arguments, such as func(x = compute_my_variable()) -- the call to compute_my_variable will take place when func tries to read x (this is referred to as lazy evaluation).
Given lazy evaluation, what you are trying to do is not well defined because of side effects - in which order would you like to evaluate the arguments? Which arguments would you like to evaluate at all? (note a function can just take an expression for its argument using substitute, but not evaluate it). As a side effect, compute_my_variable could modify something that would impact the result of another argument of func. This can happen even when you only passed variables and constants as arguments (function func could modify some of the variables that will be later read, or even reading a variable such as my_variable could trigger code that would modify some of the variables that will be read later, e.g. with active bindings or delayed assignment).
So, if all you want to do is to log how a function was called, you can use sys.call (or match.call but that indeed expands argument names, etc). If you wanted a more complete stacktrace, you can use e.g. traceback(1).
If for some reason you really wanted values of all arguments, say as if they were all read in the order of match.call, which is the order in which they are declared, you can do it using eval (returns them as list):
lapply(as.list(match.call())[-1], eval)
can't you simply
return paste('func(x =', x, ')')

Function that returns one of its argument

I heard it in a podcast, that, there is a function which takes two args, and returns one of them, and it doesn't tell you which one.
What's the name of this function?
That podcast is about functional reactive programming, and the host itself created a modified version of such a function, so that it can be predicted that which arg will be returned.
function random(a,b){
var list = [a,b];
var random_01 = Math.floor(Math.random()*2);
return list[random_01];
}
This is my solution in JavaSript.
I do not know the name of that function, but it could be named like Binary option..

Getting method parameter names

Given the following Go method:
func (t *T) TMethod(data *testData) (interface{}, *error) {
...
}
I want to reflect the name of the parameter (which is data here).
I tried the following, but it returns the structure name (which is testData here):
reflect.ValueOf(T).MethodByName("TMethod").Type.In(0).Elem().Name()
How can I get the name of the parameter?
There is no way to get the names of the parameters of a method or a function.
The reason for this is because the names are not really important for someone calling a method or a function. What matters is the types of the parameters and their order.
A Function type denotes the set of all functions with the same parameter and result types. The type of 2 functions having the same parameter and result types is identical regardless of the names of the parameters. The following code prints true:
func f1(a int) {}
func f2(b int) {}
fmt.Println(reflect.TypeOf(f1) == reflect.TypeOf(f2))
It is even possible to create a function or method where you don't even give names to the parameters (within a list of parameters or results, the names must either all be present or all be absent). This is valid code:
func NamelessParams(int, string) {
fmt.Println("NamelessParams called")
}
For details and examples, see Is unnamed arguments a thing in Go?
If you want to create some kind of framework where you call functions passing values to "named" parameters (e.g. mapping incoming API params to Go function/method params), you may use a struct because using the reflect package you can get the named fields (e.g. Value.FieldByName() and Type.FieldByName()), or you may use a map. See this related question: Initialize function fields
Here is a relevant discussion on the golang-nuts mailing list.

R: How can a function assign a value to a variable that will persist outside the function?

This is probably easy but I am confused as hell with environments. I would like to use a call to a function to assign a value to a variable, but I need to be able to use that variable after the call. However, I assume, the variable created by the function only exist within the function environment. In short, I need something akin to a global variable (but I am a beginner with R).
The following code :
assignvalue = function(varname){
assign(varname,1)
}
assignvalue("test")
test
returns Error: object 'test' not found whereas I would like it to be equivalent to
test <- 1
test
I read something in the documentation of assign about environments, but I could not understand it.
Say foo is a parameter of your function. Simply do this:
assignvalue <- function(foo) {
varname <<- foo
}
assignvalue("whatever")
varname
The variable varname will point out to the value "whatever".

Resources