So here is it as txt file. It is funny that a R file is restricted in R-devel mailing list.

Anyhow, in this case R-core have a few choices here:

 * ignore my solution
 * show that it is actually bad or worse
 * consider adding it

Considering, that it is a minor change from previous version and doesn't bother the existing usage, I saw the necessity to submit it here. But newer solution in the 3.6.0 may solve other problems too. I can't argue against that. This solves my part of the problem, without affecting existing usage of the function.

If R-core is hard to convince, because this is just who they are, then I should consider moving to other platforms. But so far, it seems to me that they are doing a great job. I don't mind also someone rejecting this tiny fix I have found, which works for me now. I can only thank for their time spent considering it.

Actually, I had in mind a more complex but cleaner solution with recursive functions to implement any kind of reformulation (not only with "+"). But I simple lack the big picture on R expressions, I need to read more. Maybe I will come back with that in the future.

Cheers to all,

Saren

On 18.04.19 17:51, Ben Bolker wrote:
   I appreciate your enthusiasm and persistence for this issue, but I
suspect you may have trouble convincing R-core to adopt your changes --
they are "better", "easier", "more intuitive" for you ... but how sure
are you they are completely backward compatible, have no performance
issues, will not break in unusual cases ... ?
--
Saren Tasciyan
/PhD Student / Sixt Group/
Institute of Science and Technology Austria
Am Campus 1
3400 Klosterneuburg, Austria


# Current problematic code in R 3.5.3
reformulateProb <- function (termlabels, response=NULL, intercept = TRUE)
{
        if(!is.character(termlabels) || !length(termlabels))
                stop("'termlabels' must be a character vector of length at 
least one")
        has.resp <- !is.null(response)
        termtext <- paste(if(has.resp) "response", "~",
                                          paste(termlabels, collapse = "+"),
                                          collapse = "")
        if(!intercept) termtext <- paste(termtext, "- 1")
        rval <- eval(parse(text = termtext, keep.source = FALSE)[[1L]])
        if(has.resp) rval[[2L]] <-
                if(is.character(response)) as.symbol(response) else response
        ## response can be a symbol or call as  Surv(ftime, case)
        environment(rval) <- parent.frame()
        rval
}

# My simple solution
reformulateMySol <- function (termlabels, response=NULL, intercept = TRUE) {
        if(!is.character(termlabels) || !length(termlabels))
                stop("'termlabels' must be a character vector of length at 
least one")
        has.resp <- !is.null(response)
        
        termtext <- paste(if(has.resp) "response", if(has.resp) "~",
                                          paste(paste0("`", termlabels, "`"), 
collapse = "+"),
                                          collapse = "")
        if(!intercept) termtext <- paste(termtext, "- 1")
        rval <- eval(parse(text = termtext, keep.source = FALSE)[[1L]])
        if(has.resp) rval[[2L]] <-
                if(is.character(response)) as.symbol(response) else response
        ## response can be a symbol or call as  Surv(ftime, case)
        environment(rval) <- parent.frame()
        rval
}

# Current development with str2lang, which I don't have and didn't/couldn't test
reformulateRDevel <- function (termlabels, response=NULL, intercept = TRUE, env 
= parent.frame())
{
        ## an extension of formula.character()
        if(!is.character(termlabels) || !length(termlabels))
                stop("'termlabels' must be a character vector of length at 
least one")
        termtext <- paste(termlabels, collapse = "+")
        if(!intercept) termtext <- paste(termtext, "- 1")
        terms <- str2lang(termtext)
        fexpr <-
                if(is.null(response))
                        call("~", terms)
        else
                call("~",
                         ## response can be a symbol or call as  Surv(ftime, 
case)
                         if(is.character(response))
                                tryCatch(str2lang(response),
                                                 error = function(e) {
                                                        sc <- sys.calls()
                                                        sc1 <- lapply(sc, `[[`, 
1L)
                                                        isF <- function(cl) 
is.symbol(cl) && cl == quote(reformulate)
                                                        reformCall <- 
sc[[match(TRUE, vapply(sc1, isF, NA))]]
                                                        
warning(warningCondition(message = paste(sprintf(
                                                                "Unparseable 
'response' \"%s\"; use is deprecated.  Use as.name(.) or `..`!",
                                                                response),
                                                                
conditionMessage(e), sep="\n"),
                                                                class = 
c("reformulate", "deprecatedWarning"),
                                                                call = 
reformCall)) # , domain=NA
                                                        as.symbol(response)
                                                 })
                         else response,
                         terms)
        formula(fexpr, env)
}

# Test control
reformulateProb(termlabels = c("Var1", "Var2"), response = "Resp")
# Test problem
reformulateProb(termlabels = c("Va r1", "Var2"), response = "Resp")


# Test control
reformulateMySol(termlabels = c("Var1", "Var2"), response = "Resp")
# Test problem
reformulateMySol(termlabels = c("Va r1", "Var2"), response = "Resp")


# Test control
reformulateRDevel(termlabels = c("Var1", "Var2"), response = "Resp")
# Test problem
reformulateRDevel(termlabels = c("Va r1", "Var2"), response = "Resp")
______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to