Search code examples
rfunctionpackagerstudiodevtools

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.


Solution

  • 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.