在R中,我可以将参数的参数从一个变量传递(仅在该变量存在时)吗?

huangapple go评论91阅读模式
英文:

In R, can I pass arguments of the parameter from a variable (only if that variable exists)?

问题

我正在创建一个Shiny应用程序,在这个应用程序中,用户会在应用程序中传递一些输入。根据这些输入,我尝试传递参数给名为'cohort()'的函数。然而,在传递参数给'cohort()'函数时,我遇到了问题。这是我的代码的相关部分:

这段代码循环创建多个基于用户输入的变量。

  1. library(Capr)
  2. for (i in seq(2)){
  3. assign(paste0('ad_cr',i),atLeast(1L,
  4. query = condition(cs(descendants(i)))))
  5. }

假设创建了2个变量-> ad_cr1ad_cr2

当没有变量生成时的函数:

  1. library(Capr)
  2. cd <- cohort(entry = condition(cs(100)),
  3. attrition = attrition(),
  4. exit = exit(
  5. endStrategy = observationExit()
  6. ))

我试图将ad_cr1和ad_cr2的值传递给cohort()函数中的attrition参数。然而,我不确定如何做到这一点。我应该如何修改我的代码以实现这一目标?

当生成了两个变量时的函数:

  1. cd <- cohort(entry = condition(cs(100)),
  2. attrition = attrition(
  3. 'ad_cr1' = withAll(ad_cr1),
  4. 'ad_cr2' = withAll(ad_cr2)),
  5. exit = exit(
  6. endStrategy = observationExit()
  7. ))

希望这有帮助。

英文:

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.

  1. library(Capr)
  2. for (i in seq(2)){
  3. assign(paste0(&#39;ad_cr&#39;,i),atLeast(1L,
  4. query = condition(cs(descendants(i)))))
  5. }

Let's assume that 2 variables were created -> ad_cr1 and ad_cr2.

Function when the no variables were generated:

  1. library(Capr)
  2. cd &lt;- cohort(entry = condition(cs(100)),
  3. attrition = attrition(),
  4. exit = exit(
  5. endStrategy = observationExit()
  6. ))

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:

  1. cd &lt;- cohort(entry = condition(cs(100)),
  2. attrition = attrition(
  3. &#39;ad_cr1&#39; = withAll(ad_cr1),
  4. &#39;ad_cr2&#39; = withAll(ad_cr2)),
  5. exit = exit(
  6. endStrategy = observationExit()
  7. ))

答案1

得分: 0

避免使用本地变量,通过将变量放在一个列表中,可以将该列表传递给其他函数,如 attrition();最后,我将所有内容封装在一个可调用的函数中。

  1. library(Capr)
  2. library(purrr)
  3. cohort_builder <- function(sequence, prefix) {
  4. assign_names <- paste0(prefix, sequence)
  5. assignations <- map(
  6. sequence,
  7. \(x)atLeast(1L,
  8. query = condition(cs(descendants(x)))
  9. )
  10. ) |-> set_names(assign_names)
  11. cohort(
  12. entry = condition(cs(100)),
  13. attrition = do.call(
  14. attrition,
  15. map(assignations, \(x){
  16. withAll(x)
  17. })
  18. ),
  19. exit = exit(
  20. endStrategy = observationExit()
  21. )
  22. )
  23. }
  24. my_empty <- cohort_builder(sequence = NULL, prefix = NULL)
  25. 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.

  1. library(Capr)
  2. library(purrr)
  3. cohort_builder &lt;- function(sequence, prefix) {
  4. assign_names &lt;- paste0(prefix, sequence)
  5. assignations &lt;- map(
  6. sequence,
  7. \(x)atLeast(1L,
  8. query = condition(cs(descendants(x)))
  9. )
  10. ) |&gt; set_names(assign_names)
  11. cohort(
  12. entry = condition(cs(100)),
  13. attrition = do.call(
  14. attrition,
  15. map(assignations, \(x){
  16. withAll(x)
  17. })
  18. ),
  19. exit = exit(
  20. endStrategy = observationExit()
  21. )
  22. )
  23. }
  24. my_empty &lt;- cohort_builder(sequence = NULL, prefix = NULL)
  25. my_2 &lt;- cohort_builder(sequence = seq(2), prefix = &quot;ad_cr&quot;)
  26. </details>

huangapple
  • 本文由 发表于 2023年6月16日 04:25:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定