英文:
In R, can I pass arguments of the parameter from a variable (only if that variable exists)?
问题
我正在创建一个Shiny应用程序,在这个应用程序中,用户会在应用程序中传递一些输入。根据这些输入,我尝试传递参数给名为'cohort()'的函数。然而,在传递参数给'cohort()'函数时,我遇到了问题。这是我的代码的相关部分:
这段代码循环创建多个基于用户输入的变量。
library(Capr)
for (i in seq(2)){
assign(paste0('ad_cr',i),atLeast(1L,
query = condition(cs(descendants(i)))))
}
假设创建了2个变量-> ad_cr1
和 ad_cr2
。
当没有变量生成时的函数:
library(Capr)
cd <- cohort(entry = condition(cs(100)),
attrition = attrition(),
exit = exit(
endStrategy = observationExit()
))
我试图将ad_cr1和ad_cr2的值传递给cohort()函数中的attrition参数。然而,我不确定如何做到这一点。我应该如何修改我的代码以实现这一目标?
当生成了两个变量时的函数:
cd <- cohort(entry = condition(cs(100)),
attrition = attrition(
'ad_cr1' = withAll(ad_cr1),
'ad_cr2' = withAll(ad_cr2)),
exit = exit(
endStrategy = observationExit()
))
希望这有帮助。
英文:
I am creating a shiny app where the user passes some inputs in the app. Based on those inputs, I am trying to pass arguments to the function called 'cohort()'. However, I am encountering issues in passing the arguments to the cohort() function. Here is the relevant portion of my code:
This code loops to create multiple variables based on user inputs.
library(Capr)
for (i in seq(2)){
assign(paste0('ad_cr',i),atLeast(1L,
query = condition(cs(descendants(i)))))
}
Let's assume that 2 variables were created -> ad_cr1
and ad_cr2
.
Function when the no variables were generated:
library(Capr)
cd <- cohort(entry = condition(cs(100)),
attrition = attrition(),
exit = exit(
endStrategy = observationExit()
))
I am trying to pass the values of ad_cr1 and ad_cr2 to the cohort() function within the attrition argument. However, I am not sure how to do this. How can I modify my code to achieve this?
Function when the two variables were generated:
cd <- cohort(entry = condition(cs(100)),
attrition = attrition(
'ad_cr1' = withAll(ad_cr1),
'ad_cr2' = withAll(ad_cr2)),
exit = exit(
endStrategy = observationExit()
))
答案1
得分: 0
避免使用本地变量,通过将变量放在一个列表中,可以将该列表传递给其他函数,如 attrition()
;最后,我将所有内容封装在一个可调用的函数中。
library(Capr)
library(purrr)
cohort_builder <- function(sequence, prefix) {
assign_names <- paste0(prefix, sequence)
assignations <- map(
sequence,
\(x)atLeast(1L,
query = condition(cs(descendants(x)))
)
) |-> set_names(assign_names)
cohort(
entry = condition(cs(100)),
attrition = do.call(
attrition,
map(assignations, \(x){
withAll(x)
})
),
exit = exit(
endStrategy = observationExit()
)
)
}
my_empty <- cohort_builder(sequence = NULL, prefix = NULL)
my_2 <- cohort_builder(sequence = seq(2), prefix = "ad_cr")
英文:
Avoid local variables, by keeping variables together in a list; this list can be passed off to other functions like attrition()
; finally I wrapped it all up in a function you can call.
library(Capr)
library(purrr)
cohort_builder <- function(sequence, prefix) {
assign_names <- paste0(prefix, sequence)
assignations <- map(
sequence,
\(x)atLeast(1L,
query = condition(cs(descendants(x)))
)
) |> set_names(assign_names)
cohort(
entry = condition(cs(100)),
attrition = do.call(
attrition,
map(assignations, \(x){
withAll(x)
})
),
exit = exit(
endStrategy = observationExit()
)
)
}
my_empty <- cohort_builder(sequence = NULL, prefix = NULL)
my_2 <- cohort_builder(sequence = seq(2), prefix = "ad_cr")
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论