"Object Not Found" Error Within a User Defined Function, Eval() Function

Object not found error within a user defined function, eval() function?

Try this. You need to keep in mind that R doesn't know what's my.time and my.event. You have to parse them with quotes and then unqoute them in order to parse it into Surv

myfun<-function(dflist,time,event){
for (i in 1:length(dflist)){
time <- noquote(time)
event <- noquote(event)
out<-cch(Surv(dflist[[i]][, time], dflist[[i]][, event])~as.factor(my.det), data=dflist[[i]],
subcoh=~sub, id=~my.id, cohort.size=500)
print(out)}
}
myfun(my.list,"my.time","my.event")

What is the cause of the 'not found' error in my user-defined function?

As we pass unquoted variable, use {{}} (curly-curly opertor) for evaluation

export_ids <- function(var) {

export_df <- df %>%
filter(!is.na({{var}})) %>%
select({{var}})

write_csv(export_df, "~/Downloads/final_ids.csv")

}

-testing

export_ids(wspid)

user defined function in r: object not found

You have not defined value of age in paste("coxph(Surv(", time, ", ", status, "==2) ~", covariate, R is searching for its value.

object not found error from eval inside a function

We can specify the envir in teval as environment (using R 3.3.0)

a <- 231
tevalyFun = function(){
a=3
teval('a', envir = environment())
}

tevalyFun()
#[1] 3

Object not found error in function

R is expecting for there to be an object called 'drat' in the environment, but 'drat' is a member of the data frame mtcars.

I know there's probably a more elegant solution, but one way to fix it would be to use:

statAncova <- function (dataframe, response, covariate, Factor) {

library(ggplot2)
eval(parse(text=paste0("mod <- aov(",response," ~ ",covariate," + ",Factor,", data=dataframe)")))
pred <- predict(mod)
eval(parse(text=paste0("plotMod <- ggplot(data = cbind(mod$model, pred), aes(",covariate,", ",response,", color=",Factor,")) +
geom_point() +
facet_grid(. ~ ",Factor,") +
geom_line(aes(y=pred))")))

return(list(mod, plotMod))

}

statAncova(mtcars, "drat", "hp", "cyl")

Alternatively, you could just pass the individual variables that you're interested in:

statAncova <- function (response, covariate, Factor) {

dataframe <- data.frame(response, covariate, Factor)

library(ggplot2)
mod <- aov(response ~ covariate + Factor, data=dataframe)
pred <- predict(mod)
plotMod <- ggplot(data = cbind(mod$model, pred), aes(covariate, response, color=Factor)) +
geom_point() +
facet_grid(. ~ Factor) +
geom_line(aes(y=pred))

return(list(mod, plotMod))

}

statAncova(mtcars$drat, mtcars$hp, mtcars$cyl)

R - Defining a function which recognises arguments not as objects, but as being part of the call

You need to study some tutorials about computing on the language. I like doing it with base R, e.g., using bquote.

myfun <- function(TimeVar, EventVar){
TimeVar <- as.name(TimeVar)
EventVar <- as.name(EventVar)

fit <- eval(bquote(survfit(Surv(.(TimeVar), .(EventVar)) ~ Area, data = data_km)))
ggsurvplot(fit)
}

x <- myfun("Duration1", "Event1")
print(x)
#works


Related Topics



Leave a reply



Submit