Eval function trouble when avoiding attach into package

Hello!! I have been developing a package for migration analysis, which I consider is in a late state. However, I have had a frequent error which has been my pain in the neck since long time ago. I searched on internet meetups groups and other kind of help and got this website. Believe me, I have been working on it but no hints!

My package is already on Github, it works (somehow), I have made some CMD checks in order to get 0 errors 0 warnings and 0 messages. However, I get always the same error when I run it, which I consider is not a package matter, is more about a misunderstanding of a function.

package address is: https://github.com/elflacosebas/migraR
Stackoverflow tag: https://stackoverflow.com/questions/59249678/eval-trouble-into-r-package

# Calling packages and dataset.
devtools::install_github("elflacosebas/migraR")
require(migraR)
require(dplyr)
data("es_asmr")
data1 <- es_asmr[-c(1,2),c(1,5)]
colnames(data1) <- c("x","y") #EVAL PROBLEM if this line is not runned with best_migramod
attach(data1) #EVAL PROBLEM if this line is not runned with best_migramod

# Creating MigraModel Classes based on Rogers and Castro expressions. 

model.rc.7 = MigraModel(
   name = 'castro_7',
   expr = rc_expression(profile = "seven")
 )
 
model.rc.9 = MigraModel(
   name = 'castro_9',
   expr = rc_expression(profile = "nine")
 )
 
 model.rc.11 = MigraModel(
   name = 'castro_11',
   expr = rc_expression(profile = "eleven")
 )
 
 model.rc.13 = MigraModel(
   name = 'castro_13',
   expr = rc_expression(profile = "thirteen")
 )

# Fitting and Plotting data
 fitted.val.7 <- best_migramod(dataIn = data1, model.rc =model.rc.7, maxite = 5E2, profile = "seven")
 fitted.val.9 <- best_migramod(dataIn = data1, model.rc =model.rc.9, maxite = 5E2, profile = "nine")
 fitted.val.11 <- best_migramod(dataIn = data1, model.rc =model.rc.11, maxite = 5E2, profile = "eleven")
 fitted.val.13 <- best_migramod(dataIn = data1, model.rc =model.rc.13, maxite = 5E2, profile = "thirteen")

x11()
plot(data1, cex=0.1, xlab = 'Age', ylab = 'Standarized Migration Rate')
lines(data1[,1], model.rc.7$value(fitted.val.7$bestParam,data1), col="blue")
lines(data1[,1], model.rc.9$value(fitted.val.9$bestParam,data1), col="orange")
lines(data1[,1], model.rc.11$value(fitted.val.11$bestParam,data1), col="blue", lty=3)
lines(data1[,1], model.rc.13$value(fitted.val.13$bestParam,data1), col="green")
legend('topright',
legend = c(paste("(7)", "MAPE:", round(as.numeric(fitted.val.7$bestMAPE),2), "R²:", round(as.numeric(fitted.val.7$bestRcuad),3)),
           paste("(9)", "MAPE:", round(as.numeric(fitted.val.9$bestMAPE),2), "R²:", round(as.numeric(fitted.val.9$bestRcuad),3)),                     
           paste("(11)", "MAPE:", round(as.numeric(fitted.val.11$bestMAPE),2), "R²:", round(as.numeric(fitted.val.11$bestRcuad),3)), 
           paste("(13)", "MAPE:", round(as.numeric(fitted.val.13$bestMAPE),2), "R²:", round(as.numeric(fitted.val.13$bestRcuad),3))),
           col = c("red",'orange',"blue","darkgreen"), lty = c(2,6,3,5))

Hi, and welcome!

Thanks for the reproducible example, called a reprex. Always helpful.

best_migramod calls fit_migramod; after you initially capture x \& y with

x <- dataIn[, 1]
y <- dataIn[, 2]

the call to fit_migramod is

valSim <- fit_migramod(dataIn, parameters_0 = param_0, model.rc = model.rc)$values

which hasn't been modified as it was earlier.

2 Likes

OK, done, it fits now (maybe with all the changes made I miss that), but if I take out the attach before using the functions, eval error persist as:

 Error in eval(.self$expr, c(as.list(p), as.list(data))) : 
  object 'x' not found 
7. eval(.self$expr, c(as.list(p), as.list(data))) 
6. eval(.self$expr, c(as.list(p), as.list(data))) at migramod.R#21
5. model.rc.7$value(fitted.val.7$bestParam, data1) 
4. xy.coords(x, y) 
3. plot.xy(xy.coords(x, y), type = type, ...) 
2. lines.default(data1[, 1], model.rc.7$value(fitted.val.7$bestParam, data1), col = "blue") 
1. lines(data1[, 1], model.rc.7$value(fitted.val.7$bestParam, data1), col = "blue") 

Pls. Try it out without the lines at the begining of the example... thanks.

NB: I haven't push yet into git your suggestion but it works in my computer.

Hi again. I have made some changes including the ones @technocrat has told me, error continues, and I really dunno what its happening with the eval... maybe it cannot be used inside a S3 class (?).
Regards.

Can you branch your changes to git hub, so I can play along?

Hi, thanks for your willingness. I did update the package once again, if you want to fork it you can, I'm open to contributors. Besides, a question has risen in my head... why is it working in my linux different than in my windows even with the attach? Thanks a lot.

I'll try it on MAC and Ubuntu. I've been WINfree for 15 years.

hehe. #nosurrender :slight_smile: Tnks.