faster alternative to for loop with two-argument function - r

I have a list of csv files called sourcefiles, and I want to apply a function with two arguments to all of files in sourcefiles. Here's what I'm doing now:
for (n in 1:length(sourcefiles)){
clcc(DT, n)
}
Is there any better way?
Thanks!

You can use lapply function:
lapply(X=aList, FUN=aFunction, otherParameters)
This function call aFunction for every item of the aList passing it as a first parameter and otherParameters as the other parameters.
The problem here is that your function clcc does not take the sourcefile as first parameter, but there is an easy workaround. If the formal name of the first parameter of the function clcc is DT (or whatever), you can call lapply by setting the name of it:
lapply(X=sourcefiles, FUN=clcc, DT=DT)

Related

Dynamically generating the name of the function to call

Is there a way to call a function when the name of the function is decided at the runtime? For example, calling pdf would look like:
pdf("myfile.pdf")
but is there a way, I could do something like:
media_type = "pdf"
media_type("myfile.pdf")
1) do.call Use do.call
do.call(media_type, list("myfile.pdf"))
2) match.fun Another approach is match.fun
fun <- match.fun(media_type)
fun("myfile.pdf")
3) switch Another approach is the following where an argument to switch would be added for each media type. stop is the default. It generates an error when called.
fun <- switch(media_type, pdf = pdf, stop)
fun("myfile.pdf")
4) eval/call This also works although the use of eval is generally frowned upon:
eval(call(media_type, "myfile.pdf"))
I'm not sure if this is what you're wanting, but given the example in your question, it is possible. Assuming you have code that determines which function you want to call, you can use do.call to pass in the string based function name. I had to wrap the input in a list to make it happy, but that's not a big deal most of the time.
f = "mean"
d = c(1,2,3)
do.call(f,list(d))
#> 2

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..

what is wrong with this list naming assignment?

Folks -
I'm going to keep my code here brief, as I think to those more familiar with R, it will be obvious. I am trying to use a function (not my own) that requires I feed it a list of named lists of parameters. I am having trouble naming the lists via a function I wrote to create each list element. Here is my function:
# for invoking grts
stratumdesign<- function(ns, points, oversamp) {
stratumname<-as.character(ns)
print("from function")
print(stratumname)
designlist<-list(ns=c(panel=points, seltype="Equal", over=oversamp))
return(designlist)
}
.. I have tried both having the function call have ns be the integer it is in the originating code, or be passed as a character. Neither work. What I'm illustrating here to myself w/in the function is that ns gets properly passed to the function, but the resulting list returned is always named "$ns" when I want it to be the value passed AS ns! What on Earth am I doing wrong, here?
Since this deserves an actual answer, not just a comment...
Try something more like this:
stratumdesign<- function(ns, points, oversamp) {
print("from function")
print(stratumname)
designlist<-list(c(panel=points, seltype="Equal", over=oversamp))
names(designlist) <- as.character(ns)
return(designlist)
}

Can an R function access its own name?

Can you write a function that prints out its own name?
(without hard-coding it in, obviously)
You sure can.
fun <- function(x, y, z) deparse(match.call()[[1]])
fun(1,2,3)
# [1] "fun"
You can, but just in case it's because you want to call the function recursively see ?Recall which is robust to name changes and avoids the need to otherwise process to get the name.
Recall package:base R Documentation
Recursive Calling
Description:
‘Recall’ is used as a placeholder for the name of the function in
which it is called. It allows the definition of recursive
functions which still work after being renamed, see example below.
As you've seen in the other great answers here, the answer seems to be "yes"...
However, the correct answer is actually "yes, but not always". What you can get is actually the name (or expression!) that was used to call the function.
First, using sys.call is probably the most direct way of finding the name, but then you need to coerce it into a string. deparse is more robust for that.
myfunc <- function(x, y=42) deparse(sys.call()[[1]])
myfunc (3) # "myfunc"
...but you can call a function in many ways:
lapply(1:2, myfunc) # "FUN"
Map(myfunc, 1:2) # (the whole function definition!)
x<-myfunc; x(3) # "x"
get("myfunc")(3) # "get(\"myfunc\")"
The basic issue is that a function doesn't have a name - it's just that you typically assign the function to a variable name. Not that you have to - you can have anonymous functions - or assign many variable names to the same function (the x case above).

Resources