'Can't find function' error even when ?function works.

Hello Everybody!

I just created my very first package and ran into issues trying to make it work on Virtual Machines.

I use this code to install dependencies and open the package, but the main function of the package doesn't work:

install.packages('foreign')

library(foreign)

install.packages('devtools',dependencies=TRUE)

library(devtools)
install_github("Idrissben/ExpoMatch",force=TRUE)
library(ExpoMatch)

lalonde_ctrl <- read.dta("http://www.nber.org/~rdehejia/data/psid_controls.dta")
lalonde_exp <- read.dta("http://www.nber.org/~rdehejia/data/nsw_dw.dta")

lalonde_trt <- lalonde_exp[which(lalonde_exp$treat == 1), ]

lalonde <- rbind(lalonde_trt,lalonde_ctrl)

X <- cbind(lalonde$age, lalonde$re74, lalonde$re75, lalonde$education, lalonde$nodegree, lalonde$married,lalonde$black,lalonde$hispanic)
Tr <- lalonde$treat
Y <- lalonde$re78

set.seed(420)
nm_lalonde = ExpoMatch_function(Tr = Tr, X = X,pop.size = 80, max.generations = 15)

I have seen a similar topic that was solved 2 years but no suggested solutions fixed my issue.

Any help would be greatly appreciated :slight_smile:

Best,
Idriss

failed, so I can't see which function is throwing the error. Here's my transcript

> devtools::install_github("Idrissben/ExpoMatch",force=TRUE)
Downloading GitHub repo Idrissben/ExpoMatch@HEAD
✓  checking for file ‘/tmp/RtmpQtG6oG/remotes53beac60dfe2/Idrissben-ExpoMatch-4957199/DESCRIPTION’ ...
─  preparing ‘ExpoMatch’:
✓  checking DESCRIPTION meta-information ...
─  checking for LF line-endings in source and make files and shell scripts
─  checking for empty or unneeded directories
   Omitted ‘LazyData’ from DESCRIPTION
─  building ‘ExpoMatch_0.0.0.9000.tar.gz’
   
Installing package into ‘/home/roc/R/x86_64-pc-linux-gnu-library/4.1’
(as ‘lib’ is unspecified)
* installing *source* package ‘ExpoMatch’ ...
** using staged installation
** R
** byte-compile and prepare package for lazy loading
Installing package into ‘/home/roc/R/x86_64-pc-linux-gnu-library/4.1/00LOCK-ExpoMatch/00new’
(as ‘lib’ is unspecified)
Error in contrib.url(repos, type) : 
  trying to use CRAN without setting a mirror
Error: unable to load R code in package ‘ExpoMatch’
Execution halted
ERROR: lazy loading failed for package ‘ExpoMatch’
* removing ‘/home/roc/R/x86_64-pc-linux-gnu-library/4.1/ExpoMatch’
Warning message:
In i.p(...) :
  installation of package ‘/tmp/RtmpQtG6oG/file53bea48598720/ExpoMatch_0.0.0.9000.tar.gz’ had non-zero exit status

I think the problem that technocrat runs into is because of the install.packages() call within the code:

install.packages('Matching')
install.packages('rgenoud')
install.packages('foreign')

library(Matching)

library(rgenoud)

library(foreign)

This should be avoided: it's up to the user to decide when and how to install packages. What you can do as a developer is add these packages to the Imports: field in the DESCRIPTION file. See here for details.

If I remove these installation lines and install your package, I can call ExpoMatch_function(), the function is found. You didn't copy the error message in your post, so I don't know what function was not found in your tests. Re-running your code, I get this error message:

Error in GenMatch(Tr = Tr, X = X, pop.size = pop.size * 50, max.generations = max.generations *  : 
  could not find function "GenMatch"

This is because the package {GenMatch} was not loaded: you do have a library(GenMatch) call in your R file, but not inside your function. That means it is run when you install the package, but not when you load it!

So there are a few ways to go. You could have a library() call in your function body, that is not a good idea, and not really useful here (note: on my computer, .packages() gives an empty string so won't work, in any case that is not the ideal approach). I would suggest instead using Matching::GenMatch() to specify the package. You can find other approaches here. And same thing for rgenoud::genoud(). Running that on my computer, it seems to work (although as the example took too long to run, I didn't let it finish).

Oh, and if it wasn't clear from my links, I can't recommend enough to read at least the first 2 chapters of the r-pkgs book.

2 Likes

Thank you so much ! This is incredibly helpful :slight_smile:

Have a blessed day and I hope everything will go well for you!

Yeah, that seems likely based on

trying to use CRAN without setting a mirror

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.