How to avoid sourcing package source?

While developing an R package, I often have an R script handy to run some quick tests. (Later I'll either delete the file or formalize it into something in the tests directory.) I'll edit some of the package source, rebuild the package (or just reload it with Load All), then source the script to see if whatever I was working on is working.

The problem with this workflow is that sometimes I accidentally source the package source instead of the test script. That has the bad side effect of putting copies of the functions from that file into the global workspace, where tests will look first, instead of looking in the package code. The cure is fairly simple (just wipe the workspace clean by running rm(list = ls())), but it's still irritating, and can lead to confusing results from tests if I forget to do the delete.

So my questions are:

  • Is there a way to disable the Source button for files that are part of the package under development?
  • Alternatively, is there a simple way to specify that a particular test script should be run without switching to that tab?
  • What do other people do to run some quick tests in the middle of developing code?

I might have misunderstood your query, but when you run devtools::load_all() like you have mentioned, it loads everything as long as the script is saved. You don't need to run the script from the other tab. You can just run the tests from a blank or test script.

# testing script
devtools::load_all()

example_function(x, y)

The problem is that I am currently looking at some of the source code (not test code) when I want to run the test, which may or may not be a formal test in the tests folder. So when things go right I switch to that tab and source it. When I make a mistake, I just click source, and source the wrong file. The test is longer than one line, so I've got it in its own file.

1 Like

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.