Search code examples
rbuildpackagertools

Function call built from a custom R package returns the result of an older version of the function


I usually do a standard reprex, SO post, rinse and repeat, but this is a tough one to replicate. I'll try to describe my issue as best as possible.

Background

I'm trying to build a custom package in R which I called, simply, myTools. The build runs with no errors, and all of the functions work (+/- a few things I need to polish up). It is just a collection of functions that I've found helpful in my line of work. I have a particular function get_data(), which reads a CSV file and cleans it up (it is a human-readable, but completely anti-tidy dataset). The function cleans it up and makes the data tidy (imagine an Invoice turned into a tidy dataset, as an example).

Issue

Every time I compile the package, and test the function get_data(), somehow R runs an older version of the function. I've run the lines in the function manually, one-by-one, and the result of the code has no errors and returns the expected tidy dataset. However, if I call the compiled get_data() in the console, an older version of the function is called.

Notes

I added a simple line print("Hello Bob!") to the function. It does not show in the output after build and install. Again, more evidence that R seems to be using some older source of my function and is compiling THAT one.

pkgbuild::find_rtools() returns TRUE

I closed the build project, opened a fresh RStudio/R session. Loaded my library in a blank script file. Upon ctrl-click on the function get_data(), indeed RStudio took me to an older version of the function.

Expected Results

The freshly-compiled version of get_data() to be called in the console/script.

Any hints, ideas to try is appreciated. Thank you!


Solution

  • From the way you describe your workflow, it seems as if this should have been resolved, but since I'm an RStudio expert, I will rely on just my experiences.

    TL;DR

    Find all instances of your package within .libPaths() directories and move or delete them. Try again.

    Explanation

    For me, I often have a workflow of:

    # code some
    devtools::load_all("path/to/package")
    # test/code some more
    devtools::load_all("path/to/package")
    # etc
    
    # every now and then
    devtools::install("path/to/package")
    # or
    install.packages(devtools::build("path/to/package"))
    

    This last part generally works alright for the moment (literally ... never more than the day). However, I've found (unreproducibly, unfortunately, not sure if it's a bug or known design feature) that when there is a directory from for the package, subsequent load_alls are not treated fairly by all functions in other packages. Perhaps there's a way to trace the search path for functions/namespaces.

    So bottom line, make sure you don't have any stale installations found by R's package search mechanisms (.libPaths()).