我有一个R语言的问题--我想创建一个函数向量,并且能够通过名称调用其中一个函数。然而,当我使用这个名称时,我想使用一个标签来映射该名称,这样我就可以更改使用的名称而不必更改代码。例如:
#define tag
tag<-"F"
#define functions
f <- function(x) print(x^2)
g <- function(x) print(x^3)
#define vector
fs<-c(f,g)
names(fs)<-c("F", "G")
#create input data
x<-5
fs$F(x)
#this gives the desired output but I want to use tag
#that is, I want syntax which uses tag, so that which element I use from fs is flexible until tag is defined
#e.g. I had hoped the following would work, but it doesn't
fs[tag](x)
有什么建议吗?