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: eval trouble into R package - Stack Overflow

Example to run and play:

# 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))

Thanks a lot!!

1 Like

Hi @elflacosebas. I’m rOpenSci’s Community Manager. Thanks for posting your question here. We’ll try to get you a response soon.

It looks like there’s at least one problem. I think on this line https://github.com/elflacosebas/migraR/blob/7bbefbb08292f1750266ab2aaa19e531b4150e86/R/fit_migramod.R#L51 you meant to use dataIn, not data. (in addition, it’s best to avoid variable names that are names of functions in base R like data). If i change that data to dataIn I get other errors.

2 Likes

@elflacosebas If you get some helpful answers from Stack Overflow, please summarize the recommendations here so others can benefit from this thread too. Cheers.

1 Like

Hi there, it has been long time (in between my child and some due dates I had to submit). First of all to summarise I have collected the answers from stack overflow and which I can sum up in two (as @stefanie ask to do): 1. dont use attach (recommendation) instead use [[ or $ or using with, and 2. cesc has runned the package and it worked without the attach so he sent his R session. I have double checked the packages and requirements.

Besides, I have been working (not regularly but sure) in R studio community, and I have got some comments, however, I still have the problem with the eval into an S3 class and still does not recognize the data when I don’t use the attach. I have research on S3 and advanced R but still have the same problem. Also, if there some other way to program the package let me know, as I’m an itermediate-advance in R, probably I’m missing some.

I have also edited what @sckott has said.

To see the comments: https://community.rstudio.com/t/re-eval-function-trouble-when-avoiding-attach-into-package/50566

Thanks a lot.

1 Like