So I am trying to input the Y parameter of the tbl_uvregression function (gt_summary package) via a custom function. The idea is to create multiple tbl inside my function and return the different tables merged.
Here an example of the code I am using:
#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)
Here is the part where I have an issue :
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)
}
When I run this function outside of reg_log, I have no issue, but it seems that inside a function, the Y parameter of tbl_uvregression does not evaluate the argument, but instead read it literally. Here's the error I get when calling my function:
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 withmutate()
columnmodel
. imodel = map(...)
. x Error in svyglm.survey.design(formula = by ~ age, design = structure(list(: all variables must be in design= argument
I am aware that the Y parameter requires a syntax without the quotes, but even when I'm using the substitute() function it does not work. I have resolved myself to make several possibilities using the switch function, but if anyone knows how to resolve this, it will be awesome.
Thanks.
CodePudding user response:
The tbl_uvregression()
function expects an unquoted input for y=
, rather than a string with the outcome name. I updated your function to account for the string input.
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")
Created on 2021-11-12 by the reprex package (v2.0.1)