如何在 R 函数之间传递参数?
How to pass arguments among R functions?
我有 2 个小函数(DoAnalysis()
和 ProcessRes()
)和 1 个将调用这 2 个小函数的最终函数(PerformSim()
)。下面列出了详细的示例函数。
每个函数包含一个唯一的参数(非重叠参数):
- 函数
PerformSim()
包含 1 个唯一参数 nsim
;
- 函数
DoAnalysis()
包含 1 个唯一参数 obj
;
- 函数
ProcessRes()
包含 1 个唯一参数 results
对于剩余的参数,函数DoAnalysis()
具有完整的集合,函数DoAnalysis()
包含函数DoAnalysis()
的参数的子集,因此,函数PerformSim()
需要包含调用函数 DoAnalysis()
和 ProcessRes()
.
的信息
以下代码不起作用,因为我不确定如何在函数之间正确传递参数。
请提前提供帮助并感谢您的帮助。
library(purrr)
DoAnalysis <- function(obj, nS1 = 40, nS2 = 40, nS3 = 20, cutoff) {
cumS2 <- nS1 + nS2 ## this is the total number of patients at the end of Stage 2
nmax <- cumS2 + nS3 ## this is the total number of patients at the end of study
## compute number of events for 1st stage
Res_n <- sum(obj[1:nS1]) ## this is the number of response in Stage 1
## compute number of events for 2nd stage
Res_n2 <- sum(obj[1:cumS2]) ## this is the number of response in Stage 2
### Now, it is the final analysis
Res_F <- sum(obj[1:nmax]) ## this is the number of response in Final
list(Res_n, Res_n2, Res_F)
}
ProcessRes <- function(results, cutoff){
part1 <- part2 <- part3 <- c()
for (i in 1:length(results)){
part1 <- as.vector(c(part1, results[[i]][[1]]))
part2 <- as.vector(c(part2, results[[i]][[2]]))
}
resultsTypeI <- as.data.frame(cbind(part1, part2, part3))
names(resultsTypeI) <- c("Res_n", "Res_n2")
resultsTypeI$fulFlag <- ifelse(resultsTypeI$Res_n2 < cutoff, 1, 0)
R_1 <- mean(resultsTypeI$fulFlag)
return(c(R_1))
}
set.seed(20201022)
PerformSim <- function(nsim, nS1 = 40, nS2 = 40, nS3 = 20, cutoff = 26){
## this is the simulation
total <- nS1 + nS2 + nS3
SimuTypeI <- map(1:nsim, ~rbinom(total, 1, 0.4))
results <- map(SimuTypeI, ~DoAnalysis(.x))
ProcessRes(results = results)
}
PerformSim(nsim = 1000)
您的所有函数定义和参数看起来都不错,但在您的 PerformSim()
函数定义中,您必须在 ProcessRes()
中传递参数 cutoff
。像这样。
PerformSim <- function(nsim, nS1 = 40, nS2 = 40, nS3 = 20, cutoff = 26){
## this is the simulation
total <- nS1 + nS2 + nS3
SimuTypeI <- map(1:nsim, ~rbinom(total, 1, 0.4))
results <- map(SimuTypeI, ~DoAnalysis(.x))
ProcessRes(results = results, cutoff)
}
我有 2 个小函数(DoAnalysis()
和 ProcessRes()
)和 1 个将调用这 2 个小函数的最终函数(PerformSim()
)。下面列出了详细的示例函数。
每个函数包含一个唯一的参数(非重叠参数):
- 函数
PerformSim()
包含 1 个唯一参数nsim
; - 函数
DoAnalysis()
包含 1 个唯一参数obj
; - 函数
ProcessRes()
包含 1 个唯一参数results
对于剩余的参数,函数DoAnalysis()
具有完整的集合,函数DoAnalysis()
包含函数DoAnalysis()
的参数的子集,因此,函数PerformSim()
需要包含调用函数 DoAnalysis()
和 ProcessRes()
.
以下代码不起作用,因为我不确定如何在函数之间正确传递参数。
请提前提供帮助并感谢您的帮助。
library(purrr)
DoAnalysis <- function(obj, nS1 = 40, nS2 = 40, nS3 = 20, cutoff) {
cumS2 <- nS1 + nS2 ## this is the total number of patients at the end of Stage 2
nmax <- cumS2 + nS3 ## this is the total number of patients at the end of study
## compute number of events for 1st stage
Res_n <- sum(obj[1:nS1]) ## this is the number of response in Stage 1
## compute number of events for 2nd stage
Res_n2 <- sum(obj[1:cumS2]) ## this is the number of response in Stage 2
### Now, it is the final analysis
Res_F <- sum(obj[1:nmax]) ## this is the number of response in Final
list(Res_n, Res_n2, Res_F)
}
ProcessRes <- function(results, cutoff){
part1 <- part2 <- part3 <- c()
for (i in 1:length(results)){
part1 <- as.vector(c(part1, results[[i]][[1]]))
part2 <- as.vector(c(part2, results[[i]][[2]]))
}
resultsTypeI <- as.data.frame(cbind(part1, part2, part3))
names(resultsTypeI) <- c("Res_n", "Res_n2")
resultsTypeI$fulFlag <- ifelse(resultsTypeI$Res_n2 < cutoff, 1, 0)
R_1 <- mean(resultsTypeI$fulFlag)
return(c(R_1))
}
set.seed(20201022)
PerformSim <- function(nsim, nS1 = 40, nS2 = 40, nS3 = 20, cutoff = 26){
## this is the simulation
total <- nS1 + nS2 + nS3
SimuTypeI <- map(1:nsim, ~rbinom(total, 1, 0.4))
results <- map(SimuTypeI, ~DoAnalysis(.x))
ProcessRes(results = results)
}
PerformSim(nsim = 1000)
您的所有函数定义和参数看起来都不错,但在您的 PerformSim()
函数定义中,您必须在 ProcessRes()
中传递参数 cutoff
。像这样。
PerformSim <- function(nsim, nS1 = 40, nS2 = 40, nS3 = 20, cutoff = 26){
## this is the simulation
total <- nS1 + nS2 + nS3
SimuTypeI <- map(1:nsim, ~rbinom(total, 1, 0.4))
results <- map(SimuTypeI, ~DoAnalysis(.x))
ProcessRes(results = results, cutoff)
}