简体   繁体   English

inject() function 来自 rlang package 无法与来自 rms package 的 Predict() function 一起工作 in R

[英]inject() function from rlang package cannot work with Predict() function from rms package in R

Context语境

I am learning use tidy eval to write my own function.我正在学习使用 tidy eval 来编写我自己的 function。

If I calculate hr1 without using tidy eval, the code works fine, such as:如果我在不使用 tidy eval 的情况下计算hr1 ,则代码可以正常工作,例如:

hr1 = Predict(fit, meal.cal = q, fun = exp, ref.zero = TRUE)

When I use rlang::inject() and rms::Predict() jointly to calculate hr1 , an error occurs, such as:当我联合使用rlang::inject()rms::Predict()计算hr1时,出现错误,例如:

hr1= rlang::inject(Predict(fit, ,,x = q. fun = exp, ref.zero = TRUE))

Question问题

How to use rlang::object() and rms::Predict() correctly jointly?如何正确联合使用rlang::object()rms::Predict()

Reproducible code可重现的代码

library(survival)
library(rms)
data(cancer)

x = sym('meal.cal')

q = quantile(lung[[x]], probs = c(0.05, 0.35, 0.65, 0.95), na.rm = T)

fit = rlang::inject(cph(Surv(time, status) ~ rcs(!!x), data = lung))

dd = datadist(lung)
options(datadist = 'dd') # Predict() cannot work without set the options

hr1 = Predict(fit, meal.cal = q, fun = exp, ref.zero = TRUE) # Run successful
head(hr1)
# meal.cal      yhat     lower    upper
# 1      338 1.2486497 0.7310498 2.132722
# 2      825 0.9916446 0.7766063 1.266226
# 3     1039 1.0245228 0.8826652 1.189179
# 4     1425 1.0558754 0.6322319 1.763392
# 
# Response variable (y):  
#   
#   Limits are 0.95 confidence limits

hr1= rlang::inject(Predict(fit, !!x = q, fun = exp, ref.zero = TRUE)) # Run failed
# Error: unexpected '=' in "hr1= rlang::inject(Predict(fit, !!x ="

The left-hand side of a named parameter (ie = inside a call) must be a name, it cannot be an expression.命名参数的左侧(即调用中的= )必须是名称,不能是表达式。 To work around this limitation, you can use splicing here:要解决此限制,您可以在此处使用拼接

hr1 = rlang::inject(Predict(fit, !!! setNames(list(q), "foo"), fun = exp, ref.zero = TRUE))

In general, 'rlang' allows := in the place of = for named arguments to work around the R syntax limitation but for some reason inject does not support this.通常,'rlang' 允许:=代替 named arguments 的=来解决 R 语法限制,但由于某些原因, inject不支持此功能。

As @Lionel Henry notes in the comments, another workaround is to use a regular do.call where we need to supply the arguments in list form and we use that together with rlang::list2() which allows the use of := and splicing on the lefthand side:正如@Lionel Henry 在评论中指出的那样,另一种解决方法是使用常规do.call ,我们需要以列表形式提供 arguments 并将其与rlang::list2()一起使用,这允许使用:=和拼接在左手侧:

library(survival)
library(rms)
data(cancer)

do.call(Predict, rlang::list2(fit, !! x := q, fun = exp, ref.zero = TRUE))

#>   meal.cal      yhat     lower    upper
#> 1      338 1.2486497 0.7310498 2.132722
#> 2      825 0.9916446 0.7766063 1.266226
#> 3     1039 1.0245228 0.8826652 1.189179
#> 4     1425 1.0558754 0.6322319 1.763392
#> 
#> Response variable (y):  
#> 
#> Limits are 0.95 confidence limits

Created on 2022-09-28 by the reprex package (v2.0.1)reprex package (v2.0.1) 创建于 2022-09-28

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM