关于函数内 tbl_uvregression 函数中 Y 参数的问题

Issue concerning the Y parameter in tbl_uvregression function inside a function

所以我试图通过自定义函数输入 tbl_uvregression 函数(gt_summary 包)的 Y 参数。我的想法是在我的函数中创建多个 tbl,然后 return 合并不同的表。

这里是我使用的代码示例:

#Loading libraries + example dataset from questionr package

library(haven)
library(tidyverse)
library(finalfit)
library(dplyr)
library(survey)
library(srvyr)
library(gtsummary)
library(glue)
library(gt)
library(knitr)
library(questionr)

data(hdv2003)

这是我遇到问题的部分:

reg_log <- function(dataframew, variables, by) {
  #@param1 : weighted dataframe
  #@param2 : vector containing variables we want in our graph
  #@param3 : the variable or column we want as our Y argument

  Table <- tbl_uvregression(data = dataframew, include = variables, exponentiate = TRUE, method.args = list(family = quasibinomial()), y = by, method = survey::svyglm)
  return(Table)
}

当我在 reg_log 之外 运行 这个函数时,我没有问题,但似乎在函数内部, tbl_uvregression 的 Y 参数不计算参数,而是逐字阅读。这是调用函数时出现的错误:

hdv2003w <- svydesign(ids = ~1, data = hdv2003, weights = ~hdv2003$poids) #setting the survey.design object

reg_log(hdv2003w, c("age", "sexe", "hard.rock", "sport"), "sport")

x There was an error constructing model survey::svyglm(formula = by ~ age, design = ., family = quasibinomial()) See error below. Erreur : Problem with mutate() column model. i model = map(...). x Error in svyglm.survey.design(formula = by ~ age, design = structure(list(: all variables must be in design= argument

我知道 Y 参数需要不带引号的语法,但即使我使用 substitute() 函数它也不起作用。我已经决定自己使用 switch 函数做出几种可能性,但是如果有人知道如何解决这个问题,那就太棒了。

谢谢。

tbl_uvregression() 函数需要 y= 的不带引号的输入,而不是带有结果名称的字符串。我更新了您的函数以说明字符串输入。

library(gtsummary)
library(questionr)
data(hdv2003)

reg_log <- function(dataframew, variables, by) {
  tbl_uvregression(
    data = dataframew, 
    include = all_of(variables), 
    exponentiate = TRUE, 
    method.args = list(family = quasibinomial()), 
    y = !!rlang::sym(by), 
    method = survey::svyglm
  )
}

hdv2003w <- survey::svydesign(ids = ~1, data = hdv2003, weights = ~hdv2003$poids) #setting the survey.design object

tbl <- 
  reg_log(hdv2003w, c("age", "sexe", "hard.rock"), "sport")

reprex package (v2.0.1)

创建于 2021-11-12