[R-pkg-devel] update.formula does not keep.order

Chris Brien chr|@@br|en @end|ng |rom @de|@|de@edu@@u
Thu Oct 28 01:16:15 CEST 2021


Hi listers,



I have a package asremlPlus on CRAN that manipulates formulae. It uses the keep.order argument, as in the stats::terms function, to allow control over the order of terms in a model.



However, when stats::update.formula is used to update a formula, there is no keep.order argument for update.formula. The result is that the updated formula is always simplified to a sum of single terms and these terms are always re-ordered. I have written a function that is able to update a formula and keep the order. The following example illustrates the problem and my solution.



#Functions to keep.order when a formula is updated

update_keep_order <- function(object, ...) {

  UseMethod("update_keep_order")

}

update_keep_order.formula <- function(old, new, keep.order = TRUE) {

  tmp <- .Call(stats:::C_updateform, as.formula(old), as.formula(new))

  formula(terms.formula(tmp, keep.order = keep.order, simplify = TRUE))

}



#Generate some factors and a formula

facs <- expand.grid(A=1:2, B=1:2, C=1:2, D=1:2)

form <- with(facs, formula(~ A*B + C*D))



#Update with update.formula

(upd <- update(form, ~ . - C, keep.order = TRUE))



#Update with update_keep_order.formula

(upd_keep <- update_keep_order(form, ~ . - C, keep.order = TRUE))



However, the function calls the undocumented function stats::C_updateform and so cannot be added to my package.



Is there another solution to achieve this outcome that does not require this undocumented function and so could be incorporated in an R package on CRAN?



Thanks in advance for any help in solving this issue.



Cheers,



  Chris


	[[alternative HTML version deleted]]



More information about the R-package-devel mailing list