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

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

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_cr1ad_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(&#39;ad_cr&#39;,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 &lt;- 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 &lt;- cohort(entry = condition(cs(100)),
         attrition = attrition(
                   &#39;ad_cr1&#39; = withAll(ad_cr1),
                   &#39;ad_cr2&#39; = 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 &lt;- function(sequence, prefix) {
  assign_names &lt;- paste0(prefix, sequence)
  assignations &lt;- map(
    sequence,
    \(x)atLeast(1L,
      query = condition(cs(descendants(x)))
    )
  ) |&gt; set_names(assign_names)


  cohort(
    entry = condition(cs(100)),
    attrition = do.call(
      attrition,
      map(assignations, \(x){
        withAll(x)
      })
    ),
    exit = exit(
      endStrategy = observationExit()
    )
  )
}

my_empty &lt;- cohort_builder(sequence = NULL, prefix = NULL)
my_2 &lt;- cohort_builder(sequence = seq(2), prefix = &quot;ad_cr&quot;)

</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:

确定