Skip to content

Commit

Permalink
plot_GrowthCurve():
Browse files Browse the repository at this point in the history
+ fx stack call problem with .toFormula()
  • Loading branch information
RLumSK committed Sep 25, 2023
1 parent f1acde1 commit c209ced
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions R/plot_GrowthCurve.R
Original file line number Diff line number Diff line change
Expand Up @@ -499,20 +499,6 @@ plot_GrowthCurve <- function(

# FITTING ----------------------------------------------------------------------
##3. Fitting values with nonlinear least-squares estimation of the parameters
## helper function to create fit formula
## from the function
.toFormula <- function(x) {
## deparse
tmp <- deparse(x)

## get parentheses position
id_par <- which(grepl(pattern = "[{}]", x = tmp))

## get equation
tmp_eq <- paste(trimws(tmp[(id_par[1]+1):(id_par[2]-1)]), collapse = "")

return(as.formula(paste0("y ~", tmp_eq)))
}

##set functions for fitting
#EXP
Expand Down Expand Up @@ -794,7 +780,6 @@ plot_GrowthCurve <- function(
b <- b.MC[i]
c <- c.MC[i]

writeLines(paste0("CHECK HERE", .toFormula(fit.functionEXP)))
fit.initial <- suppressWarnings(try(nls(
formula = .toFormula(fit.functionEXP),
data = data,
Expand Down Expand Up @@ -2331,7 +2316,7 @@ plot_GrowthCurve <- function(

}

# Helper functions --------------------------------------------------------
# Helper functions in plot_GrowthCurve() --------------------------------------
#'@title Replace coefficients in formula
#'
#'@description Replace the parameters in a fitting function by the true, fitted values.
Expand Down Expand Up @@ -2371,3 +2356,33 @@ plot_GrowthCurve <- function(
return(parse(text = str))
}

#'@title Convert function to formula
#'
#'@decription The fitting functions are provided as functions, however, later is
#'easer to work with them as expressions, this functions converts to formula
#'
#'@param f [function] (**required**): function to be converted
#'
#'@param env [environment] (*with default*): environment for the formula
#'creation. This argument is required otherwise it can cause all kind of
#'very complicated to-track-down errors when R tries to access the function
#'stack
#'
#'@md
#'@noRd
.toFormula <- function(f, env = parent.frame()) {
## deparse
tmp <- deparse(f)

## get parentheses position
id_par <- which(grepl(pattern = "[{}]", x = tmp))

## get equation
tmp_eq <- paste(trimws(tmp[(id_par[1]+1):(id_par[2]-1)]), collapse = "")

## set formula
tmp_formula <- as.formula(paste0("y ~", tmp_eq), env = env)

return(tmp_formula)
}

0 comments on commit c209ced

Please sign in to comment.