我在R中创建了一个有多个参数的函数。我希望能够在全局范围内调用这些参数,以便在函数之外使用。
如何轻松完成?我想也许match.fun()和match.arg()就是这里需要的。我这是对的吗?
我的功能如下:
HAC.sim <- function(K, N, Hstar, p, probs, perms = 10000){
specs <- 1:N
### Set up a container to hold the identity of each individual from each permutation
pop <- array(dim = c(c(perms, N), K))
### Create an ID for each haplotype
haps <- as.character(1:Hstar)
### Assign probabilities of occurrence to each haplotype, ensure they sum to 1
### This is where we assume we "know" the distribution of haplotypes
### Here, I have assumed they all occur with equal frequency, but you can change this to assume some dominant ones and some rare ones, whatever you want
# probs <- rep(1/Hstar, Hstar)
probs <- c(0.45, 0.45, rep(0.10/8, 8))
### Generate permutations, we assume each permutation has N individuals, and we sample those individuals' haplotypes from our probabilities
# If K > 1, haplotypes are partitioned into equally-sized subpopulations/demes
# Can change number of haplotypes in each subpopulation and re-run simulation
# For each additional, K, add new Ki and new pop[j ,, i] in loop
for(j in 1:perms){
for(i in 1:K){
if(i == 1){
pop[j, specs, i] <- sample(haps, size = N, replace = TRUE, prob = probs)
}
else{
pop[j ,, 1] <- sample(haps[K1], size = N, replace = TRUE, prob = probs[K1])
pop[j ,, 2] <- sample(haps[K2], size = N, replace = TRUE, prob = probs[K2])
}
}
}
### Make a matrix to hold the 1:N individuals from each permutation
HAC.mat <- array(dim = c(c(perms, N), K))
for(k in specs){
for(j in 1:perms){
for(i in 1:K){
ind.index <- sample(specs, size = k, replace = FALSE) ## which individuals will we sample
hap.plot <- pop[sample(1:nrow(pop), size = 1, replace = TRUE), ind.index, sample(1:K, size = 1, replace = TRUE)] ## pull those individuals from a permutation
HAC.mat[j, k, i] <- length(unique(hap.plot)) ## how many haplotypes did we get for a given sampling intensity (k) from each ### permutation (j)
}
}
}
### Calculate the mean and CI for number of haplotypes at each sampling intensity (j)
means <- apply(HAC.mat, MARGIN = 2, mean)
lower <- apply(HAC.mat, MARGIN = 2, function(x) quantile(x, 0.025))
upper <- apply(HAC.mat, MARGIN = 2, function(x) quantile(x, 0.975))
d <- data.frame(specs, means, lower, upper)
### Plot the curve and frequency barplot
par(mfrow = c(1, 2))
for(i in 1:K){
if(i == 1){
plot(specs, means, type = "n", xlab = "Specimens sampled", ylab = "Unique haplotypes", ylim = c(1, Hstar))
polygon(x = c(specs, rev(specs)), y = c(lower, rev(upper)), col = "gray")
lines(specs, means, lwd = 2)
HAC.bar <- barplot(N*probs, xlab = "Unique haplotypes", ylab = "Specimens sampled", names.arg = 1:Hstar)
}
else{
plot(specs, means, type = "n", xlab = "Specimens sampled", ylab = "Unique haplotypes", ylim = c(1, max(HAC.mat)))
polygon(x = c(specs, rev(specs)), y = c(lower, rev(upper)), col = "gray")
lines(specs, means, lwd = 2)
HAC.bar <- barplot(N*probs[get(paste0("K", i))], xlab = "Unique haplotypes", ylab = "Specimens sampled", names.arg = get(paste0("K",i)))
}
}
## Measures of Closeness ##
cat("\n Mean number of haplotypes sampled: " , max(means))
cat("\n Mean number of haplotypes not sampled: " , Hstar - max(means))
cat("\n Proportion of haplotypes sampled: " , max(means)/Hstar)
cat("\n Proportion of haplotypes not sampled: " , (Hstar - max(means))/Hstar)
cat("\n")
cat("\n Mean estimate of N*: ", (p*N*Hstar)/max(means))
}
HAC.sim(K = 1, N = 100, Hstar = 10, p = 0.95, probs = probs, perms = 10000)
我希望参数'p'可以传递给另一个函数。我应该在我的函数中使用省略号(...)来指定其他参数吗?
答案 0 :(得分:0)
如果我正确理解你的要求,这将演示如何从函数的参数中分配全局环境值。
> ls()
character(0)
> fn <- function(a, b, c) {
global_a <<- a
global_b <<- b
global_c <<- c
a*b*c
}
> ls()
[1] "fn"
> fn(2, 3, 4)
[1] 24
> ls()
[1] "fn" "global_a" "global_b" "global_c"
> global_a
[1] 2
但是,我会强烈鼓励您找到一种替代方法来解决您想要解决的问题,因为这种方法会导致很多麻烦。
如果您只对某些参数感兴趣,可以将参数作为属性传递给结果:
fn_attr <- function(a, b, c) {
res <- a * b * c
attr(res, "args") <- list(a = a, c = c)
res
}
> foo <- fn_attr(2, 3, 4)
> attr(foo, "args")$a
或获取所有参数值:
fn_attr_all <- function(a, b, c) {
args_vals <- as.list(environment())
res <- a * b * c
attr(res, "args") <- args_vals
res
}