1

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!

Marian Minar
  • 1,344
  • 10
  • 25
  • You say you build it. Do you actually install the new build? – Dason Nov 21 '19 at 17:19
  • @Dason yes, in RStudio > Build > Clean and Rebuild, then I Build > Install and Restart – Marian Minar Nov 21 '19 at 17:31
  • 1
    How exactly are you recompiling the package and how exactly are you testing the function? What is the exact output from these steps you are getting? Without any reproducible example it's almost impossible to tell what might be going on. – MrFlick Nov 21 '19 at 17:40
  • @MrFlick this was my fear. Creating a reproducible example would be too complex: I have sensitive data that I can't share, without which any reprex on my function would fail. There are some warnings and issues during compilation which I ignored initially but are worth mentioning: R keeps reporting that RTools is not installed (it is, in c:\RTools); RTools bin paths are in my Path (win 10). Other issues not likely related: devtools::check() complains about %>% being loaded in NAMESPACE (my attempt at loading the pipe operator into my package) – Marian Minar Nov 21 '19 at 17:48
  • Added a paragraph in **Issue** – Marian Minar Nov 21 '19 at 18:06
  • Added the result of `pkgbuild::find_rtools()` (`TRUE`) – Marian Minar Nov 21 '19 at 18:25
  • Cntrl+click on function takes me to the OLD function source. I'm currently trying to find out where that lives and why RStudio is pointing to this version even when I build the newer one – Marian Minar Nov 21 '19 at 18:32
  • 1
    I don't know that this applies to your workflow, but when I've run into a similar problem, I find that I had previously `devtools::install`'d the package and had not updated it. Even if I subsequently use `devtools::load_all`, I don't know for certain that all of the R instance will always use the `load-all`'d version. Finding the path of the previously-installed package (from `.libPaths()`) and moving/deleting the errant path might help. – r2evans Nov 21 '19 at 18:37
  • 1
    @r2evans thank you that worked. I used `.libPaths()` as you suggested, removed `myTools` from the library directory, rebuilt, reinstalled. Working! Feel free to post your comment as a solution and I'll mark it as official (if desired) – Marian Minar Nov 21 '19 at 19:15

1 Answers1

2

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()).

r2evans
  • 141,215
  • 6
  • 77
  • 149