Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R, after modifying a function in a package, modifications are ignored

I created a package in RStudio that contains some custom functions.

When I change something in a function contained in this package, I am using "Build & Reload" in the Build tab to rebuild the package, hoping that this operation "updates" it.

This normally works perfectly. However there is one function for which it is not happening. If I open the function.R file, the modification is right there; however, when I call it from the console or from a script, the older version is loaded. I tried some easy stuff like restarting, and "Clean and Rebuild". I tried removing it, rebuilding, and adding it again, but even in this case, surprisingly, the old version is loaded.

The only thing I noticed in which this function looks different from the others in the package is: screenshot I tried to look into this, but I still lack the necessary general understanding and I am not even sure if this difference is really related to the problem.

Remark 1: the global environment in empty, so it is not masking the function. Remark 2: the search() path is (the name of the package is SDA, and is in second position):

> search()
 [1] ".GlobalEnv"        "package:SDA"       "package:splines"   "tools:rstudio"     "package:stats"     "package:graphics"  "package:grDevices" "package:utils"    
 [9] "package:datasets"  "package:methods"   "Autoloads"         "package:base" 

NB: I am using the package "devtools" for the development.

like image 889
Giuseppe Avatar asked Oct 24 '14 16:10

Giuseppe


2 Answers

I just come to solve the same issue. What you have to do is just restart your R session. It seems that R caches the function and it does not matter if you detach and remove (remove.packages) the package, and then install (install.packages) and load (require) it: You need to close your session, and then with your package built with the changes:

install.packages("path_to_package.tar.gz",repos=NULL,type="source")
require("package")

This worked for me. Hope it helps.

like image 169
elcortegano Avatar answered Oct 08 '22 16:10

elcortegano


I know this is an old question but I just had the same issue and I think I figured out what was causing it. If you have a function in your global environment with the same name as a function in your package and you rebuild the package, the function in your global environment may be called instead of the function in your package. For me, hitting the "brush" icon in the "environment" tab (i.e. "Clear objects from the workspace") and then rebuilding the package did the trick.

like image 34
kenny Avatar answered Oct 08 '22 16:10

kenny