How to create a vector of functions?
Use a factory function with a closure over its argument (which will hold the value of the looping variable):
> # the factory function
> makefunc <- function(x) { x; function() x }
> funclist <- list()
> for (i in 1:3) funclist[[i]] <- makefunc(i)
> funclist[[1]]()
[1] 1
> funclist[[2]]()
[1] 2
>
R: How to create a vector of functions?
This can be solved using an eval-parse construct, although I strongly advice you not to use this construct. It often causes more problems than anything else. But I couldn't get a decent do.call way of doing it.
vector_of_functions = NULL
for (i in 1:4) {
vector_of_functions = c(vector_of_functions,
eval(parse(text=paste("function(number) func(number=number, coefficient=",i,")"))))
}
Reason is as Aaron explained: everything within the function definition is taken as is until the function evaluation.
Small remark: this is especially a list of functions, and not a vector. It's impossible to have a vector of type "function". It's also absolutely useless, as you have to select the function using the index [[]] before you can use it. Then I'd just add the argument instead of defining a function for every possible value of one of the arguments.
So what you want to achieve is unclear, but if you want to apply func with different coefficients, I wonder why you don't simply do:
> x <- c(10,20,30)
> sapply(1:4,function(y)func(number=x,coefficient=y))
[,1] [,2] [,3] [,4]
[1,] 10 20 30 40
[2,] 20 40 60 80
[3,] 30 60 90 120
A variation on the theme by Marek (avoiding the parsing):
vector_of_functions = NULL
for (i in 1:4) {
vector_of_functions = c(vector_of_functions,
eval(substitute(function(number) func(number=number, coefficient=i),list(i=i))))
}
The 1L etc. you get, just indicate they're exact integers (that take less memory space).
How do I define a list/vector of functions in R?
If you want to have a list of functions, you can do something like:
myFuns <- list(mean, sd)
And then you can lapply
over this list, or use the for
loop as you wanted. If you use the for
loop make sure that you use the [[
syntax, because this makes sure that you are retrieving the function and not a length one list:
for (i in 1:n){
output[i] <- WrapperFunction(x, myFuns[[i]])
}
or
lapply(myFuns, WrapperFunction, x = x)
Create a list of functions from a vector of characters
Maybe initialize your list with a single generic function, and then update them using:
foo <- function(x){x+3}
> body(foo) <- quote(x+4)
> foo
function (x)
x + 4
More specifically, starting from a character, you'd probably do something like:
body(foo) <- parse(text = "x+5")
How to declare a vector of functions (lambdas)
Use std::function
with the corresponding type:
std::vector<std::function<void(void)>> vec;
vec.push_back([]()->void{});
In your case it would be std::function<void(std::string)>
.
The exact type of a lambda is meaningless per standard ([expr.prim.lambda]/3):
The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type
How can I make a std::vector of function pointers?
std::vector<void (CChristianLifeMinistryEntry::*)(CString)> pfnSetAssignName = xx;
create vector of array of string and void function pointer in c++
I think you're looking for a vector
of pair<std::string, void (*)()
as shown below:
#include <iostream>
#include <utility>
#include <string>
#include <vector>
void foofunc()
{
std::cout<<"foofunc called"<<std::endl;
//do something here
}
void barfunc()
{
std::cout<<"barfunc called"<<std::endl;
//do something here
}
int main()
{
//create a vector of pair of std::string and pointer to function with no parameter and void return type
std::vector<std::pair<std::string, void (*)()>> a;
//add elements into the vector
a.push_back({"foo", &foofunc});
a.push_back({"bar", &barfunc});
//lets try calling the functions inside the vector of pairs
a[0].second();//calls foofunc()
a[1].second(); //calls barfunc()
return 0;
}
The output of the above program can be seen here.
Related Topics
Boxplot, How to Match Outliers' Color to Fill Aesthetics
Find Elements Not in Smaller Character Vector List But in Big List
R Error: Unknown Timezone with As.Posixct()
How to Remove Na Data in Only One Columns
Add Titles to Ggplots Created with Map()
Doing T.Test for Columns for Each Row in Data Set
Merging Data.Tables Based on Columns Names
R Data.Table Conditional Aggregation
Global Variable in a Package - Which Approach Is More Recommended
R Ggplot2: Labeling a Horizontal Line Without Associating the Label with a Series
R Plots: How to Draw a Border, Shadow or Buffer Around Text Labels
How to Install R-Packages Not in the Conda Repositories
How to Import Only One Function from Another Package, Without Loading the Entire Namespace
Rename Columns in Multiple Dataframes, R
Rsqlite Query with User Specified Variable in the Where Field
Findassocs for Multiple Terms in R